Mercurial > projects > qtd
annotate generator/dgenerator.cpp @ 287:b6984b290e46 signals
(none)
author | eldar |
---|---|
date | Sun, 08 Nov 2009 19:20:53 +0000 |
parents | 1f6923c8cba0 |
children | f9559a957be9 |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved. | |
4 ** | |
5 ** This file is part of Qt Jambi. | |
6 ** | |
7 ** * Commercial Usage | |
8 * Licensees holding valid Qt Commercial licenses may use this file in | |
9 * accordance with the Qt Commercial License Agreement provided with the | |
10 * Software or, alternatively, in accordance with the terms contained in | |
11 * a written agreement between you and Nokia. | |
12 * | |
13 * | |
14 * GNU General Public License Usage | |
15 * Alternatively, this file may be used under the terms of the GNU | |
16 * General Public License versions 2.0 or 3.0 as published by the Free | |
17 * Software Foundation and appearing in the file LICENSE.GPL included in | |
18 * the packaging of this file. Please review the following information | |
19 * to ensure GNU General Public Licensing requirements will be met: | |
20 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and | |
21 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special | |
22 * exception, Nokia gives you certain additional rights. These rights | |
23 * are described in the Nokia Qt GPL Exception version 1.2, included in | |
24 * the file GPL_EXCEPTION.txt in this package. | |
25 * | |
26 * Qt for Windows(R) Licensees | |
27 * As a special exception, Nokia, as the sole copyright holder for Qt | |
28 * Designer, grants users of the Qt/Eclipse Integration plug-in the | |
29 * right for the Qt/Eclipse Integration to link to functionality | |
30 * provided by Qt Designer and its related libraries. | |
31 * | |
32 * | |
33 * If you are unsure which license is appropriate for your use, please | |
34 * contact the sales department at qt-sales@nokia.com. | |
35 | |
36 ** | |
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
39 ** | |
40 ****************************************************************************/ | |
41 | |
42 #include "dgenerator.h" | |
43 #include "reporthandler.h" | |
44 #include "docparser.h" | |
45 #include "jumptable.h" | |
46 #include "fileout.h" | |
47 | |
48 #include <QtCore/QDir> | |
49 #include <QtCore/QTextStream> | |
50 #include <QtCore/QVariant> | |
51 #include <QtCore/QRegExp> | |
52 #include <QDebug> | |
53 | |
54 #include <iostream> | |
55 | |
56 | |
57 static Indentor INDENT; | |
58 | |
59 DGenerator::DGenerator() | |
60 : m_doc_parser(0), | |
61 m_docs_enabled(false), | |
62 m_native_jump_table(false), | |
63 m_recursive(0), | |
64 m_isRecursive(false) | |
65 { | |
66 excludedTypes << "long long" << "bool" << "int" << "QString" << "char" << "WId" | |
67 << "unsigned char" << "uint" << "double" << "short" << "float" | |
68 << "signed char" << "unsigned short" << "QBool" << "unsigned int" | |
69 << "Qt::HANDLE" << "QChar" << "java.lang.JObjectWrapper" << "void" | |
70 << "QLatin1String" << "unsigned long long" << "signed int" | |
71 << "signed short" << "Array" << "GLuint" << "GLenum" << "GLint" | |
72 << "unsigned long" << "ulong" << "long" << "QByteRef" | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
73 << "QStringList" << "QList" << "QVector" << "QPair" |
39 | 74 << "QSet" << "QStringRef" << "quintptr"; |
1 | 75 } |
76 | |
77 QString DGenerator::fileNameForClass(const AbstractMetaClass *d_class) const | |
78 { | |
79 return QString("%1.d").arg(d_class->name()); | |
80 } | |
81 | |
82 void DGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *field) | |
83 { | |
84 Q_ASSERT(field->isPublic() || field->isProtected()); | |
85 | |
86 const AbstractMetaClass *declaringClass = field->enclosingClass(); | |
87 | |
88 FieldModification mod = declaringClass->typeEntry()->fieldModification(field->name()); | |
89 | |
90 // Set function | |
91 if (mod.isWritable() && !field->type()->isConstant()) { | |
92 const AbstractMetaFunction *setter = field->setter(); | |
93 if (declaringClass->hasFunction(setter)) { | |
94 QString warning = | |
95 QString("class '%1' already has setter '%2' for public field '%3'") | |
96 .arg(declaringClass->name()).arg(setter->name()).arg(field->name()); | |
97 ReportHandler::warning(warning); | |
98 } else { | |
99 if (!notWrappedYet(setter)) // qtd2 | |
100 writeFunction(s, setter); | |
101 } | |
102 } | |
103 | |
104 // Get function | |
105 const AbstractMetaFunction *getter = field->getter(); | |
106 if (mod.isReadable()) { | |
107 if (declaringClass->hasFunction(getter)) { | |
108 QString warning = | |
109 QString("class '%1' already has getter '%2' for public field '%3'") | |
110 .arg(declaringClass->name()).arg(getter->name()).arg(field->name()); | |
111 ReportHandler::warning(warning); | |
112 } else { | |
113 if (!notWrappedYet(getter)) // qtd2 | |
114 writeFunction(s, getter); | |
115 } | |
116 } | |
117 } | |
118 | |
119 QString DGenerator::translateType(const AbstractMetaType *d_type, const AbstractMetaClass *context, Option option) | |
120 { | |
121 QString s; | |
122 | |
123 if (context != 0 && d_type != 0 && context->typeEntry()->isGenericClass() && d_type->originalTemplateType() != 0) | |
124 d_type = d_type->originalTemplateType(); | |
125 | |
188 | 126 QString constPrefix, constPostfix; |
127 if (d_type && d_type->isConstant() && dVersion == 2) { | |
128 constPrefix = "const("; | |
129 constPostfix = ") "; | |
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(); | |
233 // if argument is "QString &" ref attribute needed | |
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); | |
284 | 986 /* |
270 | 987 if(d_function->isSignal()) |
988 functionName += "_emit"; | |
284 | 989 */ |
1 | 990 s << functionName << "("; |
991 writeFunctionArguments(s, d_function, argument_count, option); | |
992 s << ")"; | |
993 | |
994 return result; | |
995 } | |
996 | |
997 void DGenerator::setupForFunction(const AbstractMetaFunction *d_function, | |
998 uint *included_attributes, | |
999 uint *excluded_attributes) const | |
1000 { | |
1001 *excluded_attributes |= d_function->ownerClass()->isInterface() || d_function->isConstructor() | |
1002 ? AbstractMetaAttributes::Native | AbstractMetaAttributes::Final | |
1003 : 0; | |
1004 if (d_function->ownerClass()->isInterface()) | |
1005 *excluded_attributes |= AbstractMetaAttributes::Abstract; | |
1006 if (d_function->needsCallThrough()) | |
1007 *excluded_attributes |= AbstractMetaAttributes::Native; | |
1008 | |
1009 const AbstractMetaClass *d_class = d_function->ownerClass(); | |
1010 retrieveModifications(d_function, d_class, excluded_attributes, included_attributes); | |
1011 } | |
1012 | |
1013 void DGenerator::writeReferenceCount(QTextStream &s, const ReferenceCount &refCount, | |
1014 const QString &argumentName) | |
1015 { | |
1016 if (refCount.action == ReferenceCount::Ignore) | |
1017 return; | |
1018 | |
1019 QString refCountVariableName = refCount.variableName; | |
1020 if (!refCount.declareVariable.isEmpty() && refCount.action != ReferenceCount::Set) { | |
1021 s << INDENT << "auto __rcTmp = " << refCountVariableName << ";" << endl; | |
1022 refCountVariableName = "__rcTmp"; | |
1023 } | |
1024 | |
1025 if (refCount.action != ReferenceCount::Set) { | |
1026 s << INDENT << "if (" << argumentName << " !is null"; | |
1027 | |
1028 if (!refCount.conditional.isEmpty()) | |
1029 s << " && " << refCount.conditional; | |
1030 | |
1031 s << ") {" << endl; | |
1032 } else { | |
1033 if (!refCount.conditional.isEmpty()) | |
1034 s << INDENT << "if (" << refCount.conditional << ") "; | |
1035 s << INDENT << "{" << endl; | |
1036 } | |
1037 | |
1038 { | |
1039 Indentation indent(INDENT); | |
1040 switch (refCount.action) { | |
1041 case ReferenceCount::Add: | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1042 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
|
1043 break; |
1 | 1044 case ReferenceCount::AddAll: |
1045 s << INDENT << refCountVariableName << " ~= " << argumentName << ";" << endl; | |
1046 break; | |
1047 case ReferenceCount::Remove: | |
1048 s << INDENT << "remove(" << refCountVariableName | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1049 << ", cast(Object) " << argumentName << ");" << endl; |
1 | 1050 break; |
1051 case ReferenceCount::Set: | |
1052 { | |
1053 if (refCount.declareVariable.isEmpty()) | |
1054 s << INDENT << refCount.variableName << " = cast(Object) " << argumentName << ";" << endl; | |
1055 else | |
1056 s << INDENT << refCountVariableName << " = cast(Object) " << argumentName << ";" << endl; | |
1057 } | |
1058 default: | |
1059 break; | |
1060 }; | |
1061 } | |
1062 s << INDENT << "}" << endl; | |
1063 } | |
1064 | |
1065 void DGenerator::writeFunction(QTextStream &s, const AbstractMetaFunction *d_function, | |
1066 uint included_attributes, uint excluded_attributes) | |
1067 { | |
1068 s << endl; | |
1069 | |
1070 if (d_function->isModifiedRemoved(TypeSystem::TargetLangCode)) | |
1071 return ; | |
1072 QString functionName = d_function->name(); | |
1073 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
1074 | |
1075 if (!d_function->ownerClass()->isInterface()) { | |
1076 // qtd2 writeEnumOverload(s, d_function, included_attributes, excluded_attributes); | |
1077 // qtd writeFunctionOverloads(s, d_function, included_attributes, excluded_attributes); | |
1078 } | |
1079 /* qtd | |
1080 static QRegExp regExp("^(insert|set|take|add|remove|install).*"); | |
1081 | |
1082 if (regExp.exactMatch(d_function->name())) { | |
1083 AbstractMetaArgumentList arguments = d_function->arguments(); | |
1084 | |
1085 const AbstractMetaClass *c = d_function->implementingClass(); | |
1086 bool hasObjectTypeArgument = false; | |
1087 foreach (AbstractMetaArgument *argument, arguments) { | |
1088 TypeSystem::Ownership d_ownership = d_function->ownership(c, TypeSystem::TargetLangCode, argument->argumentIndex()+1); | |
1089 TypeSystem::Ownership shell_ownership = d_function->ownership(c, TypeSystem::ShellCode, argument->argumentIndex()+1); | |
1090 | |
1091 if (argument->type()->typeEntry()->isObject() | |
1092 && d_ownership == TypeSystem::InvalidOwnership | |
1093 && shell_ownership == TypeSystem::InvalidOwnership) { | |
1094 hasObjectTypeArgument = true; | |
1095 break; | |
1096 } | |
1097 } | |
1098 | |
1099 if (hasObjectTypeArgument | |
1100 && !d_function->isAbstract() | |
1101 && d_function->referenceCounts(d_function->implementingClass()).size() == 0) { | |
1102 m_reference_count_candidate_functions.append(d_function); | |
1103 } | |
1104 } | |
1105 | |
1106 | |
1107 if (m_doc_parser) { | |
1108 QString signature = functionSignature(d_function, | |
1109 included_attributes | NoBlockedSlot, | |
1110 excluded_attributes); | |
1111 s << m_doc_parser->documentationForFunction(signature) << endl; | |
1112 } | |
1113 | |
1114 const QPropertySpec *spec = d_function->propertySpec(); | |
1115 if (spec && d_function->modifiedName() == d_function->originalName()) { | |
1116 if (d_function->isPropertyReader()) { | |
1117 s << " @qt.QtPropertyReader(name=\"" << spec->name() << "\")" << endl; | |
1118 if (!spec->designable().isEmpty()) | |
1119 s << " @qt.QtPropertyDesignable(\"" << spec->designable() << "\")" << endl; | |
1120 } else if (d_function->isPropertyWriter()) { | |
1121 s << " @qt.QtPropertyWriter(name=\"" << spec->name() << "\")" << endl; | |
1122 } else if (d_function->isPropertyResetter()) { | |
1123 s << " @qt.QtPropertyResetter(name=\"" << spec->name() << "\")" | |
1124 << endl; | |
1125 } | |
1126 } | |
1127 */ | |
1128 s << functionSignature(d_function, included_attributes, excluded_attributes); | |
1129 | |
1130 if (d_function->isConstructor()) { | |
1131 writeConstructorContents(s, d_function); | |
1132 } else if (d_function->needsCallThrough() || d_function->isStatic()) { // qtd | |
1133 if (d_function->isAbstract()) { | |
1134 s << ";" << endl; | |
1135 } else { | |
1136 s << " {" << endl; | |
1137 { | |
1138 Indentation indent(INDENT); | |
1139 writeJavaCallThroughContents(s, d_function); | |
1140 } | |
1141 s << INDENT << "}" << endl; | |
1142 } | |
1143 | |
1144 /* qtd | |
1145 if (d_function->jumpTableId() == -1) { | |
1146 writePrivateNativeFunction(s, d_function); | |
1147 } | |
1148 */ | |
1149 } else { | |
1150 s << ";" << endl; | |
1151 } | |
1152 } | |
1153 | |
1154 static void write_equals_parts(QTextStream &s, const AbstractMetaFunctionList &lst, char prefix, bool *first) { | |
1155 foreach (AbstractMetaFunction *f, lst) { | |
1156 AbstractMetaArgument *arg = f->arguments().at(0); | |
1157 QString type = f->typeReplaced(1); | |
1158 if (type.isEmpty()) | |
1159 type = arg->type()->typeEntry()->qualifiedTargetLangName(); | |
1160 s << INDENT << (*first ? "if" : "else if") << " (other instanceof " << type << ")" << endl | |
1161 << INDENT << " return "; | |
1162 if (prefix != 0) s << prefix; | |
1163 s << f->name() << "((" << type << ") other);" << endl; | |
1164 *first = false; | |
1165 } | |
1166 } | |
1167 | |
1168 static void write_compareto_parts(QTextStream &s, const AbstractMetaFunctionList &lst, int value, bool *first) { | |
1169 foreach (AbstractMetaFunction *f, lst) { | |
1170 AbstractMetaArgument *arg = f->arguments().at(0); | |
1171 QString type = f->typeReplaced(1); | |
1172 if (type.isEmpty()) | |
1173 type = arg->type()->typeEntry()->qualifiedTargetLangName(); | |
1174 s << INDENT << (*first ? "if" : "else if") << " (other instanceof " << type << ") {" << endl | |
1175 << INDENT << " if (" << f->name() << "((" << type << ") other)) return " << value << ";" << endl | |
1176 << INDENT << " else return " << -value << ";" << endl | |
1177 << INDENT << "}" << endl; | |
1178 *first = false; | |
1179 } | |
1180 s << INDENT << "throw new ClassCastException();" << endl; | |
1181 } | |
1182 | |
1183 bool DGenerator::isComparable(const AbstractMetaClass *cls) const | |
1184 { | |
1185 AbstractMetaFunctionList eq_functions = cls->equalsFunctions(); | |
1186 AbstractMetaFunctionList neq_functions = cls->notEqualsFunctions(); | |
1187 | |
1188 // Write the comparable functions | |
1189 AbstractMetaFunctionList ge_functions = cls->greaterThanFunctions(); | |
1190 AbstractMetaFunctionList geq_functions = cls->greaterThanEqFunctions(); | |
1191 AbstractMetaFunctionList le_functions = cls->lessThanFunctions(); | |
1192 AbstractMetaFunctionList leq_functions = cls->lessThanEqFunctions(); | |
1193 | |
1194 bool hasEquals = eq_functions.size() || neq_functions.size(); | |
1195 bool isComparable = hasEquals | |
1196 ? ge_functions.size() || geq_functions.size() || le_functions.size() || leq_functions.size() | |
1197 : geq_functions.size() == 1 && leq_functions.size() == 1; | |
1198 | |
1199 return isComparable; | |
1200 } | |
1201 | |
1202 | |
1203 void DGenerator::writeJavaLangObjectOverrideFunctions(QTextStream &s, | |
1204 const AbstractMetaClass *cls) | |
1205 { | |
1206 AbstractMetaFunctionList eq_functions = cls->equalsFunctions(); | |
1207 AbstractMetaFunctionList neq_functions = cls->notEqualsFunctions(); | |
1208 | |
1209 if (eq_functions.size() || neq_functions.size()) { | |
1210 s << endl | |
1211 << INDENT << "@SuppressWarnings(\"unchecked\")" << endl | |
1212 << INDENT << "@Override" << endl | |
1213 << INDENT << "public boolean equals(Object other) {" << endl; | |
1214 bool first = true; | |
1215 write_equals_parts(s, eq_functions, (char) 0, &first); | |
1216 write_equals_parts(s, neq_functions, '!', &first); | |
1217 s << INDENT << " return false;" << endl | |
1218 << INDENT << "}" << endl << endl; | |
1219 } | |
1220 | |
1221 // Write the comparable functions | |
1222 AbstractMetaFunctionList ge_functions = cls->greaterThanFunctions(); | |
1223 AbstractMetaFunctionList geq_functions = cls->greaterThanEqFunctions(); | |
1224 AbstractMetaFunctionList le_functions = cls->lessThanFunctions(); | |
1225 AbstractMetaFunctionList leq_functions = cls->lessThanEqFunctions(); | |
1226 | |
1227 bool hasEquals = eq_functions.size() || neq_functions.size(); | |
1228 bool comparable = isComparable(cls); | |
1229 if (comparable) { | |
1230 s << INDENT << "public int compareTo(Object other) {" << endl; | |
1231 { | |
1232 Indentation indent(INDENT); | |
1233 if (hasEquals) { | |
1234 s << INDENT << "if (equals(other)) return 0;" << endl; | |
1235 bool first = false; | |
1236 if (le_functions.size()) { | |
1237 write_compareto_parts(s, le_functions, -1, &first); | |
1238 } else if (ge_functions.size()) { | |
1239 write_compareto_parts(s, ge_functions, 1, &first); | |
1240 } else if (leq_functions.size()) { | |
1241 write_compareto_parts(s, leq_functions, -1, &first); | |
1242 } else if (geq_functions.size()) { | |
1243 write_compareto_parts(s, geq_functions, 1, &first); | |
1244 } | |
1245 | |
1246 } else if (le_functions.size() == 1) { | |
1247 QString className = cls->typeEntry()->qualifiedTargetLangName(); | |
1248 s << INDENT << "if (operator_less((" << className << ") other)) return -1;" << endl | |
1249 << INDENT << "else if (((" << className << ") other).operator_less(this)) return 1;" << endl | |
1250 << INDENT << "else return 0;" << endl; | |
1251 | |
1252 } else if (geq_functions.size() == 1 && leq_functions.size()) { | |
1253 QString className = cls->typeEntry()->qualifiedTargetLangName(); | |
1254 s << INDENT << "boolean less = operator_less_or_equal((" << className << ") other);" << endl | |
1255 << INDENT << "boolean greater = operator_greater_or_equal((" << className << ") other);" << endl | |
1256 << INDENT << "if (less && greater) return 0;" << endl | |
1257 << INDENT << "else if (less) return -1;" << endl | |
1258 << INDENT << "else return 1;" << endl; | |
1259 } | |
1260 } | |
1261 | |
1262 s << INDENT << "}" << endl; | |
1263 } | |
1264 | |
1265 | |
1266 if (cls->hasHashFunction()) { | |
1267 AbstractMetaFunctionList hashcode_functions = cls->queryFunctionsByName("hashCode"); | |
1268 bool found = false; | |
1269 foreach (const AbstractMetaFunction *function, hashcode_functions) { | |
1270 if (function->actualMinimumArgumentCount() == 0) { | |
1271 found = true; | |
1272 break; | |
1273 } | |
1274 } | |
1275 | |
1276 if (!found) { | |
1277 s << endl | |
1278 << INDENT << "@Override" << endl | |
1279 << INDENT << "public int hashCode() {" << endl | |
1280 << INDENT << " if (nativeId() == 0)" << endl | |
1281 << INDENT << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl | |
1282 << INDENT << " return __qt_hashCode(nativeId());" << endl | |
1283 << INDENT << "}" << endl | |
1284 << INDENT << "native int __qt_hashCode(long __this_nativeId);" << endl; | |
1285 } | |
1286 } | |
1287 | |
1288 // Qt has a standard toString() conversion in QVariant? | |
1289 QVariant::Type type = QVariant::nameToType(cls->qualifiedCppName().toLatin1()); | |
1290 if (QVariant(type).canConvert(QVariant::String) && !cls->hasToStringCapability()) { | |
1291 AbstractMetaFunctionList tostring_functions = cls->queryFunctionsByName("toString"); | |
1292 bool found = false; | |
1293 foreach (const AbstractMetaFunction *function, tostring_functions) { | |
1294 if (function->actualMinimumArgumentCount() == 0) { | |
1295 found = true; | |
1296 break; | |
1297 } | |
1298 } | |
1299 | |
1300 if (!found) { | |
1301 s << endl | |
1302 << INDENT << "@Override" << endl | |
1303 << INDENT << "public String toString() {" << endl | |
1304 << INDENT << " if (nativeId() == 0)" << endl | |
1305 << INDENT << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl | |
1306 << INDENT << " return __qt_toString(nativeId());" << endl | |
1307 << INDENT << "}" << endl | |
1308 << INDENT << "native String __qt_toString(long __this_nativeId);" << endl; | |
1309 } | |
1310 } | |
1311 } | |
1312 | |
1313 void DGenerator::writeEnumOverload(QTextStream &s, const AbstractMetaFunction *d_function, | |
1314 uint include_attributes, uint exclude_attributes) | |
1315 { | |
1316 AbstractMetaArgumentList arguments = d_function->arguments(); | |
1317 | |
1318 if ((d_function->implementingClass() != d_function->declaringClass()) | |
1319 || ((!d_function->isNormal() && !d_function->isConstructor()) || d_function->isEmptyFunction() || d_function->isAbstract())) { | |
1320 return ; | |
1321 } | |
1322 | |
1323 | |
1324 int option = 0; | |
1325 if (d_function->isConstructor()) | |
1326 option = Option(option | SkipReturnType); | |
1327 else | |
1328 include_attributes |= AbstractMetaAttributes::FinalInTargetLang; | |
1329 | |
1330 int generate_enum_overload = -1; | |
1331 for (int i=0; i<arguments.size(); ++i) | |
1332 generate_enum_overload = arguments.at(i)->type()->isTargetLangFlags() ? i : -1; | |
1333 | |
1334 if (generate_enum_overload >= 0) { | |
1335 if (m_doc_parser) { | |
1336 // steal documentation from main function | |
1337 QString signature = functionSignature(d_function, | |
1338 include_attributes | NoBlockedSlot, | |
1339 exclude_attributes); | |
1340 s << m_doc_parser->documentationForFunction(signature) << endl; | |
1341 } | |
1342 | |
1343 s << endl; | |
1344 | |
1345 writeFunctionAttributes(s, d_function, include_attributes, exclude_attributes, option); | |
1346 s << d_function->name() << "("; | |
1347 if (generate_enum_overload > 0) { | |
1348 writeFunctionArguments(s, d_function, generate_enum_overload); | |
1349 s << ", "; | |
1350 } | |
1351 | |
1352 // Write the ellipsis convenience argument | |
1353 AbstractMetaArgument *affected_arg = arguments.at(generate_enum_overload); | |
1354 EnumTypeEntry *originator = ((FlagsTypeEntry *)affected_arg->type()->typeEntry())->originator(); | |
1355 | |
1356 s << originator->javaPackage() << "." << originator->javaQualifier() << "." << originator->targetLangName() | |
1357 << " ... " << affected_arg->argumentName() << ") {" << endl; | |
1358 | |
1359 s << " "; | |
1360 QString new_return_type = d_function->typeReplaced(0); | |
1361 if (new_return_type != "void" && (!new_return_type.isEmpty() || d_function->type() != 0)) | |
1362 s << "return "; | |
1363 | |
1364 if (d_function->isConstructor()) { | |
1365 s << "this"; | |
1366 } else { | |
1367 if (d_function->isStatic()) | |
1368 s << d_function->implementingClass()->fullName() << "."; | |
1369 else | |
1370 s << "this."; | |
1371 s << d_function->name(); | |
1372 } | |
1373 | |
1374 s << "("; | |
1375 for (int i=0; i<generate_enum_overload; ++i) { | |
1376 s << arguments.at(i)->argumentName() << ", "; | |
1377 } | |
1378 s << "new " << affected_arg->type()->fullName() << "(" << affected_arg->argumentName() << "));" << endl | |
1379 << " }" << endl; | |
1380 } | |
1381 } | |
1382 | |
1383 void DGenerator::writeInstantiatedType(QTextStream &s, const AbstractMetaType *abstractMetaType) const | |
1384 { | |
1385 Q_ASSERT(abstractMetaType != 0); | |
1386 | |
1387 const TypeEntry *type = abstractMetaType->typeEntry(); | |
1388 s << type->qualifiedTargetLangName(); | |
1389 | |
1390 if (abstractMetaType->hasInstantiations()) { | |
1391 s << "<"; | |
1392 QList<AbstractMetaType *> instantiations = abstractMetaType->instantiations(); | |
1393 for(int i=0; i<instantiations.size(); ++i) { | |
1394 if (i > 0) | |
1395 s << ", "; | |
1396 | |
1397 writeInstantiatedType(s, instantiations.at(i)); | |
1398 } | |
1399 s << ">"; | |
1400 } | |
1401 } | |
1402 | |
1403 void DGenerator::writeFunctionOverloads(QTextStream &s, const AbstractMetaFunction *d_function, | |
1404 uint include_attributes, uint exclude_attributes) | |
1405 { | |
1406 AbstractMetaArgumentList arguments = d_function->arguments(); | |
1407 int argument_count = arguments.size(); | |
1408 | |
1409 // We only create the overloads for the class that actually declares the function | |
1410 // unless this is an interface, in which case we create the overloads for all | |
1411 // classes that directly implement the interface. | |
1412 const AbstractMetaClass *decl_class = d_function->declaringClass(); | |
1413 if (decl_class->isInterface()) { | |
1414 AbstractMetaClassList interfaces = d_function->implementingClass()->interfaces(); | |
1415 foreach (AbstractMetaClass *iface, interfaces) { | |
1416 if (iface == decl_class) { | |
1417 decl_class = d_function->implementingClass(); | |
1418 break; | |
1419 } | |
1420 } | |
1421 } | |
1422 if (decl_class != d_function->implementingClass()) | |
1423 return; | |
1424 | |
1425 // Figure out how many functions we need to write out, | |
1426 // One extra for each default argument. | |
1427 int overload_count = 0; | |
1428 uint excluded_attributes = AbstractMetaAttributes::Abstract | |
1429 | AbstractMetaAttributes::Native | |
1430 | exclude_attributes; | |
1431 uint included_attributes = (d_function->isConstructor() ? 0 : AbstractMetaAttributes::Final) | include_attributes; | |
1432 | |
1433 for (int i=0; i<argument_count; ++i) { | |
1434 if (!arguments.at(i)->defaultValueExpression().isEmpty() && !d_function->argumentRemoved(i+1)) | |
1435 ++overload_count; | |
1436 } | |
1437 Q_ASSERT(overload_count <= argument_count); | |
1438 for (int i=0; i<overload_count; ++i) { | |
1439 int used_arguments = argument_count - i - 1; | |
1440 | |
1441 QString signature = functionSignature(d_function, included_attributes, | |
1442 excluded_attributes, | |
1443 d_function->isEmptyFunction() | |
1444 || d_function->isNormal() | |
1445 || d_function->isSignal() ? NoOption | |
1446 : SkipReturnType, | |
1447 used_arguments); | |
1448 | |
1449 s << endl; | |
1450 if (m_doc_parser) { | |
1451 s << m_doc_parser->documentationForFunction(signature) << endl; | |
1452 } | |
1453 | |
1454 s << signature << " {\n "; | |
1455 QString new_return_type = d_function->typeReplaced(0); | |
1456 if (new_return_type != "void" && (!new_return_type.isEmpty() || d_function->type())) | |
1457 s << "return "; | |
1458 if (d_function->isConstructor()) | |
1459 s << "this"; | |
1460 else | |
1461 s << d_function->name(); | |
1462 s << "("; | |
1463 | |
1464 int written_arguments = 0; | |
1465 for (int j=0; j<argument_count; ++j) { | |
1466 if (!d_function->argumentRemoved(j+1)) { | |
1467 if (written_arguments++ > 0) | |
1468 s << ", "; | |
1469 | |
1470 if (j < used_arguments) { | |
1471 s << arguments.at(j)->argumentName(); | |
1472 } else { | |
1473 AbstractMetaType *arg_type = 0; | |
1474 QString modified_type = d_function->typeReplaced(j+1); | |
1475 if (modified_type.isEmpty()) { | |
1476 arg_type = arguments.at(j)->type(); | |
1477 if (arg_type->isNativePointer()) { | |
1478 s << "(qt.QNativePointer)"; | |
1479 } else { | |
1480 const AbstractMetaType *abstractMetaType = arguments.at(j)->type(); | |
1481 const TypeEntry *type = abstractMetaType->typeEntry(); | |
1482 if (type->designatedInterface()) | |
1483 type = type->designatedInterface(); | |
1484 if (!type->isEnum() && !type->isFlags()) { | |
1485 s << "("; | |
1486 writeInstantiatedType(s, abstractMetaType); | |
1487 s << ")"; | |
1488 } | |
1489 } | |
1490 } else { | |
1491 s << "(" << modified_type.replace('$', '.') << ")"; | |
1492 } | |
1493 | |
1494 QString defaultExpr = arguments.at(j)->defaultValueExpression(); | |
1495 | |
1496 int pos = defaultExpr.indexOf("."); | |
1497 if (pos > 0) { | |
1498 QString someName = defaultExpr.left(pos); | |
1499 ComplexTypeEntry *ctype = | |
1500 TypeDatabase::instance()->findComplexType(someName); | |
1501 QString replacement; | |
1502 if (ctype != 0 && ctype->isVariant()) | |
1503 replacement = "qt.QVariant."; | |
1504 else if (ctype != 0) | |
1505 replacement = ctype->javaPackage() + "." + ctype->targetLangName() + "."; | |
1506 else | |
1507 replacement = someName + "."; | |
1508 defaultExpr = defaultExpr.replace(someName + ".", replacement); | |
1509 } | |
1510 | |
1511 if (arg_type != 0 && arg_type->isFlags()) { | |
1512 s << "new " << arg_type->fullName() << "(" << defaultExpr << ")"; | |
1513 } else { | |
1514 s << defaultExpr; | |
1515 } | |
1516 } | |
1517 } | |
1518 } | |
1519 s << ");\n }" << endl; | |
1520 } | |
1521 } | |
1522 | |
1523 const TypeEntry* DGenerator::fixedTypeEntry(const TypeEntry *type) | |
1524 { | |
1525 if (!type) | |
1526 return NULL; | |
1527 if (type->designatedInterface()) | |
1528 return type; | |
1529 else if (type->isEnum()) { | |
1530 const EnumTypeEntry *te = static_cast<const EnumTypeEntry *>(type); | |
1531 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
|
1532 if(ownerTe) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1533 typeEntriesEnums << ownerTe; |
1 | 1534 return NULL; |
1535 // return ownerTe; | |
1536 } else if (type->isFlags()) { | |
1537 const FlagsTypeEntry *te = static_cast<const FlagsTypeEntry *>(type); | |
1538 TypeEntry *ownerTe = TypeDatabase::instance()->findType(te->qualifier()); | |
1539 return NULL; | |
1540 // return ownerTe; | |
1541 } else //if (type->isObject()) | |
1542 return type; | |
1543 // else return NULL; | |
1544 } | |
1545 | |
1546 void DGenerator::addInstantiations(const AbstractMetaType* d_type) | |
1547 { | |
1548 if (d_type->isContainer()) { | |
1549 QList<AbstractMetaType *> args = d_type->instantiations(); | |
1550 for (int i=0; i<args.size(); ++i) { | |
1551 const TypeEntry *type = fixedTypeEntry(args.at(i)->typeEntry()); | |
1552 if (type) | |
1553 typeEntries.insert(type); | |
1554 } | |
1555 } | |
1556 } | |
1557 | |
1558 void DGenerator::addTypeEntry(const AbstractMetaClass *d_class, const AbstractMetaFunction *function, QSet<const TypeEntry*> &typeEntries) | |
1559 { | |
1560 // If a method in an interface class is modified to be private, this should | |
1561 // not be present in the interface at all, only in the implementation. | |
1562 if (d_class->isInterface()) { | |
1563 uint includedAttributes = 0; | |
1564 uint excludedAttributes = 0; | |
1565 retrieveModifications(function, d_class, &excludedAttributes, &includedAttributes); | |
1566 if (includedAttributes & AbstractMetaAttributes::Private) | |
1567 return; | |
1568 } | |
1569 | |
1570 if (notWrappedYet(function)) // qtd2 | |
1571 return; | |
1572 | |
1573 // return type for function | |
1574 if (function->type()) { | |
1575 addInstantiations(function->type()); | |
1576 const TypeEntry *type = fixedTypeEntry(function->type()->typeEntry()); | |
1577 if (type) | |
1578 typeEntries.insert(type); | |
1579 } | |
1580 | |
1581 AbstractMetaArgumentList arguments = function->arguments(); | |
1582 for (int i=0; i<arguments.count(); ++i) { | |
1583 const AbstractMetaArgument *arg = arguments.at(i); | |
1584 addInstantiations(arg->type()); | |
1585 const TypeEntry *type = fixedTypeEntry(arg->type()->typeEntry()); | |
1586 if (type) | |
1587 typeEntries.insert(type); | |
1588 } | |
1589 } | |
1590 | |
1591 void DGenerator::fillRequiredImports(const AbstractMetaClass *d_class) | |
1592 { | |
1593 if (m_recursive < 2) { | |
1594 typeEntries.clear(); | |
1595 typeEntriesEnums.clear(); | |
1596 } | |
1597 | |
1598 // import for base class | |
1599 if(d_class->baseClass()) | |
1600 typeEntries << d_class->baseClass()->typeEntry(); | |
1601 | |
1602 //interfaces | |
1603 AbstractMetaClassList interfaces = d_class->interfaces(); | |
1604 if (!interfaces.isEmpty()) { | |
1605 for (int i=0; i<interfaces.size(); ++i) { | |
1606 AbstractMetaClass *iface = interfaces.at(i); | |
1607 InterfaceTypeEntry *te = (InterfaceTypeEntry*) iface->typeEntry(); | |
1608 typeEntries << te->origin(); | |
1609 } | |
1610 } | |
1611 | |
1612 AbstractMetaFunctionList d_funcs = d_class->functionsInTargetLang(); | |
1613 | |
1614 // in case of ConcreteWrapper - adding extra functions | |
1615 if (!d_class->isInterface() && d_class->isAbstract()) { | |
1616 AbstractMetaFunctionList functions_add = d_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NonEmptyFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
1617 d_funcs << functions_add; | |
1618 } | |
1619 | |
1620 for (int i=0; i<d_funcs.size(); ++i) { | |
1621 AbstractMetaFunction *function = d_funcs.at(i); | |
1622 addTypeEntry(d_class, function, typeEntries); | |
1623 } | |
1624 | |
1625 // virtual dispatch | |
1626 AbstractMetaFunctionList virtualFunctions = d_class->virtualFunctions(); | |
1627 for (int i=0; i<virtualFunctions.size(); ++i) { | |
1628 AbstractMetaFunction *function = virtualFunctions.at(i); | |
1629 addTypeEntry(d_class, function, typeEntries); | |
1630 } | |
1631 | |
1632 AbstractMetaFieldList fields = d_class->fields(); | |
1633 foreach (const AbstractMetaField *field, fields) { | |
1634 if (field->wasPublic() || (field->wasProtected() && !d_class->isFinal())) { | |
1635 addTypeEntry(d_class, field->setter(), typeEntries); | |
1636 addTypeEntry(d_class, field->getter(), typeEntries); | |
1637 } | |
1638 } | |
1639 | |
1640 // signals | |
1641 AbstractMetaFunctionList signal_funcs = d_class->queryFunctions(AbstractMetaClass::Signals | |
1642 | AbstractMetaClass::Visible | |
1643 | AbstractMetaClass::NotRemovedFromTargetLang); | |
1644 for (int i=0; i<signal_funcs.size(); ++i) | |
1645 addTypeEntry(d_class, signal_funcs.at(i), typeEntries); | |
1646 | |
1647 if(d_class->isQObject() && d_class->name() != "QObject") | |
1648 typeEntries << TypeDatabase::instance()->findType("QObject"); | |
1649 | |
1650 if(m_recursive == 1) | |
1651 m_recursive++; | |
1652 } | |
1653 | |
1654 void DGenerator::writeImportString(QTextStream &s, const TypeEntry* typeEntry) | |
1655 { | |
1656 /* QString visibility = "private"; | |
1657 if (typeEntry->isNamespace() || typeEntry->name() == "QObject") | |
1658 visibility = "public"; | |
1659 if(d_class->baseClass() && d_class->baseClass()->typeEntry() == typeEntry) | |
1660 visibility = "public";*/ | |
1661 QString visibility = "public"; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1662 s << QString("%1 import ").arg(visibility) << typeEntry->javaPackage() << "." << typeEntry->targetLangName() << ";" << endl; |
1 | 1663 } |
1664 | |
1665 void DGenerator::writeRequiredImports(QTextStream &s, const AbstractMetaClass *d_class) | |
1666 { | |
1667 foreach (const TypeEntry *typeEntry, typeEntriesEnums) { | |
1668 if (!excludedTypes.contains(typeEntry->name()) && d_class->typeEntry() != typeEntry | |
1669 && typeEntry->javaQualifier() != typeEntry->name() | |
1670 /*also*/ && !excludedTypes2.contains(typeEntry->name())) | |
1671 writeImportString(s, typeEntry); | |
1672 } | |
1673 | |
1674 foreach (const TypeEntry *typeEntry, typeEntries) { | |
1675 if (!excludedTypes.contains(typeEntry->name()) && d_class->typeEntry() != typeEntry | |
1676 && typeEntry->javaQualifier() != typeEntry->name() | |
1677 /*also*/ && !excludedTypes2.contains(typeEntry->name())) | |
1678 writeImportString(s, typeEntry); | |
1679 } | |
1680 excludedTypes2.clear(); | |
1681 } | |
1682 | |
1683 void DGenerator::writeDestructor(QTextStream &s, const AbstractMetaClass *d_class) | |
1684 { | |
1685 if (!d_class->hasConstructors()) | |
1686 return; | |
1687 | |
253 | 1688 bool isTheQObject = d_class->name() == "QObject"; |
1689 if (isTheQObject || !d_class->isQObject()) | |
1690 { | |
1691 s << INDENT << "protected override void __deleteNative() {" << endl; | |
1 | 1692 { |
253 | 1693 if (isTheQObject) |
1694 s << INDENT << "qtd_delete_qobject(__nativeId);" << endl; | |
1695 else if (!d_class->isQObject()) | |
1696 s << INDENT << "qtd_" << d_class->name() << "_destructor(__nativeId);" << endl; | |
1 | 1697 } |
1698 s << INDENT << "}" << endl << endl; | |
1699 } | |
1700 } | |
1701 | |
253 | 1702 void DGenerator::writeFlagsSetter(QTextStream &s, const AbstractMetaClass *d_class) |
1 | 1703 { |
1704 if (d_class->isInterface() || d_class->isNamespace()) | |
253 | 1705 s << INDENT << "void __setFlags(QtdObjectFlags flags, bool val);"; |
1706 else // COMPILER BUG: | |
1707 s << INDENT << "void __setFlags(QtdObjectFlags flags, bool val) { super.__setFlags(flags, val); }"; | |
1 | 1708 } |
1709 | |
1710 void DGenerator::writeSignalHandlers(QTextStream &s, const AbstractMetaClass *d_class) | |
1711 { | |
1712 AbstractMetaFunctionList signal_funcs = signalFunctions(d_class); | |
1713 | |
1714 QString attr; | |
1715 | |
1716 s << "// signal handlers" << endl; | |
1717 foreach(AbstractMetaFunction *signal, signal_funcs) { | |
1718 QString sigExternName = signalExternName(d_class, signal); | |
1719 | |
1720 s << "private " << attr << "extern(C) void " << sigExternName << "_connect(void* native_id);" << endl; | |
1721 s << "private " << attr << "extern(C) void " << sigExternName << "_disconnect(void* native_id);" << endl; | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1722 /* |
1 | 1723 QString extra_args; |
1724 | |
1725 foreach (AbstractMetaArgument *argument, arguments) { | |
1726 if(argument->type()->isContainer()) { | |
1727 QString arg_name = argument->indexedName(); | |
1728 const AbstractMetaType *arg_type = argument->type(); | |
1729 QString type_string = translateType(argument->type(), signal->implementingClass(), BoxedPrimitive); | |
1730 extra_args += ", " + type_string + " " + arg_name; | |
1731 } | |
1732 } | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1733 */ |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1734 AbstractMetaArgumentList arguments = signal->arguments(); |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1735 |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1736 s << "private extern(C) void " << sigExternName << "_handle(void* d_entity, void** args) {" << endl; |
1 | 1737 { |
1738 Indentation indent(INDENT); | |
1739 s << INDENT << "auto d_object = cast(" << d_class->name() << ") d_entity;" << endl; | |
1740 int sz = arguments.count(); | |
1741 | |
1742 for (int j=0; j<sz; ++j) { | |
1743 AbstractMetaArgument *argument = arguments.at(j); | |
1744 QString arg_name = argument->indexedName(); | |
1745 AbstractMetaType *type = argument->type(); | |
1746 // if has QString argument we have to pass char* and str.length to QString constructor | |
1747 | |
1748 QString arg_ptr = QString("args[%1]").arg(argument->argumentIndex() + 1); | |
1749 | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1750 if (type->isContainer()) { |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1751 s << INDENT << translateType(type, signal->implementingClass(), BoxedPrimitive) << " " << arg_name << ";" << endl |
270 | 1752 << 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
|
1753 } else if (type->isTargetLangString()) { |
1 | 1754 s << INDENT << "auto " << arg_name << "_ptr = " << arg_ptr << ";" << endl |
1755 << 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
|
1756 } else if(type->isPrimitive() || type->isEnum() || type->isFlags() || type->typeEntry()->isStructInD()) { |
1 | 1757 QString type_name = argument->type()->typeEntry()->qualifiedTargetLangName(); |
1758 if (type->isFlags()) | |
1759 type_name = "int"; | |
1760 s << INDENT << "auto " << arg_name << " = *(cast(" << type_name << "*)" << arg_ptr << ");"; | |
1761 } else if(type->isObject() || type->isQObject() | |
1762 || (type->typeEntry()->isValue() && type->isNativePointer()) | |
1763 || type->isValue()) { | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1764 QString type_name = type->name(); |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1765 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(type->typeEntry()); |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1766 if(ctype->isAbstract()) |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1767 type_name = type_name + "_ConcreteWrapper"; |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1768 s << INDENT << "scope " << arg_name << " = new " << type_name |
253 | 1769 << "(cast(void*)(" << arg_ptr << "), QtdObjectFlags.nativeOwnership);" << endl; |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1770 } |
1 | 1771 s << endl; |
1772 } | |
1773 // s << INDENT << "Stdout(\"" << d_class->name() << "\", \"" << signal->name() << "\").newline;" << endl; | |
284 | 1774 s << INDENT << "d_object." << signal->name() << "_emit("; |
1 | 1775 for (int j = 0; j<sz; ++j) { |
1776 AbstractMetaArgument *argument = arguments.at(j); | |
1777 QString arg_name = argument->indexedName(); | |
1778 if (j != 0) | |
1779 s << ", "; | |
1780 s << arg_name; | |
1781 } | |
1782 | |
1783 s << ");" << endl; | |
1784 } | |
1785 s << "}" << endl; | |
1786 } | |
1787 } | |
1788 | |
1789 void DGenerator::write(QTextStream &s, const AbstractMetaClass *d_class) | |
1790 { | |
1791 ReportHandler::debugSparse("Generating class: " + d_class->fullName()); | |
1792 | |
1793 bool fakeClass = d_class->attributes() & AbstractMetaAttributes::Fake; | |
1794 | |
253 | 1795 |
1796 QString auxModName = d_class->package() + "." + d_class->name() + "_aux"; | |
1797 FileOut auxFile(outputDirectory() + "/" + subDirectoryForClass(d_class) + "/" + d_class->name() + "_aux.d"); | |
1798 auxFile.isDone = true; | |
1799 auxFile.stream << "module " << auxModName << ";" << endl << endl; | |
1800 | |
1801 bool staticInit = d_class->isQObject() || (cpp_shared && d_class->generateShellClass() && !d_class->isInterface()); | |
1802 if (staticInit) | |
1803 { | |
1804 auxFile.isDone = false; | |
1805 auxFile.stream << "extern(C) void static_init_" << d_class->name() << "();" << endl; | |
1806 auxFile.stream << "static this() { static_init_" << d_class->name() << "; }" << endl << endl; | |
1807 } | |
1808 | |
1 | 1809 if (m_docs_enabled) { |
1810 m_doc_parser = new DocParser(m_doc_directory + "/" + d_class->name().toLower() + ".jdoc"); | |
1811 } | |
1812 if (!m_isRecursive) | |
1813 s << "module " << d_class->package() << "." << d_class->name() <<";" << endl << endl; | |
1814 | |
39 | 1815 /* |
1 | 1816 s << "// some type info" << endl; |
1817 QString hasVirtuals = d_class->hasVirtualFunctions() ? "has" : "doesn't have"; | |
1818 QString isFinal = d_class->isFinal() ? "is" : "is not"; | |
1819 QString isNativeId = d_class->typeEntry()->isNativeIdBased() ? "is" : "is not"; | |
1820 s << "// " << hasVirtuals << " virtual functions" << endl | |
1821 << "// " << isFinal << " final" << endl | |
1822 << "// " << isNativeId << " native id based" << endl << endl | |
1823 << "// " << d_class->generateShellClass() << " shell class" << endl | |
1824 << "// " << d_class->hasVirtualFunctions() << endl | |
1825 << "// " << d_class->hasProtectedFunctions() << endl | |
1826 << "// " << d_class->hasFieldAccessors() << endl | |
1827 << "// " << d_class->typeEntry()->isObject() << endl; | |
39 | 1828 */ |
1 | 1829 |
1830 const ComplexTypeEntry *ctype = d_class->typeEntry(); | |
1831 if (!ctype->addedTo.isEmpty() && !m_isRecursive) { | |
1832 ComplexTypeEntry *ctype_parent = TypeDatabase::instance()->findComplexType(ctype->addedTo); | |
1833 s << "public import " << ctype_parent->javaPackage() << "." << ctype_parent->name() << ";" << endl; | |
1834 return; | |
1835 } | |
1836 | |
1837 if(d_class->isInterface() && !m_isRecursive) { | |
1838 s << "public import " << ctype->javaPackage() << "." << ctype->qualifiedCppName() << ";" << endl; | |
1839 return; | |
1840 } | |
1841 AbstractMetaClassList includedClassesList; | |
1842 | |
1843 /* m_recursive is increasing by 1 each time we fill the import for a class | |
1844 if it equals to 0 or 1 imports Set is cleared before a filling cycle - if there | |
1845 is only one class as usual or if there are many classes in module, but before | |
1846 filling for first class we need to clear Set. Wow :) | |
1847 */ | |
1848 if(ctype->includedClasses.size() > 0) | |
1849 m_recursive = 1; | |
1850 else | |
1851 m_recursive = 0; | |
1852 | |
1853 foreach(QString child, ctype->includedClasses) { | |
1854 ComplexTypeEntry *ctype_child = TypeDatabase::instance()->findComplexType(child); | |
1855 foreach (AbstractMetaClass *cls, m_classes) { | |
1856 if ( cls->name() == ctype_child->name() ) { | |
1857 includedClassesList << cls; | |
1858 fillRequiredImports(cls); | |
1859 excludedTypes2 << cls->name(); | |
1860 } | |
1861 } | |
1862 } | |
1863 | |
1864 QString interface; | |
1865 if(d_class->typeEntry()->designatedInterface()) | |
1866 interface = d_class->typeEntry()->designatedInterface()->name(); | |
1867 | |
1868 if(d_class->typeEntry()->designatedInterface()) { | |
1869 foreach (AbstractMetaClass *cls, m_classes) { | |
1870 if ( cls->name() == interface ) { | |
1871 includedClassesList << cls; | |
1872 fillRequiredImports(cls); | |
1873 excludedTypes2 << cls->name(); | |
1874 } | |
1875 } | |
1876 } | |
1877 | |
1878 fillRequiredImports(d_class); | |
1879 excludedTypes2 << d_class->name(); | |
1880 if(ctype->includedClasses.size() > 0) | |
1881 m_recursive = 0; | |
1882 | |
1883 QList<Include> includes = d_class->typeEntry()->extraIncludes(); | |
1884 foreach (const Include &inc, includes) { | |
1885 if (inc.type == Include::TargetLangImport) { | |
1886 s << inc.toString() << endl; | |
1887 } | |
1888 } | |
1889 | |
253 | 1890 // Enums aliases outside of the class - hack |
1891 if (!d_class->enums().isEmpty()) { | |
1892 auxFile.isDone = false; | |
1893 foreach (AbstractMetaEnum *d_enum, d_class->enums()) | |
1894 writeEnum(auxFile.stream, d_enum); | |
1895 } | |
1896 | |
1897 // Auxiliary file contents should have been written at this point | |
1898 if (!auxFile.isDone) | |
1899 { | |
1900 s << "public import " << auxModName << ";" << endl; | |
1901 auxFile.done(); | |
1902 } | |
1903 | |
1 | 1904 if (!m_isRecursive) { |
1905 s << "public import qt.QGlobal;" << endl | |
1906 << "public import qt.core.Qt;" << endl | |
253 | 1907 << "private import qt.QtdObject;" << endl |
81 | 1908 << "private import qt.core.QString;" << endl |
1909 << "private import qt.qtd.Array;" << endl; | |
1 | 1910 if (d_class->isQObject()) { |
253 | 1911 s << "public import qt.Signal;" << endl |
1912 << "public import qt.core.QMetaObject;" << endl | |
1913 << "public import qt.qtd.Traits;" << endl; | |
1914 | |
1 | 1915 if (d_class->name() != "QObject") |
1916 s << "public import qt.core.QObject;" << endl; | |
1917 } | |
1918 | |
1919 // qtd2 hack! | |
1920 if (d_class->name() == "QCoreApplication") | |
1921 s << "private import qt.core.ArrayOps;" << endl; | |
1922 else if (d_class->name() == "QApplication") | |
1923 s << "private import qt.gui.ArrayOps;" << endl; | |
1924 | |
187
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1925 /* |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1926 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
|
1927 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
|
1928 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
|
1929 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
|
1930 */ |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1931 if(d_class->isQObject()) |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1932 s << "private import " << d_class->package() << ".ArrayOps2;" << endl; |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1933 |
1 | 1934 s << "// automatic imports-------------" << endl; |
1935 writeRequiredImports(s, d_class); | |
1936 s << endl; | |
1937 if (dPhobos) | |
1938 { | |
1939 s << "import std.stdio;" << endl | |
1940 << "import std.string;" << endl | |
1941 << "import std.utf;" << endl | |
253 | 1942 << "import core.memory;" << endl; |
1 | 1943 } |
1944 else | |
1945 { | |
1946 s << "import tango.io.Stdout;" << endl | |
1947 << "import tango.stdc.stringz;" << endl | |
1948 << "import tango.text.convert.Utf;" << endl | |
253 | 1949 << "import tango.core.Memory;" << endl; |
1 | 1950 } |
253 | 1951 |
1952 s << endl << endl; | |
1 | 1953 } |
1954 | |
1955 if (m_doc_parser) { | |
1956 s << m_doc_parser->documentation(d_class) << endl << endl; | |
1957 } | |
1958 | |
188 | 1959 /* qtd s << "@QtJambiGeneratedClass" << endl; |
1960 | |
1961 if ((d_class->typeEntry()->typeFlags() & ComplexTypeEntry::Deprecated) != 0) { | |
1962 s << "@Deprecated" << endl; | |
1963 } | |
1964 */ | |
1965 | |
1966 | |
1 | 1967 if (d_class->isInterface()) { |
1968 s << "public interface "; | |
1969 } else { | |
1970 if (d_class->isPublic()) | |
1971 s << "public "; | |
1972 // else friendly | |
1973 | |
1974 bool force_abstract = (d_class->typeEntry()->typeFlags() & ComplexTypeEntry::ForceAbstract) != 0; | |
1975 if (d_class->isFinal() && !force_abstract) | |
1976 s << "final "; | |
1977 if ((d_class->isAbstract() && !d_class->isNamespace()) || force_abstract) | |
1978 s << "abstract "; | |
1979 | |
1980 if (!d_class->typeEntry()->targetType().isEmpty()) { | |
1981 s << d_class->typeEntry()->targetType() << " "; | |
1982 } else if (d_class->isNamespace() && d_class->functionsInTargetLang().size() == 0) { | |
1983 s << "interface "; | |
1984 } else if (d_class->isNamespace()) { | |
1985 s << "class "; | |
1986 } else { | |
1987 s << "class "; | |
1988 } | |
1989 | |
1990 } | |
1991 | |
1992 const ComplexTypeEntry *type = d_class->typeEntry(); | |
1993 | |
1994 s << d_class->name(); | |
1995 | |
1996 if (type->isGenericClass()) { | |
1997 s << "<"; | |
1998 QList<TypeEntry *> templateArguments = d_class->templateBaseClass()->templateArguments(); | |
1999 for (int i=0; i<templateArguments.size(); ++i) { | |
2000 TypeEntry *templateArgument = templateArguments.at(i); | |
2001 if (i > 0) | |
2002 s << ", "; | |
2003 s << templateArgument->name(); | |
2004 } | |
2005 s << ">"; | |
2006 } | |
2007 | |
2008 if (!d_class->isNamespace() && !d_class->isInterface()) { | |
2009 if (!d_class->baseClassName().isEmpty()) { | |
2010 s << " : " << d_class->baseClass()->name(); | |
2011 } else { | |
2012 QString sc = type->defaultSuperclass(); | |
2013 if ((sc != d_class->name()) && !sc.isEmpty()) | |
2014 s << " : " << sc; | |
2015 } | |
2016 }/* qtd else if (d_class->isInterface()) { | |
2017 s << " extends QtJambiInterface"; | |
2018 }*/ | |
2019 | |
2020 // implementing interfaces... | |
2021 bool implements = false; | |
2022 AbstractMetaClassList interfaces = d_class->interfaces(); | |
2023 if (!interfaces.isEmpty()) { | |
2024 if (!d_class->isInterface()) | |
2025 s << ", "; | |
2026 else { | |
2027 implements = true; | |
2028 s << ": "; | |
2029 } | |
2030 for (int i=0; i<interfaces.size(); ++i) { | |
2031 AbstractMetaClass *iface = interfaces.at(i); | |
2032 if (i) s << ", "; | |
2033 s << iface->name(); | |
2034 } | |
2035 } | |
2036 /* qtd | |
2037 if (isComparable(d_class)) { | |
2038 if (!implements) { | |
2039 implements = true; | |
2040 s << endl << " implements "; | |
2041 } | |
2042 else | |
2043 s << "," << endl << " "; | |
2044 s << "java.lang.Comparable<Object>"; | |
2045 } | |
2046 | |
2047 if (d_class->hasCloneOperator()) { | |
2048 if (!implements) { | |
2049 implements = true; | |
2050 s << endl << " implements "; | |
2051 } | |
2052 else | |
2053 s << "," << endl << " "; | |
2054 s << "java.lang.Cloneable"; | |
2055 } | |
2056 */ | |
2057 s << endl << "{" << endl; | |
2058 | |
2059 Indentation indent(INDENT); | |
2060 | |
2061 // Define variables for reference count mechanism | |
2062 if (!d_class->isInterface() && !d_class->isNamespace()) { | |
2063 QHash<QString, int> variables; | |
2064 foreach (AbstractMetaFunction *function, d_class->functions()) { | |
2065 QList<ReferenceCount> referenceCounts = function->referenceCounts(d_class); | |
2066 foreach (ReferenceCount refCount, referenceCounts) { | |
2067 variables[refCount.variableName] |= refCount.action | |
2068 | refCount.access | |
2069 | (refCount.threadSafe ? ReferenceCount::ThreadSafe : 0) | |
2070 | (function->isStatic() ? ReferenceCount::Static : 0) | |
2071 | (refCount.declareVariable.isEmpty() ? ReferenceCount::DeclareVariable : 0); | |
2072 } | |
2073 } | |
2074 | |
2075 foreach (QString variableName, variables.keys()) { | |
2076 int actions = variables.value(variableName) & ReferenceCount::ActionsMask; | |
2077 // bool threadSafe = variables.value(variableName) & ReferenceCount::ThreadSafe; | |
2078 bool isStatic = variables.value(variableName) & ReferenceCount::Static; | |
2079 bool declareVariable = variables.value(variableName) & ReferenceCount::DeclareVariable; | |
2080 int access = variables.value(variableName) & ReferenceCount::AccessMask; | |
2081 | |
2082 if (actions == ReferenceCount::Ignore || !declareVariable) | |
2083 continue; | |
2084 | |
2085 if (((actions & ReferenceCount::Add) == 0) != ((actions & ReferenceCount::Remove) == 0)) { | |
2086 QString warn = QString("either add or remove specified for reference count variable '%1' in '%2' but not both") | |
2087 .arg(variableName).arg(d_class->fullName()); | |
2088 ReportHandler::warning(warn); | |
2089 } | |
2090 s << endl; | |
2091 /* qtd | |
2092 if (TypeDatabase::instance()->includeEclipseWarnings()) | |
2093 s << INDENT << "@SuppressWarnings(\"unused\")" << endl; | |
2094 */ | |
2095 if (actions != ReferenceCount::Set && actions != ReferenceCount::Ignore) { // qtd2 | |
2096 | |
2097 s << INDENT; | |
2098 switch (access) { | |
2099 case ReferenceCount::Private: | |
2100 s << "package "; break; // qtd | |
2101 case ReferenceCount::Protected: | |
2102 s << "protected "; break; | |
2103 case ReferenceCount::Public: | |
2104 s << "public "; break; | |
2105 default: | |
2106 s << "protected"; // friendly | |
2107 } | |
2108 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2109 } // qtd2 |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2110 |
1 | 2111 if (isStatic) |
2112 s << "static "; | |
2113 | |
2114 if (actions != ReferenceCount::Set && actions != ReferenceCount::Ignore) { | |
2115 s << "Object[] " << variableName << ";" << endl; | |
2116 /* | |
2117 if (threadSafe) | |
2118 s << "java.util.Collections.synchronizedCollection("; | |
2119 s << "new java.util.ArrayList<Object>()"; | |
2120 if (threadSafe) | |
2121 s << ")"; | |
2122 s << ";" << endl;*/ | |
2123 } else if (actions != ReferenceCount::Ignore) { | |
2124 /* qtd2 if (threadSafe) | |
2125 s << "synchronized ";*/ | |
2126 s << "Object " << variableName << " = null;" << endl; | |
2127 } | |
2128 } | |
2129 s << endl; | |
2130 } | |
2131 | |
2132 /* qtd2 | |
2133 if (!d_class->isInterface() && (!d_class->isNamespace() || d_class->functionsInTargetLang().size() > 0) | |
2134 && (d_class->baseClass() == 0 || d_class->package() != d_class->baseClass()->package())) { | |
2135 s << endl | |
2136 << INDENT << "static {" << endl; | |
2137 | |
2138 if (d_class->isNamespace()) { | |
2139 s << INDENT << " qt.QtJambi_LibraryInitializer.init();" << endl; | |
2140 } | |
2141 | |
2142 s << INDENT << " " << d_class->package() << ".QtJambi_LibraryInitializer.init();" << endl | |
2143 << INDENT << "}" << endl; | |
2144 } | |
2145 */ | |
2146 | |
2147 // Enums aliaases | |
2148 foreach (AbstractMetaEnum *d_enum, d_class->enums()) | |
2149 writeEnumAlias(s, d_enum); | |
2150 | |
253 | 2151 // Signals |
2152 if (d_class->isQObject()) | |
1 | 2153 { |
253 | 2154 AbstractMetaFunctionList signal_funcs = signalFunctions(d_class, false); |
2155 writeSignalSignatures(s, d_class, signal_funcs); | |
2156 | |
1 | 2157 foreach (AbstractMetaFunction *signal, signal_funcs) |
270 | 2158 { |
253 | 2159 writeSignal(s, signal); |
270 | 2160 if(!signal->isPrivate()) |
2161 writeFunction(s, signal); | |
2162 } | |
1 | 2163 } |
2164 | |
2165 // Class has subclasses but also only private constructors | |
2166 if (!d_class->isFinalInTargetLang() && d_class->isFinalInCpp()) { | |
2167 s << endl << INDENT << "/**" << endl | |
2168 << INDENT << " * This constructor is a place holder intended to prevent" << endl | |
2169 << INDENT << " * users from subclassing the class. Certain classes can" << endl | |
2170 << INDENT << " * unfortunately only be subclasses internally. The constructor" << endl | |
2171 << INDENT << " * will indiscriminately throw an exception if called. If the" << endl | |
2172 << INDENT << " * exception is ignored, any use of the constructed object will" << endl | |
2173 << INDENT << " * cause an exception to occur." << endl << endl | |
2174 << INDENT << " * @throws QClassCannotBeSubclassedException" << endl | |
2175 << INDENT << " **/" << endl | |
2176 << INDENT << "protected " << d_class->name() << "() throws QClassCannotBeSubclassedException {" << endl | |
2177 << INDENT << " throw new QClassCannotBeSubclassedException(" << d_class->name() << ".class);" << endl | |
2178 << INDENT << "}" << endl << endl; | |
2179 } | |
2180 s << "// Functions" << endl; | |
2181 | |
2182 // Functions | |
2183 AbstractMetaFunctionList d_funcs = d_class->functionsInTargetLang(); | |
2184 for (int i=0; i<d_funcs.size(); ++i) { | |
2185 AbstractMetaFunction *function = d_funcs.at(i); | |
2186 | |
2187 // If a method in an interface class is modified to be private, this should | |
2188 // not be present in the interface at all, only in the implementation. | |
2189 if (d_class->isInterface()) { | |
2190 uint includedAttributes = 0; | |
2191 uint excludedAttributes = 0; | |
2192 retrieveModifications(function, d_class, &excludedAttributes, &includedAttributes); | |
2193 if (includedAttributes & AbstractMetaAttributes::Private) | |
2194 continue; | |
2195 } | |
2196 | |
2197 if (!notWrappedYet(function)) // qtd2 | |
2198 writeFunction(s, function); | |
2199 // s << function->minimalSignature() << endl; | |
2200 } | |
2201 if(d_class->isInterface()) | |
2202 s << endl << INDENT << "public void* __ptr_" << d_class->name() << "();" << endl << endl; | |
2203 | |
2204 | |
2205 s << "// Field accessors" << endl; | |
2206 // Field accessors | |
2207 AbstractMetaFieldList fields = d_class->fields(); | |
2208 foreach (const AbstractMetaField *field, fields) { | |
2209 if (field->wasPublic() || (field->wasProtected() && !d_class->isFinal())) | |
2210 writeFieldAccessors(s, field); | |
2211 } | |
2212 | |
2213 /* qtd | |
2214 | |
2215 // the static fromNativePointer function... | |
2216 if (!d_class->isNamespace() && !d_class->isInterface() && !fakeClass) { | |
2217 s << endl | |
2218 << INDENT << "public static native " << d_class->name() << " fromNativePointer(" | |
2219 << "QNativePointer nativePointer);" << endl; | |
2220 } | |
2221 | |
2222 if (d_class->isQObject()) { | |
2223 s << endl; | |
2224 if (TypeDatabase::instance()->includeEclipseWarnings()) | |
2225 s << INDENT << "@SuppressWarnings(\"unused\")" << endl; | |
2226 | |
2227 s << INDENT << "private static native long originalMetaObject();" << endl; | |
2228 } | |
2229 | |
2230 // The __qt_signalInitialization() function | |
2231 if (signal_funcs.size() > 0) { | |
2232 s << endl | |
2233 << INDENT << "@Override" << endl | |
2234 << INDENT << "@QtBlockedSlot protected boolean __qt_signalInitialization(String name) {" << endl | |
2235 << INDENT << " return (__qt_signalInitialization(nativeId(), name)" << endl | |
2236 << INDENT << " || super.__qt_signalInitialization(name));" << endl | |
2237 << INDENT << "}" << endl | |
2238 << endl | |
2239 << INDENT << "@QtBlockedSlot" << endl | |
2240 << INDENT << "private native boolean __qt_signalInitialization(long ptr, String name);" << endl; | |
2241 } | |
2242 */ | |
253 | 2243 if (d_class->isQObject()) |
2244 writeQObjectFunctions(s, d_class); | |
2245 | |
1 | 2246 // Add dummy constructor for use when constructing subclasses |
2247 if (!d_class->isNamespace() && !d_class->isInterface() && !fakeClass) { | |
2248 s << endl | |
2249 << INDENT << "public " | |
2250 << "this"; | |
2251 | |
253 | 2252 |
2253 Indentation indent(INDENT); | |
2254 | |
2255 s << "(void* native_id, QtdObjectFlags flags = QtdObjectFlags.none) {" << endl | |
2256 << INDENT << "super(native_id, flags);" << endl; | |
2257 | |
2258 if (d_class->name() == "QObject") | |
1 | 2259 { |
253 | 2260 // To prevent GC from collecting the object, add it to the statically rooted linked list |
2261 s << INDENT << " __next = __root;" << endl | |
2262 << INDENT << " __root = this;" << endl | |
2263 << INDENT << " if (__next) {" << endl | |
2264 << INDENT << " __next.__prev = this;" << endl | |
2265 << INDENT << " }" << endl << endl; | |
1 | 2266 } |
253 | 2267 |
2268 /* | |
223 | 2269 if (cpp_shared) { |
2270 if (d_class->generateShellClass() && !d_class->isInterface()) | |
253 | 2271 s << INDENT << "if (!static_inited)" << endl |
223 | 2272 << INDENT << " static_init_" << d_class->name() << "();" << endl << endl; |
2273 } | |
253 | 2274 */ |
2275 | |
1 | 2276 // customized store-result instances |
2277 d_funcs = d_class->functionsInTargetLang(); | |
2278 for (int i=0; i<d_funcs.size(); ++i) { | |
2279 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2280 uint included_attributes = 0; | |
2281 uint excluded_attributes = 0; | |
2282 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2283 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2284 bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2285 | |
2286 if (!isStatic && (attr & AbstractMetaAttributes::Abstract)) | |
2287 continue; | |
2288 | |
2289 if(d_function->storeResult()) { | |
2290 QString type_name = d_function->type()->name(); | |
2291 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2292 if(ctype->isAbstract()) | |
2293 type_name = type_name + "_ConcreteWrapper"; | |
2294 | |
2295 s << INDENT << " __m_" << d_function->name() << " = new " | |
2296 << type_name << "(cast(void*)null);" << endl; | |
2297 if (d_function->type()->isQObject()) | |
253 | 2298 s << INDENT << " __m_" << d_function->name() << ".__setFlags(QtdObjectFlags.nativeOwnership, true);" << endl; |
1 | 2299 } |
2300 } | |
2301 | |
2302 // pointers to native interface objects for classes that implement interfaces | |
2303 // initializing | |
2304 interfaces = d_class->interfaces(); | |
2305 if (!interfaces.isEmpty()) { | |
2306 for (int i=0; i<interfaces.size(); ++i) { | |
2307 AbstractMetaClass *iface = interfaces.at(i); | |
2308 | |
2309 s << INDENT << " __m_ptr_" << iface->name() << " = qtd_" << d_class->name() << "_cast_to_" << iface->qualifiedCppName() | |
253 | 2310 << "(__nativeId);" << endl; |
1 | 2311 } |
2312 } | |
2313 | |
2314 | |
2315 s << INDENT << "}" << endl << endl; | |
2316 | |
253 | 2317 /******************!!!DUPLICATE OF ABOVE!!!*********************/ |
1 | 2318 for (int i=0; i<d_funcs.size(); ++i) { |
2319 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2320 uint included_attributes = 0; | |
2321 uint excluded_attributes = 0; | |
2322 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2323 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2324 bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2325 | |
2326 if (!isStatic && (attr & AbstractMetaAttributes::Abstract)) | |
2327 continue; | |
2328 | |
2329 if(d_function->storeResult()) { | |
2330 QString type_name = d_function->type()->name(); | |
2331 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2332 if(ctype->isAbstract()) | |
2333 type_name = type_name + "_ConcreteWrapper"; | |
2334 | |
2335 s << INDENT << type_name << " __m_" << d_function->name() << ";" << endl; | |
2336 } | |
2337 } | |
2338 /***************************************************************/ | |
2339 | |
2340 // pointers to native interface objects for classes that implement interfaces | |
2341 // initializing | |
2342 interfaces = d_class->interfaces(); | |
2343 if (!interfaces.isEmpty()) { | |
2344 for (int i=0; i<interfaces.size(); ++i) { | |
2345 AbstractMetaClass *iface = interfaces.at(i); | |
2346 | |
2347 s << INDENT << "private void* __m_ptr_" << iface->name() << ";" << endl | |
2348 << INDENT << "public void* __ptr_" << iface->name() << "() { return __m_ptr_" << iface->name() << "; }" << endl << endl; | |
2349 } | |
2350 } | |
2351 | |
2352 writeDestructor(s, d_class); | |
2353 } | |
2354 | |
2355 /* qtd | |
2356 // Add a function that converts an array of the value type to a QNativePointer | |
2357 if (d_class->typeEntry()->isValue() && !fakeClass) { | |
2358 s << endl | |
2359 << INDENT << "public static native QNativePointer nativePointerArray(" << d_class->name() | |
2360 << " array[]);" << endl; | |
2361 } | |
2362 | |
2363 // write the cast to this function.... | |
2364 if (d_class->isInterface()) { | |
2365 s << endl | |
2366 << " public long __qt_cast_to_" | |
2367 << static_cast<const InterfaceTypeEntry *>(type)->origin()->targetLangName() | |
2368 << "(long ptr);" << endl; | |
2369 } else { | |
2370 foreach (AbstractMetaClass *cls, interfaces) { | |
2371 s << endl | |
2372 << " @QtBlockedSlot public native long __qt_cast_to_" | |
2373 << static_cast<const InterfaceTypeEntry *>(cls->typeEntry())->origin()->targetLangName() | |
2374 << "(long ptr);" << endl; | |
2375 } | |
2376 } | |
2377 */ | |
2378 | |
2379 /* qtd writeJavaLangObjectOverrideFunctions(s, d_class); | |
2380 */ | |
253 | 2381 writeFlagsSetter(s, d_class); |
1 | 2382 s << "// Injected code in class" << endl; |
2383 writeExtraFunctions(s, d_class); | |
2384 // qtd2 writeToStringFunction(s, d_class); | |
2385 /* qtd | |
2386 if (d_class->hasCloneOperator()) { | |
2387 writeCloneFunction(s, d_class); | |
2388 } | |
2389 */ | |
2390 s << "}" << endl; | |
2391 | |
81 | 2392 /* ---------------- injected free code ----------------*/ |
2393 const ComplexTypeEntry *class_type = d_class->typeEntry(); | |
2394 Q_ASSERT(class_type); | |
2395 | |
2396 CodeSnipList code_snips = class_type->codeSnips(); | |
2397 foreach (const CodeSnip &snip, code_snips) { | |
2398 if (!d_class->isInterface() && snip.language == TypeSystem::TargetLangFreeCode) { | |
2399 s << endl; | |
2400 snip.formattedCode(s, INDENT); | |
2401 } | |
2402 } | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2403 /* --------------------------------------------------- */ |
14 | 2404 |
1 | 2405 interfaces = d_class->interfaces(); |
2406 if (!interfaces.isEmpty()) { | |
2407 for (int i=0; i<interfaces.size(); ++i) { | |
2408 AbstractMetaClass *iface = interfaces.at(i); | |
2409 | |
2410 s << INDENT << "private static extern (C) void*" << "qtd_" << d_class->name() << "_cast_to_" << iface->qualifiedCppName() | |
2411 << "(void* nativeId);" << endl; | |
2412 } | |
2413 } | |
2414 | |
2415 if (!d_class->isInterface() && d_class->isAbstract()) { | |
2416 s << endl; | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2417 |
1 | 2418 s << INDENT << "public class " << d_class->name() << "_ConcreteWrapper : " << d_class->name() << " {" << endl; |
2419 | |
2420 { | |
2421 Indentation indent(INDENT); | |
253 | 2422 QString hasShellFlag = d_class->generateShellClass() ? " | QtdObjectFlags.hasShell" : ""; |
2423 s << INDENT << "public this(void* native_id, QtdObjectFlags flags = QtdObjectFlags.nativeOwnership) {" << endl | |
2424 << INDENT << " super(native_id, flags);" << endl << endl; | |
2425 | |
2426 /******************!!!DUPLICATE!!!*********************/ | |
1 | 2427 d_funcs = d_class->functionsInTargetLang(); |
2428 for (int i=0; i<d_funcs.size(); ++i) { | |
2429 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2430 uint included_attributes = 0; | |
2431 uint excluded_attributes = 0; | |
2432 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2433 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2434 // qtd bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2435 | |
2436 if(d_function->storeResult()) { | |
2437 QString type_name = d_function->type()->name(); | |
2438 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2439 if(ctype->isAbstract()) | |
2440 type_name = type_name + "_ConcreteWrapper"; | |
2441 s << INDENT << " __m_" << d_function->name() << " = new " | |
2442 << type_name << "(cast(void*)null);" << endl; | |
2443 if (d_function->type()->isQObject()) | |
253 | 2444 s << INDENT << " __m_" << d_function->name() << ".__setFlags(QtdObjectFlags.nativeOwnership, true);" << endl; |
1 | 2445 } |
2446 } | |
2447 | |
2448 s << INDENT << "}" << endl << endl; | |
2449 | |
2450 for (int i=0; i<d_funcs.size(); ++i) { | |
2451 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2452 uint included_attributes = 0; | |
2453 uint excluded_attributes = 0; | |
2454 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2455 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2456 // qtd bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2457 | |
2458 if(d_function->storeResult()) { | |
2459 QString type_name = d_function->type()->name(); | |
2460 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2461 if(ctype->isAbstract()) | |
2462 type_name = type_name + "_ConcreteWrapper"; | |
2463 | |
2464 s << INDENT << d_function->type()->name() << " __m_" << d_function->name() << ";" << endl; | |
2465 } | |
2466 } | |
2467 /***************************************************************/ | |
2468 | |
2469 | |
2470 | |
2471 | |
2472 | |
2473 | |
2474 uint exclude_attributes = AbstractMetaAttributes::Native | AbstractMetaAttributes::Abstract; | |
2475 uint include_attributes = 0; | |
2476 AbstractMetaFunctionList functions = d_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NonEmptyFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
2477 foreach (const AbstractMetaFunction *d_function, functions) { | |
2478 retrieveModifications(d_function, d_class, &exclude_attributes, &include_attributes); | |
2479 if (notWrappedYet(d_function)) | |
2480 continue; | |
2481 /* qtd s << endl | |
2482 << INDENT << "@Override" << endl; */ | |
2483 writeFunctionAttributes(s, d_function, include_attributes, exclude_attributes, | |
2484 d_function->isNormal() || d_function->isSignal() ? 0 : SkipReturnType); | |
2485 | |
2486 s << d_function->name() << "("; | |
2487 writeFunctionArguments(s, d_function, d_function->arguments().count()); | |
2488 s << ") {" << endl; | |
2489 { | |
2490 Indentation indent(INDENT); | |
2491 writeJavaCallThroughContents(s, d_function, SuperCall); | |
2492 } | |
2493 s << INDENT << "}" << endl; | |
2494 } | |
2495 } | |
2496 s << INDENT << "}" << endl << endl; | |
2497 } | |
2498 | |
2499 if (d_class->generateShellClass()) { // qtd2 | |
2500 if (d_class->hasVirtualFunctions() | |
253 | 2501 && (d_class->typeEntry()->isObject() && !d_class->typeEntry()->isQObject()) ) |
1 | 2502 s << endl << "extern (C) void *__" << d_class->name() << "_entity(void *q_ptr);" << endl << endl; |
2503 } | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2504 |
110 | 2505 |
152 | 2506 // if (d_class->needsConversionFunc) |
110 | 2507 writeConversionFunction(s, d_class); |
1 | 2508 |
253 | 2509 if (d_class->hasConstructors() && !d_class->isQObject()) |
1 | 2510 s << "extern (C) void qtd_" << d_class->name() << "_destructor(void *ptr);" << endl << endl; |
2511 | |
2512 // qtd | |
270 | 2513 |
2514 s << endl << "// C wrappers for signal emitters" << endl; | |
2515 | |
2516 if (d_class->isQObject()) | |
2517 { | |
2518 AbstractMetaFunctionList signal_funcs = signalFunctions(d_class, false); | |
2519 | |
2520 foreach (AbstractMetaFunction *signal, signal_funcs) | |
2521 if(!signal->isPrivate()) | |
2522 writePrivateNativeFunction(s, signal); | |
2523 } | |
2524 | |
1 | 2525 s << endl << "// C wrappers" << endl; |
2526 d_funcs = d_class->functionsInTargetLang(); | |
2527 if (!d_class->isInterface()) | |
2528 for (int i=0; i<d_funcs.size(); ++i) { | |
2529 AbstractMetaFunction *function = d_funcs.at(i); | |
2530 | |
2531 if (!notWrappedYet(function)) // qtd2 | |
2532 if (function->jumpTableId() == -1) | |
2533 writePrivateNativeFunction(s, function); | |
2534 } | |
2535 | |
2536 | |
2537 s << "// Just the private functions for abstract functions implemeneted in superclasses" << endl; | |
2538 // Just the private functions for abstract functions implemeneted in superclasses | |
2539 if (!d_class->isInterface() && d_class->isAbstract()) { | |
2540 d_funcs = d_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
2541 foreach (AbstractMetaFunction *d_function, d_funcs) { | |
2542 if (d_function->implementingClass() != d_class) { | |
2543 s << endl; | |
2544 writePrivateNativeFunction(s, d_function); | |
2545 } | |
2546 } | |
2547 } | |
2548 | |
2549 | |
2550 foreach (const AbstractMetaField *field, fields) { | |
2551 if (field->wasPublic() || (field->wasProtected() && !d_class->isFinal())) | |
2552 writeNativeField(s, field); | |
2553 } | |
2554 s << endl << endl; | |
2555 | |
2556 // qtd | |
2557 s << endl << "// Virtual Dispatch functions" << endl; | |
2558 AbstractMetaFunctionList virtualFunctions = d_class->virtualFunctions(); | |
2559 for (int pos = 0; pos<virtualFunctions.size(); ++pos) { | |
2560 const AbstractMetaFunction *function = virtualFunctions.at(pos); | |
2561 if (!notWrappedYet(function)) // qtd2 | |
2562 writeShellVirtualFunction(s, function, d_class, pos); | |
2563 } | |
2564 | |
253 | 2565 // write static constructor |
2566 if (staticInit) { | |
2567 QString initArgs; | |
279 | 2568 if (cpp_shared && d_class->generateShellClass()) |
253 | 2569 { |
2570 initArgs = "void* virtuals"; | |
1 | 2571 if (d_class->isQObject()) |
253 | 2572 initArgs += ", void* signals"; |
1 | 2573 |
2574 s << "private extern (C) void qtd_" << d_class->name() | |
253 | 2575 << QString("_initCallBacks(%1);").arg(initArgs) << endl << endl; |
2576 } | |
2577 | |
2578 s << "extern(C) void static_init_" << d_class->name() << "() {" << endl; | |
2579 | |
2580 if (d_class->isQObject()) { | |
2581 s << INDENT << "if (!" << d_class->name() << "._staticMetaObject) " << endl | |
2582 << INDENT << " " << d_class->name() << ".createStaticMetaObject;" << endl << endl; | |
2583 } | |
2584 | |
279 | 2585 if (cpp_shared && d_class->generateShellClass()) { |
253 | 2586 // virtual functions |
2587 s << INDENT << "void*[" << virtualFunctions.size() << "] virt_arr;" << endl; | |
1 | 2588 for (int pos = 0; pos<virtualFunctions.size(); ++pos) { |
2589 const AbstractMetaFunction *function = virtualFunctions.at(pos); | |
2590 if (!notWrappedYet(function)) // qtd2 | |
2591 s << INDENT << "virt_arr[" << pos << "] = &" << function->marshalledName() << "_dispatch;" <<endl; | |
2592 } | |
2593 if (virtualFunctions.size() == 0) | |
2594 initArgs = "null"; | |
2595 else | |
2596 initArgs = "virt_arr.ptr"; | |
2597 | |
2598 if (d_class->isQObject()) { | |
253 | 2599 |
2600 // signals | |
1 | 2601 AbstractMetaFunctionList signal_funcs = signalFunctions(d_class); |
2602 s << endl << INDENT << "void*[" << signal_funcs.size() << "] sign_arr;" << endl; | |
2603 for(int i = 0; i < signal_funcs.size(); i++) { | |
2604 AbstractMetaFunction *signal = signal_funcs.at(i); | |
218 | 2605 s << INDENT << "sign_arr[" << i << "] = &" << signalExternName(d_class, signal) << "_handle;" << endl; |
1 | 2606 } |
2607 if(signal_funcs.size() == 0) | |
2608 initArgs += ", null"; | |
2609 else | |
2610 initArgs += ", sign_arr.ptr"; | |
2611 } | |
2612 | |
253 | 2613 s << INDENT << "qtd_" << d_class->name() << QString("_initCallBacks(%1);").arg(initArgs) << endl; |
1 | 2614 } |
253 | 2615 |
2616 s << "}" << endl << endl; | |
1 | 2617 } |
2618 | |
2619 writeSignalHandlers(s, d_class); | |
2620 s << endl; | |
2621 | |
2622 if (m_docs_enabled) { | |
2623 delete m_doc_parser; | |
2624 m_doc_parser = 0; | |
2625 } | |
2626 | |
2627 // qtd multiple classes | |
2628 foreach (AbstractMetaClass *cls, includedClassesList) { | |
2629 m_isRecursive = true; | |
2630 write(s, cls); | |
2631 m_isRecursive = false; | |
2632 } | |
253 | 2633 |
2634 | |
2635 if (d_class->isQObject()) | |
2636 { | |
2637 s << "private extern(C) void* qtd_" << d_class->name() << "_staticMetaObject();" << endl << endl | |
2638 << "private extern(C) void qtd_" << d_class->name() << "_createEntity(void* nativeId, void* dId);" << endl << endl; | |
2639 } | |
1 | 2640 } |
2641 | |
110 | 2642 void DGenerator::writeConversionFunction(QTextStream &s, const AbstractMetaClass *d_class) |
2643 { | |
2644 const ComplexTypeEntry *ctype = d_class->typeEntry(); | |
253 | 2645 if(ctype->isQObject() || !ctype->isObject()) |
152 | 2646 return; |
110 | 2647 QString class_name = ctype->name(); |
111 | 2648 QString return_type_name = class_name; |
2649 if(ctype->designatedInterface()) | |
2650 return_type_name = ctype->designatedInterface()->name(); | |
253 | 2651 |
2652 s << return_type_name << " qtd_" << class_name << "_from_ptr(void* ret) {" << endl; | |
2653 | |
2654 | |
2655 QString type_name = class_name; | |
2656 if(ctype->isAbstract()) | |
2657 type_name = ctype->targetLangName() + "_ConcreteWrapper"; | |
2658 | |
2659 // if class has virtual functions then it has classname_entity function so | |
2660 // we can look for D Object pointer. otherwise create new wrapper | |
2661 if (d_class->hasVirtualFunctions()) { | |
2662 s << INDENT << "void* d_obj = __" << ctype->targetLangName() << "_entity(ret);" << endl | |
2663 << INDENT << "if (d_obj !is null) {" << endl | |
2664 << INDENT << " auto d_obj_ref = cast (Object) d_obj;" << endl | |
2665 << INDENT << " return cast(" << return_type_name << ") d_obj_ref;" << endl | |
2666 << INDENT << "} else {" << endl | |
2667 << INDENT << " auto return_value = new " << type_name << "(ret, QtdObjectFlags.nativeOwnership);" << endl | |
2668 << INDENT << " return return_value;" << endl | |
2669 << INDENT << "}" << endl; | |
2670 } else { | |
2671 s << INDENT << "auto return_value = new " << type_name << "(ret, QtdObjectFlags.nativeOwnership);" << endl | |
2672 << INDENT << "return return_value;" << endl; | |
111 | 2673 } |
2674 s << "}" << endl << endl; | |
110 | 2675 } |
2676 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2677 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
|
2678 { |
253 | 2679 QString concreteArg; |
2680 if (d_class->isAbstract()) | |
2681 concreteArg += ", " + d_class->name() + "_ConcreteWrapper"; | |
2682 | |
2683 s << " private static QMetaObject _staticMetaObject;" << endl | |
2684 << " protected static void createStaticMetaObject() {" << endl | |
2685 << " assert(!_staticMetaObject);" << endl | |
2686 << " QMetaObject base;" << endl; | |
2687 | |
2688 if (d_class->name() != "QObject") | |
2689 { | |
2690 QString baseName = d_class->baseClassName(); | |
2691 s << " if (!" << baseName << "._staticMetaObject)" << endl | |
2692 << " " << baseName << ".createStaticMetaObject;" << endl | |
2693 << " base = " << baseName << "._staticMetaObject;" << endl; | |
2694 } | |
2695 | |
2696 s << " _staticMetaObject = new QMetaObject(qtd_" << d_class->name() << "_staticMetaObject, base);" << endl | |
2697 << " _staticMetaObject.construct!(" << d_class->name() << concreteArg << ");" << endl | |
2698 << " }" << endl << endl | |
2699 | |
2700 << " QMetaObject metaObject() {" << endl | |
2701 << " return _staticMetaObject;" << endl | |
2702 << " }" << endl << endl | |
2703 | |
2704 << " static QMetaObject staticMetaObject() {" << endl | |
2705 << " return _staticMetaObject;" << endl | |
2706 << " }" << endl << endl | |
2707 | |
2708 << " static " << d_class->name() << " __getObject(void* nativeId) {" << endl | |
2709 << " return static_cast!(" << d_class->name() << ")(_staticMetaObject.getObject(nativeId));" << endl | |
2710 << " }" << endl << endl | |
2711 | |
2712 << " static void __createEntity(void* nativeId, void* dId) {" << endl | |
2713 << " return qtd_" << d_class->name() << "_createEntity(nativeId, dId);" << endl | |
2714 << " }" << endl << endl; | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2715 } |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2716 |
1 | 2717 /* |
2718 void DGenerator::writeMarshallFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
2719 { | |
2720 | |
2721 } | |
2722 */ | |
2723 void DGenerator::marshallFromCppToD(QTextStream &s, const ComplexTypeEntry* ctype) | |
2724 { | |
2725 if(ctype->isQObject()) { | |
2726 QString type_name = ctype->name(); | |
253 | 2727 if (ctype->isAbstract()) |
2728 type_name += "_ConcreteWrapper"; | |
2729 s << "return " << type_name << ".__getObject(ret);" << endl; | |
152 | 2730 } else if (ctype->isValue() && !ctype->isStructInD()) { |
253 | 2731 s << INDENT << "return new " << ctype->name() << "(ret);" << endl; |
152 | 2732 } else if (ctype->isVariant()) { |
253 | 2733 s << INDENT << "return new QVariant(ret);" << endl; |
152 | 2734 } else if (ctype->name() == "QModelIndex" || ctype->isStructInD()) { |
253 | 2735 s << INDENT << "return ret;" << endl; |
152 | 2736 } else if (ctype->isObject()) { |
1 | 2737 QString type_name = ctype->name(); |
253 | 2738 s << "return qtd_" << type_name << "_from_ptr(ret);" << endl; |
1 | 2739 } |
2740 } | |
2741 | |
2742 void DGenerator::writeNativeField(QTextStream &s, const AbstractMetaField *field) | |
2743 { | |
2744 Q_ASSERT(field->isPublic() || field->isProtected()); | |
2745 | |
2746 const AbstractMetaClass *declaringClass = field->enclosingClass(); | |
2747 | |
2748 FieldModification mod = declaringClass->typeEntry()->fieldModification(field->name()); | |
2749 | |
2750 // Set function | |
2751 if (mod.isWritable() && !field->type()->isConstant()) { | |
2752 const AbstractMetaFunction *setter = field->setter(); | |
2753 if (declaringClass->hasFunction(setter)) { | |
2754 QString warning = | |
2755 QString("class '%1' already has setter '%2' for public field '%3'") | |
2756 .arg(declaringClass->name()).arg(setter->name()).arg(field->name()); | |
2757 ReportHandler::warning(warning); | |
2758 } else { | |
2759 if (!notWrappedYet(setter)) | |
2760 writePrivateNativeFunction(s, setter); | |
2761 } | |
2762 } | |
2763 | |
2764 // Get function | |
2765 const AbstractMetaFunction *getter = field->getter(); | |
2766 if (mod.isReadable()) { | |
2767 if (declaringClass->hasFunction(getter)) { | |
2768 QString warning = | |
2769 QString("class '%1' already has getter '%2' for public field '%3'") | |
2770 .arg(declaringClass->name()).arg(getter->name()).arg(field->name()); | |
2771 ReportHandler::warning(warning); | |
2772 } else { | |
2773 if (!notWrappedYet(getter)) | |
2774 writePrivateNativeFunction(s, getter); | |
2775 } | |
2776 } | |
2777 } | |
2778 | |
253 | 2779 void DGenerator::writeSignalSignatures(QTextStream &s, const AbstractMetaClass *d_class, AbstractMetaFunctionList signal_funcs) |
1 | 2780 { |
253 | 2781 s << INDENT << "private const string[" << signal_funcs.size() << "] __signalSignatures = ["; |
1 | 2782 { |
2783 Indentation indent(INDENT); | |
253 | 2784 for (int i = 0; i < signal_funcs.size(); ++i) |
2785 { | |
2786 if (i) | |
2787 s << ", "; | |
2788 s << endl << INDENT << " \"" << signal_funcs.at(i)->minimalSignature() << "\""; | |
1 | 2789 } |
2790 } | |
253 | 2791 s << INDENT << "];" << endl << endl; |
2792 | |
2793 s << INDENT << "int signalSignature(int signalId, ref stringz signature) {" << endl; | |
1 | 2794 { |
2795 Indentation indent(INDENT); | |
253 | 2796 |
2797 if (d_class->name() != "QObject") | |
2798 { | |
2799 s << INDENT << "signalId = super.signalSignature(signalId, signature);" << endl | |
2800 << INDENT << "if (signature)" << endl | |
2801 << INDENT << " return signalId;" << endl; | |
2802 } | |
2803 | |
2804 s << INDENT << "if (signalId < __signalSignatures.length)" << endl | |
2805 << INDENT << " signature = __signalSignatures[signalId].ptr;" << endl | |
2806 << INDENT << "else" << endl | |
2807 << INDENT << " signalId -= __signalSignatures.length;" << endl | |
2808 << INDENT << "return signalId;" << endl; | |
1 | 2809 } |
2810 s << INDENT << "}" << endl; | |
2811 } | |
2812 | |
2813 void DGenerator::writeSignal(QTextStream &s, const AbstractMetaFunction *d_function) | |
2814 { | |
2815 Q_ASSERT(d_function->isSignal()); | |
2816 | |
2817 AbstractMetaArgumentList arguments = d_function->arguments(); | |
2818 int sz = arguments.count(); | |
2819 | |
284 | 2820 s << INDENT << "mixin BindQtSignal!(\"" << d_function->name() << "\""; |
1 | 2821 |
2822 if (sz > 0) { | |
2823 for (int i=0; i<sz; ++i) { | |
270 | 2824 s << ", "; |
1 | 2825 |
2826 QString modifiedType = d_function->typeReplaced(i+1); | |
2827 | |
2828 if (modifiedType.isEmpty()) | |
2829 s << translateType(arguments.at(i)->type(), d_function->implementingClass(), BoxedPrimitive); | |
2830 else | |
2831 s << modifiedType; | |
2832 } | |
2833 } | |
2834 | |
2835 s << ");" << endl; | |
2836 } | |
2837 | |
2838 void DGenerator::writeShellVirtualFunction(QTextStream &s, const AbstractMetaFunction *d_function, | |
2839 const AbstractMetaClass *implementor, int id) | |
2840 { | |
2841 Q_UNUSED(id); | |
2842 Q_UNUSED(implementor); | |
2843 | |
2844 s << "private extern(C) "; | |
2845 CppImplGenerator::writeVirtualDispatchFunction(s, d_function, true); | |
2846 s << "{" << endl; | |
2847 | |
2848 const AbstractMetaClass *own_class = d_function->ownerClass(); | |
2849 | |
2850 s << INDENT << "auto d_object = cast(" << own_class->name() << ") d_entity;" << endl; | |
2851 | |
2852 // the function arguments | |
2853 AbstractMetaArgumentList arguments = d_function->arguments(); | |
2854 foreach (const AbstractMetaArgument *argument, arguments) | |
2855 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
2856 QString arg_name = argument->indexedName(); | |
2857 AbstractMetaType *type = argument->type(); | |
2858 // if has QString argument we have to pass char* and str.length to QString constructor | |
2859 { | |
2860 if(type->isEnum()) | |
2861 s << INDENT << "auto " << arg_name << "_enum = cast(" | |
2862 << type->typeEntry()->qualifiedTargetLangName() << ") " << arg_name << ";"; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2863 else if (type->typeEntry()->qualifiedCppName() == "QChar") |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2864 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
|
2865 << ") " << arg_name << ";"; |
1 | 2866 else if (type->isTargetLangString()) |
188 | 2867 s << INDENT << "string " << arg_name << "_d_ref = toUTF8(" |
1 | 2868 << arg_name << "[0.." << arg_name << "_size]);"; |
2869 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
|
2870 s << INDENT << "auto " << arg_name << "_d_qstr = QString(" << arg_name << ", true);" << endl |
1 | 2871 << INDENT << "string " << arg_name << "_d_ref = " << arg_name << "_d_qstr.toNativeString();"; |
2872 } else if(type->isVariant()) | |
253 | 2873 s << INDENT << "scope " << arg_name << "_d_ref = new QVariant(" << arg_name << ", QtdObjectFlags.nativeOwnership);"; |
1 | 2874 else if (type->typeEntry()->isStructInD()) |
2875 continue; | |
2876 else if (!type->hasNativeId() && !(type->typeEntry()->isValue() && type->isNativePointer())) | |
2877 continue; | |
2878 else if(type->isObject() | |
2879 || (type->typeEntry()->isValue() && type->isNativePointer()) | |
2880 || type->isValue() || type->isVariant()) { | |
2881 QString type_name = type->typeEntry()->name(); | |
2882 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(type->typeEntry()); | |
2883 if(ctype->isAbstract()) | |
2884 type_name = type_name + "_ConcreteWrapper"; | |
253 | 2885 s << INDENT << "scope " << arg_name << "_d_ref = new " << type_name << "(" << arg_name << ", QtdObjectFlags.nativeOwnership);"; |
1 | 2886 } |
2887 else if (type->isQObject()) { | |
2888 QString type_name = type->name(); | |
2889 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(type->typeEntry()); | |
2890 if(ctype->isAbstract()) | |
2891 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
|
2892 |
253 | 2893 s << INDENT << "scope " << arg_name << "_d_ref = new " << type_name << "(" << arg_name << ", QtdObjectFlags.nativeOwnership);" << endl; |
1 | 2894 } |
2895 s << endl; | |
2896 } | |
2897 } | |
2898 | |
2899 s << INDENT; | |
2900 AbstractMetaType *return_type = d_function->type(); | |
2901 QString new_return_type = QString(d_function->typeReplaced(0)).replace('$', '.'); | |
2902 bool has_return_type = new_return_type != "void" | |
2903 && (!new_return_type.isEmpty() || return_type != 0); | |
2904 if(has_return_type) { | |
2905 AbstractMetaType *f_type = d_function->type(); | |
2906 if(f_type && (f_type->isObject() || f_type->isQObject() || f_type->isVariant() || | |
2907 (f_type->isValue() && !f_type->typeEntry()->isStructInD()))) | |
2908 { | |
2909 QString f_type_name = f_type->name(); | |
2910 if(f_type->typeEntry()->designatedInterface()) | |
2911 f_type_name = f_type->typeEntry()->designatedInterface()->name(); | |
2912 s << f_type_name << " ret_value = "; | |
2913 } | |
2914 else if (f_type && f_type->isTargetLangString()) | |
2915 s << "string _d_str = "; | |
81 | 2916 else if (f_type && (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD())) |
1 | 2917 s << "*__d_return_value = "; |
2918 else | |
2919 s << "auto return_value = "; | |
2920 } | |
2921 s << "d_object." << d_function->name() << "("; | |
2922 | |
2923 uint nativeArgCount = 0; | |
2924 foreach (const AbstractMetaArgument *argument, arguments) | |
2925 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
2926 QString arg_name = argument->indexedName(); | |
2927 const AbstractMetaType *type = argument->type(); | |
2928 | |
2929 if (nativeArgCount > 0) | |
2930 s << "," << " "; | |
2931 | |
2932 QString modified_type = d_function->typeReplaced(argument->argumentIndex() + 1); | |
2933 if (!modified_type.isEmpty()) | |
2934 modified_type = modified_type.replace('$', '.'); | |
2935 | |
2936 if (modified_type == "string" /* && type->fullName() == "char" */) | |
2937 s << "fromStringz(" << arg_name << ")"; | |
2938 else { | |
2939 if(type->isContainer() | |
2940 || (type->isReference() && type->typeEntry()->isStructInD())) | |
2941 s << "*"; | |
2942 s << arg_name; | |
2943 } | |
2944 if (type->typeEntry()->isStructInD()) ; | |
2945 else if (type->isQObject() || type->isObject() | |
2946 || (type->typeEntry()->isValue() && type->isNativePointer()) | |
2947 || type->isValue() | |
2948 || type->isTargetLangString() || type->isVariant()) | |
2949 s << "_d_ref"; | |
2950 else if(type->isEnum()) | |
2951 s << "_enum"; | |
2952 | |
2953 nativeArgCount++; | |
2954 } | |
2955 s << ");" << endl; | |
2956 | |
2957 // check for arguments that may return value | |
2958 foreach (const AbstractMetaArgument *argument, arguments) | |
2959 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
2960 QString arg_name = argument->indexedName(); | |
2961 AbstractMetaType *type = argument->type(); | |
2962 | |
2963 if (type->typeEntry()->isValue() && type->isNativePointer() && type->typeEntry()->name() == "QString") | |
2964 s << INDENT << arg_name << "_d_qstr.assign(" << arg_name << "_d_ref);" << endl; | |
2965 } | |
2966 | |
2967 if(has_return_type) { | |
2968 AbstractMetaType *f_type = d_function->type(); | |
2969 if(f_type) { | |
2970 if(f_type->isObject() || f_type->isQObject() || f_type->isVariant() || | |
2971 (f_type->isValue() && !f_type->typeEntry()->isStructInD())) { | |
253 | 2972 QString native_id = "__nativeId"; |
1 | 2973 if (f_type->typeEntry()->designatedInterface()) |
2974 native_id = "__ptr_" + f_type->typeEntry()->designatedInterface()->name(); | |
2975 s << INDENT << "return ret_value is null? null : ret_value." << native_id << ";" << endl; | |
2976 } else if (f_type->isTargetLangString()) | |
104 | 2977 s << INDENT << "*ret_str = _d_str;" << endl; |
1 | 2978 else if (f_type->isContainer()) |
2979 s << INDENT << "*__d_arr_ptr = return_value.ptr;" << endl | |
2980 << INDENT << "*__d_arr_size = return_value.length;" << endl; | |
81 | 2981 else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 2982 ; |
2983 else | |
2984 s << INDENT << "return return_value;" << endl; | |
2985 } else | |
2986 s << INDENT << "return return_value;" << endl; | |
2987 | |
2988 } | |
2989 | |
2990 s << "}" << endl << endl; | |
2991 } | |
2992 | |
2993 void DGenerator::generate() | |
2994 { | |
2995 // qtd | |
2996 // code for including classses in 1 module for avoiding circular imports | |
2997 foreach (AbstractMetaClass *cls, m_classes) { | |
2998 const ComplexTypeEntry *ctype = cls->typeEntry(); | |
2999 | |
3000 if (!cls->isInterface() && cls->isAbstract()) { | |
3001 ComplexTypeEntry *ctype_m = (ComplexTypeEntry *)ctype; | |
3002 ctype_m->setAbstract(true); | |
3003 } | |
3004 | |
3005 foreach(QString child, ctype->includedClasses) { | |
3006 ComplexTypeEntry *ctype_child = TypeDatabase::instance()->findComplexType(child); | |
3007 ctype_child->addedTo = cls->name(); | |
3008 } | |
3009 | |
3010 foreach (AbstractMetaFunction *function, cls->functions()) | |
3011 function->checkStoreResult(); | |
152 | 3012 /* we don't need this anymore |
110 | 3013 // generate QObject conversion functions only those that are required |
3014 AbstractMetaFunctionList d_funcs = cls->functionsInTargetLang(); | |
3015 for (int i=0; i<d_funcs.size(); ++i) { | |
3016 AbstractMetaType *f_type = d_funcs.at(i)->type(); | |
3017 if (!f_type) | |
3018 continue; | |
3019 if (f_type->isQObject() || f_type->isObject()) { | |
3020 const ComplexTypeEntry* cte = static_cast<const ComplexTypeEntry *>(f_type->typeEntry()); | |
3021 AbstractMetaClass* d_class = ClassFromEntry::get(cte); | |
3022 if (d_class) | |
3023 d_class->needsConversionFunc = true; | |
3024 } | |
152 | 3025 }*/ |
1 | 3026 } |
3027 | |
3028 Generator::generate(); | |
3029 | |
3030 { | |
3031 const AbstractMetaClass *last_class = 0; | |
3032 QFile file("mjb_nativepointer_api.log"); | |
3033 if (file.open(QFile::WriteOnly)) { | |
3034 QTextStream s(&file); | |
3035 | |
3036 AbstractMetaFunctionList nativepointer_functions; | |
3037 for (int i=0; i<m_nativepointer_functions.size(); ++i) { | |
3038 AbstractMetaFunction *f = const_cast<AbstractMetaFunction *>(m_nativepointer_functions[i]); | |
3039 if (f->ownerClass() == f->declaringClass() || f->isFinal()) | |
3040 nativepointer_functions.append(f); | |
3041 } | |
3042 | |
3043 s << "Number of public or protected functions with QNativePointer API: " << nativepointer_functions.size() << endl; | |
3044 foreach (const AbstractMetaFunction *f, nativepointer_functions) { | |
3045 if (last_class != f->ownerClass()) { | |
3046 last_class = f->ownerClass(); | |
3047 s << endl << endl<< "Class " << last_class->name() << ":" << endl; | |
3048 s << "---------------------------------------------------------------------------------" | |
3049 << endl; | |
3050 } | |
3051 | |
3052 s << f->minimalSignature() << endl; | |
3053 } | |
3054 | |
3055 m_nativepointer_functions.clear(); } | |
3056 } | |
3057 | |
3058 { | |
3059 const AbstractMetaClass *last_class = 0; | |
3060 QFile file("mjb_object_type_usage.log"); | |
3061 if (file.open(QFile::WriteOnly)) { | |
3062 QTextStream s(&file); | |
3063 | |
3064 AbstractMetaFunctionList resettable_object_functions; | |
3065 for (int i=0; i<m_resettable_object_functions.size(); ++i) { | |
3066 AbstractMetaFunction *f = const_cast<AbstractMetaFunction *>(m_resettable_object_functions[i]); | |
3067 if (f->ownerClass() == f->declaringClass() || f->isFinal()) | |
3068 resettable_object_functions.append(f); | |
3069 } | |
3070 | |
3071 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; | |
3072 foreach (const AbstractMetaFunction *f, resettable_object_functions) { | |
3073 if (last_class != f->ownerClass()) { | |
3074 last_class = f->ownerClass(); | |
3075 s << endl << endl<< "Class " << last_class->name() << ":" << endl; | |
3076 s << "---------------------------------------------------------------------------------" | |
3077 << endl; | |
3078 } | |
3079 | |
3080 s << f->minimalSignature() << endl; | |
3081 } | |
3082 | |
3083 m_resettable_object_functions.clear(); } | |
3084 } | |
3085 | |
3086 { | |
3087 QFile file("mjb_reference_count_candidates.log"); | |
3088 if (file.open(QFile::WriteOnly)) { | |
3089 QTextStream s(&file); | |
3090 | |
3091 s << "The following functions have a signature pattern which may imply that" << endl | |
3092 << "they need to apply reference counting to their arguments (" | |
3093 << m_reference_count_candidate_functions.size() << " functions) : " << endl; | |
3094 | |
3095 foreach (const AbstractMetaFunction *f, m_reference_count_candidate_functions) { | |
3096 s << f->implementingClass()->fullName() << " : " << f->minimalSignature() << endl; | |
3097 } | |
3098 } | |
3099 file.close(); | |
3100 } | |
3101 } | |
3102 | |
3103 void DGenerator::writeFunctionAttributes(QTextStream &s, const AbstractMetaFunction *d_function, | |
3104 uint included_attributes, uint excluded_attributes, | |
3105 uint options) | |
3106 { | |
3107 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
3108 | |
3109 if ((attr & AbstractMetaAttributes::Public) || (attr & AbstractMetaAttributes::Protected)) { | |
3110 | |
3111 // Does the function use native pointer API? | |
3112 bool nativePointer = d_function->type() && d_function->type()->isNativePointer() | |
3113 && d_function->typeReplaced(0).isEmpty(); | |
3114 | |
3115 // Does the function need to be considered for resetting the Java objects after use? | |
3116 bool resettableObject = false; | |
3117 | |
3118 if (!nativePointer | |
3119 && d_function->type() | |
3120 && d_function->type()->hasInstantiations() | |
3121 && d_function->typeReplaced(0).isEmpty()) { | |
3122 | |
3123 QList<AbstractMetaType *> instantiations = d_function->type()->instantiations(); | |
3124 | |
3125 foreach (const AbstractMetaType *type, instantiations) { | |
3126 if (type && type->isNativePointer()) { | |
3127 nativePointer = true; | |
3128 break; | |
3129 } | |
3130 } | |
3131 | |
3132 } | |
3133 | |
3134 AbstractMetaArgumentList arguments = d_function->arguments(); | |
3135 if (!nativePointer || (!resettableObject && !d_function->isFinal())) { | |
3136 foreach (const AbstractMetaArgument *argument, arguments) { | |
3137 if (!d_function->argumentRemoved(argument->argumentIndex()+1) | |
3138 && d_function->typeReplaced(argument->argumentIndex()+1).isEmpty()) { | |
3139 | |
3140 if (argument->type()->isNativePointer()) { | |
3141 | |
3142 nativePointer = true; | |
3143 if (resettableObject) break ; | |
3144 | |
3145 } else if (!d_function->isFinalInTargetLang() | |
3146 && argument->type()->isObject() | |
3147 && !argument->type()->isQObject() | |
3148 && !d_function->resetObjectAfterUse(argument->argumentIndex()+1) | |
3149 && d_function->ownership(d_function->declaringClass(), TypeSystem::ShellCode, argument->argumentIndex()+1) == TypeSystem::InvalidOwnership) { | |
3150 | |
3151 resettableObject = true; | |
3152 if (nativePointer) break ; | |
3153 | |
3154 } else if (argument->type()->hasInstantiations()) { | |
3155 | |
3156 QList<AbstractMetaType *> instantiations = argument->type()->instantiations(); | |
3157 foreach (AbstractMetaType *type, instantiations) { | |
3158 if (type && type->isNativePointer()) { | |
3159 nativePointer = true; | |
3160 if (resettableObject) break; | |
3161 } else if (!d_function->isFinal() | |
3162 && type | |
3163 && type->isObject() | |
3164 && !type->isQObject() | |
3165 && !d_function->resetObjectAfterUse(argument->argumentIndex()+1)) { | |
3166 resettableObject = true; | |
3167 if (nativePointer) break ; | |
3168 } | |
3169 } | |
3170 | |
3171 if (nativePointer && resettableObject) | |
3172 break; | |
3173 | |
3174 } | |
3175 } | |
3176 } | |
3177 } | |
3178 | |
3179 if (nativePointer && !m_nativepointer_functions.contains(d_function)) | |
3180 m_nativepointer_functions.append(d_function); | |
3181 if (resettableObject && !m_resettable_object_functions.contains(d_function)) | |
3182 m_resettable_object_functions.append(d_function); | |
3183 } | |
3184 | |
3185 if ((options & SkipAttributes) == 0) { | |
3186 if (d_function->isEmptyFunction() | |
3187 || d_function->isDeprecated()) s << INDENT << "deprecated "; | |
3188 /* | |
3189 bool needsSuppressUnusedWarning = TypeDatabase::instance()->includeEclipseWarnings() | |
3190 && d_function->isSignal() | |
3191 && (((excluded_attributes & AbstractMetaAttributes::Private) == 0) | |
3192 && (d_function->isPrivate() | |
3193 || ((included_attributes & AbstractMetaAttributes::Private) != 0))); | |
3194 | |
3195 if (needsSuppressUnusedWarning && d_function->needsSuppressUncheckedWarning()) { | |
3196 s << INDENT<< "@SuppressWarnings({\"unchecked\", \"unused\"})" << endl; | |
3197 } else if (d_function->needsSuppressUncheckedWarning()) { | |
3198 s << INDENT<< "@SuppressWarnings(\"unchecked\")" << endl; | |
3199 } else if (needsSuppressUnusedWarning) { | |
3200 s << INDENT<< "@SuppressWarnings(\"unused\")" << endl; | |
3201 } | |
3202 | |
3203 if (!(attr & NoBlockedSlot) | |
3204 && !d_function->isConstructor() | |
3205 && !d_function->isSlot() | |
3206 && !d_function->isSignal() | |
3207 && !d_function->isStatic() | |
3208 && !(included_attributes & AbstractMetaAttributes::Static)) | |
3209 s << INDENT << "@QtBlockedSlot" << endl; | |
3210 */ | |
3211 if (!(options & ExternC)) | |
3212 s << INDENT; | |
3213 | |
3214 if (attr & AbstractMetaAttributes::Public) s << "public "; | |
3215 else if (attr & AbstractMetaAttributes::Protected) s << "protected "; | |
3216 else if (attr & AbstractMetaAttributes::Private) s << "private "; | |
3217 else if (attr & AbstractMetaAttributes::Native) s << "private extern(C) "; | |
3218 bool isStatic = (attr & AbstractMetaAttributes::Static); | |
3219 | |
3220 if (attr & AbstractMetaAttributes::Native) ; | |
3221 else if (!isStatic && (attr & AbstractMetaAttributes::FinalInTargetLang)) s << "final "; | |
3222 else if (!isStatic && (attr & AbstractMetaAttributes::Abstract)) s << "abstract "; | |
3223 | |
3224 if (isStatic && !(options & ExternC)) s << "static "; | |
3225 } | |
3226 | |
3227 if ((options & SkipReturnType) == 0) { | |
3228 QString modified_type = d_function->typeReplaced(0); | |
3229 if (options & ExternC) { | |
3230 uint options = 0x0004; // qtd externC | |
3231 s << CppImplGenerator::jniReturnName(d_function, options, true) << " "; | |
3232 } | |
3233 else if (modified_type.isEmpty()) | |
3234 s << translateType(d_function->type(), d_function->implementingClass(), (Option) options); | |
3235 else | |
3236 s << modified_type.replace('$', '.'); | |
3237 s << " "; | |
3238 } | |
3239 | |
3240 } | |
3241 | |
3242 void DGenerator::writeConstructorContents(QTextStream &s, const AbstractMetaFunction *d_function) | |
3243 { | |
3244 // Write constructor | |
3245 s << " {" << endl; | |
3246 { | |
3247 Indentation indent(INDENT); | |
3248 bool shellClass = d_function->ownerClass()->generateShellClass(); | |
223 | 3249 |
1 | 3250 writeJavaCallThroughContents(s, d_function); |
3251 | |
3252 // Write out expense checks if present... | |
3253 const AbstractMetaClass *d_class = d_function->implementingClass(); | |
3254 const ComplexTypeEntry *te = d_class->typeEntry(); | |
3255 if (te->expensePolicy().isValid()) { | |
3256 s << endl; | |
3257 const ExpensePolicy &ep = te->expensePolicy(); | |
3258 s << INDENT << "qt.GeneratorUtilities.countExpense(" << d_class->fullName() | |
3259 << ".class, " << ep.cost << ", " << ep.limit << ");" << endl; | |
3260 } | |
3261 | |
3262 foreach (CodeSnip snip, te->codeSnips()) { | |
3263 if (snip.language == TypeSystem::Constructors) { | |
3264 snip.formattedCode(s, INDENT); | |
3265 } | |
3266 } | |
3267 | |
253 | 3268 s << INDENT << "this(ret);" << endl; |
1 | 3269 } |
3270 s << INDENT << "}" << endl << endl; | |
3271 | |
3272 /* qtd // Write native constructor | |
3273 if (d_function->jumpTableId() == -1) | |
3274 writePrivateNativeFunction(s, d_function); | |
3275 */ | |
3276 } | |
3277 | |
3278 void DGenerator::writeFunctionArguments(QTextStream &s, const AbstractMetaFunction *d_function, | |
3279 int argument_count, uint options) | |
3280 { | |
3281 AbstractMetaArgumentList arguments = d_function->arguments(); | |
3282 | |
3283 if (argument_count == -1) | |
3284 argument_count = arguments.size(); | |
3285 | |
3286 for (int i=0; i<argument_count; ++i) { | |
3287 if (!d_function->argumentRemoved(i+1)) { | |
3288 if (i != 0) | |
3289 s << ", "; | |
3290 writeArgument(s, d_function, arguments.at(i), options); | |
3291 } | |
3292 } | |
3293 } | |
3294 | |
3295 | |
3296 void DGenerator::writeExtraFunctions(QTextStream &s, const AbstractMetaClass *d_class) | |
3297 { | |
3298 const ComplexTypeEntry *class_type = d_class->typeEntry(); | |
3299 Q_ASSERT(class_type); | |
3300 | |
3301 CodeSnipList code_snips = class_type->codeSnips(); | |
3302 foreach (const CodeSnip &snip, code_snips) { | |
3303 if ((!d_class->isInterface() && snip.language == TypeSystem::TargetLangCode) | |
3304 || (d_class->isInterface() && snip.language == TypeSystem::Interface)) { | |
3305 s << endl; | |
3306 snip.formattedCode(s, INDENT); | |
3307 } | |
3308 } | |
3309 } | |
3310 | |
3311 | |
3312 void DGenerator::writeToStringFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
3313 { | |
3314 bool generate = d_class->hasToStringCapability() && !d_class->hasDefaultToStringFunction(); | |
3315 bool core = d_class->package() == QLatin1String("qt.core"); | |
3316 bool qevent = false; | |
3317 | |
3318 const AbstractMetaClass *cls = d_class; | |
3319 while (cls) { | |
3320 if (cls->name() == "QEvent") { | |
3321 qevent = true; | |
3322 break; | |
3323 } | |
3324 cls = cls->baseClass(); | |
3325 } | |
3326 | |
3327 if (generate || qevent) { | |
3328 | |
3329 if (qevent && core) { | |
3330 s << endl | |
3331 << " @Override" << endl | |
3332 << " public String toString() {" << endl | |
3333 << " return getClass().getSimpleName() + \"(type=\" + type().name() + \")\";" << endl | |
3334 << " }" << endl; | |
3335 } else { | |
3336 s << endl | |
3337 << " @Override" << endl | |
3338 << " public String toString() {" << endl | |
253 | 3339 << " if (__nativeId == 0)" << endl |
1 | 3340 << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl |
3341 << " return __qt_toString(nativeId());" << endl | |
3342 << " }" << endl | |
3343 << " native String __qt_toString(long __this_nativeId);" << endl; | |
3344 } | |
3345 } | |
3346 } | |
3347 | |
3348 void DGenerator::writeCloneFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
3349 { | |
3350 s << endl | |
3351 << " @Override" << endl | |
3352 << " public " << d_class->name() << " clone() {" << endl | |
3353 << " if (nativeId() == 0)" << endl | |
3354 << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl | |
3355 << " return __qt_clone(nativeId());" << endl | |
3356 << " }" << endl | |
3357 << " native " << d_class->name() << " __qt_clone(long __this_nativeId);" << endl; | |
3358 } | |
3359 | |
3360 ClassFromEntry* ClassFromEntry::m_instance = NULL; | |
3361 | |
3362 ClassFromEntry::ClassFromEntry() | |
3363 { | |
110 | 3364 } |
3365 | |
3366 AbstractMetaClass* ClassFromEntry::get(const TypeEntry *ctype) | |
3367 { | |
3368 if(!m_instance) | |
3369 return NULL; | |
3370 | |
3371 return m_instance->classFromEntry[ctype]; | |
3372 } | |
3373 | |
3374 void ClassFromEntry::construct(const AbstractMetaClassList &classes) | |
3375 { | |
3376 if(!m_instance) { | |
3377 m_instance = new ClassFromEntry; | |
3378 m_instance->setClasses(classes); | |
3379 m_instance->buildHash(); | |
3380 } | |
3381 } | |
3382 | |
3383 void ClassFromEntry::buildHash() | |
3384 { | |
1 | 3385 foreach (AbstractMetaClass *cls, m_classes) { |
3386 const ComplexTypeEntry *ctype = cls->typeEntry(); | |
3387 classFromEntry[ctype] = cls; | |
3388 } | |
3389 } | |
3390 | |
110 | 3391 void ClassFromEntry::print(QTextStream &s) |
1 | 3392 { |
110 | 3393 s << "_fuck_" << m_instance->m_classes.size(); |
3394 foreach (AbstractMetaClass *cls, m_instance->m_classes) { | |
3395 s << cls->name() << endl; | |
3396 } | |
1 | 3397 } |
110 | 3398 |