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