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