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