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