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