Mercurial > projects > qtd
annotate generator/abstractmetabuilder.cpp @ 308:2485419c5c63 signals
merge with 299
author | eldar_ins@eldar-laptop |
---|---|
date | Wed, 23 Dec 2009 04:45:55 +0500 |
parents | ae34188ddd84 |
children | 17b5e13364b7 18bd68f586c6 |
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 "abstractmetabuilder.h" | |
43 #include "reporthandler.h" | |
44 | |
45 #include "ast.h" | |
46 #include "binder.h" | |
47 #include "control.h" | |
48 #include "default_visitor.h" | |
49 #include "dumptree.h" | |
50 #include "lexer.h" | |
51 #include "parser.h" | |
52 #include "tokens.h" | |
53 | |
54 #include <QtCore/QDebug> | |
55 #include <QtCore/QFile> | |
56 #include <QtCore/QFileInfo> | |
57 #include <QtCore/QTextCodec> | |
58 #include <QtCore/QTextStream> | |
59 #include <QtCore/QVariant> | |
60 | |
61 static QString strip_template_args(const QString &name) | |
62 { | |
63 int pos = name.indexOf('<'); | |
64 return pos < 0 ? name : name.left(pos); | |
65 } | |
66 | |
67 static QHash<QString, QString> *operator_names; | |
68 QString rename_operator(const QString &oper) | |
69 { | |
70 QString op = oper.trimmed(); | |
71 if (!operator_names) { | |
72 operator_names = new QHash<QString, QString>; | |
73 | |
74 operator_names->insert("+", "add"); | |
75 operator_names->insert("-", "subtract"); | |
76 operator_names->insert("*", "multiply"); | |
77 operator_names->insert("/", "divide"); | |
78 operator_names->insert("%", "modulo"); | |
79 operator_names->insert("&", "and"); | |
80 operator_names->insert("|", "or"); | |
81 operator_names->insert("^", "xor"); | |
82 operator_names->insert("~", "negate"); | |
83 operator_names->insert("<<", "shift_left"); | |
84 operator_names->insert(">>", "shift_right"); | |
85 | |
86 // assigments | |
87 operator_names->insert("=", "assign"); | |
88 operator_names->insert("+=", "add_assign"); | |
89 operator_names->insert("-=", "subtract_assign"); | |
90 operator_names->insert("*=", "multiply_assign"); | |
91 operator_names->insert("/=", "divide_assign"); | |
92 operator_names->insert("%=", "modulo_assign"); | |
93 operator_names->insert("&=", "and_assign"); | |
94 operator_names->insert("|=", "or_assign"); | |
95 operator_names->insert("^=", "xor_assign"); | |
96 operator_names->insert("<<=", "shift_left_assign"); | |
97 operator_names->insert(">>=", "shift_right_assign"); | |
98 | |
99 // Logical | |
100 operator_names->insert("&&", "logical_and"); | |
101 operator_names->insert("||", "logical_or"); | |
102 operator_names->insert("!", "not"); | |
103 | |
104 // incr/decr | |
105 operator_names->insert("++", "increment"); | |
106 operator_names->insert("--", "decrement"); | |
107 | |
108 // compare | |
109 operator_names->insert("<", "less"); | |
110 operator_names->insert(">", "greater"); | |
111 operator_names->insert("<=", "less_or_equal"); | |
112 operator_names->insert(">=", "greater_or_equal"); | |
113 operator_names->insert("!=", "not_equal"); | |
114 operator_names->insert("==", "equal"); | |
115 | |
116 // other | |
117 operator_names->insert("[]", "subscript"); | |
118 operator_names->insert("->", "pointer"); | |
119 } | |
120 | |
121 if (!operator_names->contains(op)) { | |
122 TypeDatabase *tb = TypeDatabase::instance(); | |
123 | |
124 TypeParser::Info typeInfo = TypeParser::parse(op); | |
125 QString cast_to_name = typeInfo.qualified_name.join("::"); | |
126 TypeEntry *te = tb->findType(cast_to_name); | |
127 if ((te && te->codeGeneration() == TypeEntry::GenerateNothing) | |
128 || tb->isClassRejected(cast_to_name)) { | |
129 return QString(); | |
130 } else if (te) { | |
131 return "operator_cast_" + typeInfo.qualified_name.join("_"); | |
132 } else { | |
133 ReportHandler::warning(QString("unknown operator '%1'").arg(op)); | |
134 return "operator " + op; | |
135 } | |
136 } | |
137 | |
138 return "operator_" + operator_names->value(op); | |
139 } | |
140 | |
141 AbstractMetaBuilder::AbstractMetaBuilder() | |
142 : m_current_class(0) | |
143 { | |
144 } | |
145 | |
146 void AbstractMetaBuilder::checkFunctionModifications() | |
147 { | |
148 TypeDatabase *types = TypeDatabase::instance(); | |
149 SingleTypeEntryHash entryHash = types->entries(); | |
150 QList<TypeEntry *> entries = entryHash.values(); | |
151 foreach (TypeEntry *entry, entries) { | |
152 if (entry == 0) | |
153 continue; | |
154 if (!entry->isComplex() || entry->codeGeneration() == TypeEntry::GenerateNothing) | |
155 continue; | |
156 | |
157 ComplexTypeEntry *centry = static_cast<ComplexTypeEntry *>(entry); | |
158 FunctionModificationList modifications = centry->functionModifications(); | |
159 | |
160 foreach (FunctionModification modification, modifications) { | |
161 QString signature = modification.signature; | |
162 | |
163 QString name = signature.trimmed(); | |
164 name = name.mid(0, signature.indexOf("(")); | |
165 | |
166 AbstractMetaClass *clazz = m_meta_classes.findClass(centry->qualifiedCppName()); | |
167 if (clazz == 0) | |
168 continue; | |
169 | |
170 AbstractMetaFunctionList functions = clazz->functions(); | |
171 bool found = false; | |
172 QStringList possibleSignatures; | |
173 foreach (AbstractMetaFunction *function, functions) { | |
174 if (function->minimalSignature() == signature && function->implementingClass() == clazz) { | |
175 found = true; | |
176 break; | |
177 } | |
178 | |
179 if (function->originalName() == name) | |
180 possibleSignatures.append(function->minimalSignature() + " in " + function->implementingClass()->name()); | |
181 } | |
182 | |
183 if (!found) { | |
184 QString warning | |
185 = QString("signature '%1' for function modification in '%2' not found. Possible candidates: %3") | |
186 .arg(signature) | |
187 .arg(clazz->qualifiedCppName()) | |
188 .arg(possibleSignatures.join(", ")); | |
189 | |
190 ReportHandler::warning(warning); | |
191 } | |
192 } | |
193 } | |
194 } | |
195 | |
196 AbstractMetaClass *AbstractMetaBuilder::argumentToClass(ArgumentModelItem argument) | |
197 { | |
198 AbstractMetaClass *returned = 0; | |
199 bool ok = false; | |
200 AbstractMetaType *type = translateType(argument->type(), &ok); | |
201 if (ok && type != 0 && type->typeEntry() != 0 && type->typeEntry()->isComplex()) { | |
202 const TypeEntry *entry = type->typeEntry(); | |
203 returned = m_meta_classes.findClass(entry->name()); | |
204 } | |
205 delete type; | |
206 return returned; | |
207 } | |
208 | |
209 /** | |
210 * Checks the argument of a hash function and flags the type if it is a complex type | |
211 */ | |
212 void AbstractMetaBuilder::registerHashFunction(FunctionModelItem function_item) | |
213 { | |
214 ArgumentList arguments = function_item->arguments(); | |
215 if (arguments.size() == 1) { | |
216 if (AbstractMetaClass *cls = argumentToClass(arguments.at(0))) | |
217 cls->setHasHashFunction(true); | |
218 } | |
219 } | |
220 | |
221 /** | |
222 * Check if a class has a debug stream operator that can be used as toString | |
223 */ | |
224 | |
225 void AbstractMetaBuilder::registerToStringCapability(FunctionModelItem function_item) | |
226 { | |
227 ArgumentList arguments = function_item->arguments(); | |
228 if (arguments.size() == 2) { | |
229 if (arguments.at(0)->type().toString() == "QDebug"){ | |
230 ArgumentModelItem arg = arguments.at(1); | |
231 if (AbstractMetaClass *cls = argumentToClass(arg)) { | |
232 if (arg->type().indirections() < 2) { | |
233 cls->setToStringCapability(function_item); | |
234 } | |
235 } | |
236 } | |
237 } | |
238 } | |
239 | |
240 void AbstractMetaBuilder::traverseCompareOperator(FunctionModelItem item) { | |
241 ArgumentList arguments = item->arguments(); | |
242 if (arguments.size() == 2 && item->accessPolicy() == CodeModel::Public) { | |
243 AbstractMetaClass *comparer_class = argumentToClass(arguments.at(0)); | |
244 AbstractMetaClass *compared_class = argumentToClass(arguments.at(1)); | |
245 if (comparer_class != 0 && compared_class != 0) { | |
246 AbstractMetaClass *old_current_class = m_current_class; | |
247 m_current_class = comparer_class; | |
248 | |
249 AbstractMetaFunction *meta_function = traverseFunction(item); | |
250 if (meta_function != 0 && !meta_function->isInvalid()) { | |
251 // Strip away first argument, since that is the containing object | |
252 AbstractMetaArgumentList arguments = meta_function->arguments(); | |
253 arguments.pop_front(); | |
254 meta_function->setArguments(arguments); | |
255 | |
256 meta_function->setFunctionType(AbstractMetaFunction::GlobalScopeFunction); | |
257 | |
258 meta_function->setOriginalAttributes(meta_function->attributes()); | |
259 setupFunctionDefaults(meta_function, comparer_class); | |
260 | |
261 comparer_class->addFunction(meta_function); | |
262 } else if (meta_function != 0) { | |
263 delete meta_function; | |
264 } | |
265 | |
266 m_current_class = old_current_class; | |
267 } | |
268 } | |
269 } | |
270 | |
271 void AbstractMetaBuilder::traverseStreamOperator(FunctionModelItem item) | |
272 { | |
273 ArgumentList arguments = item->arguments(); | |
274 if (arguments.size() == 2 && item->accessPolicy() == CodeModel::Public) { | |
275 AbstractMetaClass *streamClass = argumentToClass(arguments.at(0)); | |
276 AbstractMetaClass *streamedClass = argumentToClass(arguments.at(1)); | |
277 | |
278 if (streamClass != 0 && streamedClass != 0 | |
279 && (streamClass->name() == "QDataStream" || streamClass->name() == "QTextStream")) { | |
280 AbstractMetaClass *old_current_class = m_current_class; | |
281 m_current_class = streamedClass; | |
282 AbstractMetaFunction *streamFunction = traverseFunction(item); | |
283 | |
284 if (streamFunction != 0 && !streamFunction->isInvalid()) { | |
285 QString name = item->name(); | |
286 streamFunction->setFunctionType(AbstractMetaFunction::GlobalScopeFunction); | |
287 | |
288 if (name.endsWith("<<")) | |
289 streamFunction->setName("writeTo"); | |
290 else | |
291 streamFunction->setName("readFrom"); | |
292 | |
293 // Strip away last argument, since that is the containing object | |
294 AbstractMetaArgumentList arguments = streamFunction->arguments(); | |
295 arguments.pop_back(); | |
296 streamFunction->setArguments(arguments); | |
297 | |
298 *streamFunction += AbstractMetaAttributes::Final; | |
299 *streamFunction += AbstractMetaAttributes::Public; | |
300 streamFunction->setOriginalAttributes(streamFunction->attributes()); | |
301 | |
302 streamFunction->setType(0); | |
303 | |
304 setupFunctionDefaults(streamFunction, streamedClass); | |
305 | |
306 streamedClass->addFunction(streamFunction); | |
307 streamedClass->typeEntry()->addExtraInclude(streamClass->typeEntry()->include()); | |
308 | |
309 m_current_class = old_current_class; | |
310 } | |
311 } | |
312 } | |
313 } | |
314 | |
315 void AbstractMetaBuilder::fixQObjectForScope(TypeDatabase *types, | |
316 NamespaceModelItem scope) | |
317 { | |
318 foreach (ClassModelItem item, scope->classes()) { | |
319 QString qualified_name = item->qualifiedName().join("::"); | |
320 TypeEntry *entry = types->findType(qualified_name); | |
321 if (entry) { | |
322 if (isQObject(qualified_name) && entry->isComplex()) { | |
323 ((ComplexTypeEntry *) entry)->setQObject(true); | |
324 } | |
325 } | |
326 } | |
327 | |
328 foreach (NamespaceModelItem item, scope->namespaceMap().values()) { | |
329 if (scope != item) | |
330 fixQObjectForScope(types, item); | |
331 } | |
332 } | |
333 | |
334 static bool class_less_than(AbstractMetaClass *a, AbstractMetaClass *b) | |
335 { | |
336 return a->name() < b->name(); | |
337 } | |
338 | |
339 | |
340 void AbstractMetaBuilder::sortLists() | |
341 { | |
342 qSort(m_meta_classes.begin(), m_meta_classes.end(), class_less_than); | |
343 foreach (AbstractMetaClass *cls, m_meta_classes) { | |
344 cls->sortFunctions(); | |
345 } | |
346 } | |
347 | |
348 bool AbstractMetaBuilder::build() | |
349 { | |
350 Q_ASSERT(!m_file_name.isEmpty()); | |
351 ReportHandler::setContext("Parser"); | |
352 | |
353 QFile file(m_file_name); | |
354 | |
355 if (!file.open(QFile::ReadOnly)) | |
356 return false; | |
357 | |
358 QTextStream stream(&file); | |
359 stream.setCodec(QTextCodec::codecForName("UTF-8")); | |
360 QByteArray contents = stream.readAll().toUtf8(); | |
361 file.close(); | |
362 | |
363 Control control; | |
364 Parser p(&control); | |
365 pool __pool; | |
366 | |
367 TranslationUnitAST *ast = p.parse(contents, contents.size(), &__pool); | |
368 | |
369 CodeModel model; | |
370 Binder binder(&model, p.location()); | |
371 m_dom = binder.run(ast); | |
372 | |
373 | |
374 ReportHandler::setContext("MetaJavaBuilder"); | |
375 | |
376 | |
377 pushScope(model_dynamic_cast<ScopeModelItem>(m_dom)); | |
378 | |
379 QHash<QString, ClassModelItem> typeMap = m_dom->classMap(); | |
380 | |
381 | |
382 // fix up QObject's in the type system.. | |
383 TypeDatabase *types = TypeDatabase::instance(); | |
384 fixQObjectForScope(types, model_dynamic_cast<NamespaceModelItem>(m_dom)); | |
385 | |
386 | |
387 // Start the generation... | |
388 foreach (ClassModelItem item, typeMap.values()) { | |
389 AbstractMetaClass *cls = traverseClass(item); | |
390 addAbstractMetaClass(cls); | |
391 } | |
392 | |
393 | |
394 QHash<QString, NamespaceModelItem> namespaceMap = m_dom->namespaceMap(); | |
395 foreach (NamespaceModelItem item, namespaceMap.values()) { | |
396 AbstractMetaClass *meta_class = traverseNamespace(item); | |
397 if (meta_class) | |
398 m_meta_classes << meta_class; | |
399 } | |
400 | |
401 | |
402 // Some trickery to support global-namespace enums... | |
403 QHash<QString, EnumModelItem> enumMap = m_dom->enumMap(); | |
404 m_current_class = 0; | |
405 foreach (EnumModelItem item, enumMap) { | |
406 AbstractMetaEnum *meta_enum = traverseEnum(item, 0, QSet<QString>()); | |
407 | |
408 if (meta_enum) { | |
409 QString package = meta_enum->typeEntry()->javaPackage(); | |
410 QString globalName = TypeDatabase::globalNamespaceClassName(meta_enum->typeEntry()); | |
411 | |
412 AbstractMetaClass *global = m_meta_classes.findClass(package + "." + globalName); | |
413 if (!global) { | |
414 ComplexTypeEntry *gte = new ObjectTypeEntry(globalName); | |
415 gte->setTargetLangPackage(meta_enum->typeEntry()->javaPackage()); | |
416 gte->setCodeGeneration(meta_enum->typeEntry()->codeGeneration()); | |
417 global = createMetaClass(); | |
418 global->setTypeEntry(gte); | |
419 *global += AbstractMetaAttributes::Final; | |
420 *global += AbstractMetaAttributes::Public; | |
421 *global += AbstractMetaAttributes::Fake; | |
422 | |
423 m_meta_classes << global; | |
424 } | |
425 | |
426 global->addEnum(meta_enum); | |
427 meta_enum->setEnclosingClass(global); | |
428 meta_enum->typeEntry()->setQualifier(globalName); | |
429 } | |
430 | |
431 | |
432 } | |
433 | |
434 | |
435 // Go through all typedefs to see if we have defined any | |
436 // specific typedefs to be used as classes. | |
437 TypeAliasList typeAliases = m_dom->typeAliases(); | |
438 foreach (TypeAliasModelItem typeAlias, typeAliases) { | |
439 AbstractMetaClass *cls = traverseTypeAlias(typeAlias); | |
440 addAbstractMetaClass(cls); | |
441 } | |
442 | |
443 | |
444 | |
445 | |
446 foreach (AbstractMetaClass *cls, m_meta_classes) { | |
447 if (!cls->isInterface() && !cls->isNamespace()) { | |
448 setupInheritance(cls); | |
449 } | |
450 } | |
451 | |
452 | |
453 foreach (AbstractMetaClass *cls, m_meta_classes) { | |
454 cls->fixFunctions(); | |
455 | |
456 if (cls->typeEntry() == 0) { | |
457 ReportHandler::warning(QString("class '%1' does not have an entry in the type system") | |
458 .arg(cls->name())); | |
459 } else { | |
460 if (!cls->hasConstructors() && !cls->isFinalInCpp() && !cls->isInterface() && !cls->isNamespace()) | |
461 cls->addDefaultConstructor(); | |
462 } | |
463 | |
464 if (cls->isAbstract() && !cls->isInterface()) { | |
465 cls->typeEntry()->setLookupName(cls->typeEntry()->targetLangName() + "_ConcreteWrapper"); | |
466 } | |
467 } | |
468 | |
469 QList<TypeEntry *> entries = TypeDatabase::instance()->entries().values(); | |
470 foreach (const TypeEntry *entry, entries) { | |
471 if (entry->isPrimitive()) | |
472 continue; | |
473 | |
474 if ((entry->isValue() || entry->isObject()) | |
475 && !entry->isString() | |
476 && !entry->isChar() | |
477 && !entry->isContainer() | |
478 && !entry->isCustom() | |
479 && !entry->isVariant() | |
480 && !m_meta_classes.findClass(entry->qualifiedCppName())) { | |
481 ReportHandler::warning(QString("type '%1' is specified in typesystem, but not defined. This could potentially lead to compilation errors.") | |
482 .arg(entry->qualifiedCppName())); | |
483 } | |
484 | |
485 if (entry->isEnum()) { | |
486 QString pkg = entry->javaPackage(); | |
487 QString name = (pkg.isEmpty() ? QString() : pkg + ".") | |
488 + ((EnumTypeEntry *) entry)->javaQualifier(); | |
489 AbstractMetaClass *cls = m_meta_classes.findClass(name); | |
490 | |
491 if (!cls) { | |
492 ReportHandler::warning(QString("namespace '%1' for enum '%2' is not declared") | |
493 .arg(name).arg(entry->targetLangName())); | |
494 } else { | |
495 AbstractMetaEnum *e = cls->findEnum(entry->targetLangName()); | |
496 if (!e) | |
497 ReportHandler::warning(QString("enum '%1' is specified in typesystem, " | |
498 "but not declared") | |
499 .arg(entry->qualifiedCppName())); | |
500 } | |
501 } | |
502 } | |
503 | |
504 { | |
505 FunctionList hash_functions = m_dom->findFunctions("qHash"); | |
506 foreach (FunctionModelItem item, hash_functions) { | |
507 registerHashFunction(item); | |
508 } | |
509 } | |
510 | |
511 { | |
512 FunctionList hash_functions = m_dom->findFunctions("operator<<"); | |
513 foreach (FunctionModelItem item, hash_functions) { | |
514 registerToStringCapability(item); | |
515 } | |
516 } | |
517 | |
518 { | |
519 FunctionList compare_operators = m_dom->findFunctions("operator==") | |
520 + m_dom->findFunctions("operator<=") | |
521 + m_dom->findFunctions("operator>=") | |
522 + m_dom->findFunctions("operator<") | |
523 + m_dom->findFunctions("operator>"); | |
524 foreach (FunctionModelItem item, compare_operators) { | |
525 traverseCompareOperator(item); | |
526 } | |
527 } | |
528 | |
529 { | |
530 FunctionList stream_operators = m_dom->findFunctions("operator<<") + m_dom->findFunctions("operator>>"); | |
531 foreach (FunctionModelItem item, stream_operators) { | |
532 traverseStreamOperator(item); | |
533 } | |
534 } | |
535 | |
536 figureOutEnumValues(); | |
537 figureOutDefaultEnumArguments(); | |
538 checkFunctionModifications(); | |
539 | |
540 foreach (AbstractMetaClass *cls, m_meta_classes) { | |
541 setupEquals(cls); | |
542 setupComparable(cls); | |
543 setupClonable(cls); | |
544 } | |
545 | |
546 dumpLog(); | |
547 | |
548 sortLists(); | |
549 | |
550 return true; | |
551 } | |
552 | |
553 | |
554 void AbstractMetaBuilder::addAbstractMetaClass(AbstractMetaClass *cls) | |
555 { | |
556 if (!cls) | |
557 return; | |
558 | |
559 cls->setOriginalAttributes(cls->attributes()); | |
560 if (cls->typeEntry()->isContainer()) { | |
561 m_templates << cls; | |
562 } else { | |
563 m_meta_classes << cls; | |
564 if (cls->typeEntry()->designatedInterface()) { | |
565 AbstractMetaClass *interface = cls->extractInterface(); | |
566 m_meta_classes << interface; | |
567 ReportHandler::debugSparse(QString(" -> interface '%1'").arg(interface->name())); | |
568 } | |
569 } | |
570 } | |
571 | |
572 | |
573 AbstractMetaClass *AbstractMetaBuilder::traverseNamespace(NamespaceModelItem namespace_item) | |
574 { | |
575 QString namespace_name = (!m_namespace_prefix.isEmpty() ? m_namespace_prefix + "::" : QString()) + namespace_item->name(); | |
576 NamespaceTypeEntry *type = TypeDatabase::instance()->findNamespaceType(namespace_name); | |
577 | |
578 if (TypeDatabase::instance()->isClassRejected(namespace_name)) { | |
579 m_rejected_classes.insert(namespace_name, GenerationDisabled); | |
580 return 0; | |
581 } | |
582 | |
583 if (!type) { | |
584 ReportHandler::warning(QString("namespace '%1' does not have a type entry") | |
585 .arg(namespace_name)); | |
586 return 0; | |
587 } | |
588 | |
589 AbstractMetaClass *meta_class = createMetaClass(); | |
590 meta_class->setTypeEntry(type); | |
591 | |
592 *meta_class += AbstractMetaAttributes::Public; | |
593 | |
594 m_current_class = meta_class; | |
595 | |
596 ReportHandler::debugSparse(QString("namespace '%1.%2'") | |
597 .arg(meta_class->package()) | |
598 .arg(namespace_item->name())); | |
599 | |
600 traverseEnums(model_dynamic_cast<ScopeModelItem>(namespace_item), meta_class, namespace_item->enumsDeclarations()); | |
601 traverseFunctions(model_dynamic_cast<ScopeModelItem>(namespace_item), meta_class); | |
602 // traverseClasses(model_dynamic_cast<ScopeModelItem>(namespace_item)); | |
603 | |
604 pushScope(model_dynamic_cast<ScopeModelItem>(namespace_item)); | |
605 m_namespace_prefix = currentScope()->qualifiedName().join("::"); | |
606 | |
607 | |
608 ClassList classes = namespace_item->classes(); | |
609 foreach (ClassModelItem cls, classes) { | |
610 AbstractMetaClass *mjc = traverseClass(cls); | |
611 addAbstractMetaClass(mjc); | |
612 } | |
613 | |
614 // Go through all typedefs to see if we have defined any | |
615 // specific typedefs to be used as classes. | |
616 TypeAliasList typeAliases = namespace_item->typeAliases(); | |
617 foreach (TypeAliasModelItem typeAlias, typeAliases) { | |
618 AbstractMetaClass *cls = traverseTypeAlias(typeAlias); | |
619 addAbstractMetaClass(cls); | |
620 } | |
621 | |
622 | |
623 | |
624 // Traverse namespaces recursively | |
625 QList<NamespaceModelItem> inner_namespaces = namespace_item->namespaceMap().values(); | |
626 foreach (const NamespaceModelItem &ni, inner_namespaces) { | |
627 AbstractMetaClass *mjc = traverseNamespace(ni); | |
628 addAbstractMetaClass(mjc); | |
629 } | |
630 | |
631 m_current_class = 0; | |
632 | |
633 | |
634 popScope(); | |
635 m_namespace_prefix = currentScope()->qualifiedName().join("::"); | |
636 | |
637 if (!type->include().isValid()) { | |
638 QFileInfo info(namespace_item->fileName()); | |
639 type->setInclude(Include(Include::IncludePath, info.fileName())); | |
640 } | |
641 | |
642 return meta_class; | |
643 } | |
644 | |
645 struct Operator | |
646 { | |
647 enum Type { Plus, ShiftLeft, None }; | |
648 | |
649 Operator() : type(None) { } | |
650 | |
651 int calculate(int x) { | |
652 switch (type) { | |
653 case Plus: return x + value; | |
654 case ShiftLeft: return x << value; | |
655 case None: return x; | |
656 } | |
657 return x; | |
658 } | |
659 | |
660 Type type; | |
661 int value; | |
662 }; | |
663 | |
664 | |
665 | |
666 Operator findOperator(QString *s) { | |
667 const char *names[] = { | |
668 "+", | |
669 "<<" | |
670 }; | |
671 | |
672 for (int i=0; i<Operator::None; ++i) { | |
673 QString name = QLatin1String(names[i]); | |
674 QString str = *s; | |
675 int splitPoint = str.indexOf(name); | |
676 if (splitPoint > 0) { | |
677 bool ok; | |
678 QString right = str.mid(splitPoint + name.length()); | |
679 Operator op; | |
680 op.value = right.toInt(&ok); | |
681 if (ok) { | |
682 op.type = Operator::Type(i); | |
683 *s = str.left(splitPoint).trimmed(); | |
684 return op; | |
685 } | |
686 } | |
687 } | |
688 return Operator(); | |
689 } | |
690 | |
691 int AbstractMetaBuilder::figureOutEnumValue(const QString &stringValue, | |
692 int oldValuevalue, | |
693 AbstractMetaEnum *meta_enum, | |
694 AbstractMetaFunction *meta_function) | |
695 { | |
696 if (stringValue.isEmpty()) | |
697 return oldValuevalue; | |
698 | |
699 QStringList stringValues = stringValue.split("|"); | |
700 | |
701 int returnValue = 0; | |
702 | |
703 bool matched = false; | |
704 | |
705 for (int i=0; i<stringValues.size(); ++i) { | |
706 QString s = stringValues.at(i).trimmed(); | |
707 | |
708 bool ok; | |
709 int v; | |
710 | |
711 Operator op = findOperator(&s); | |
712 | |
713 if (s.length() > 0 && s.at(0) == QLatin1Char('0')) | |
714 v = s.toUInt(&ok, 0); | |
715 else | |
716 v = s.toInt(&ok); | |
717 | |
718 if (ok) { | |
719 matched = true; | |
720 | |
721 } else if (m_enum_values.contains(s)) { | |
722 v = m_enum_values[s]->value(); | |
723 matched = true; | |
724 | |
725 } else { | |
726 AbstractMetaEnumValue *ev = 0; | |
727 | |
728 if (meta_enum && (ev = meta_enum->values().find(s))) { | |
729 v = ev->value(); | |
730 matched = true; | |
731 | |
732 } else if (meta_enum && (ev = meta_enum->enclosingClass()->findEnumValue(s, meta_enum))) { | |
733 v = ev->value(); | |
734 matched = true; | |
735 | |
736 } else { | |
737 if (meta_enum) | |
738 ReportHandler::warning("unhandled enum value: " + s + " in " | |
739 + meta_enum->enclosingClass()->name() + "::" | |
740 + meta_enum->name()); | |
741 else | |
742 ReportHandler::warning("unhandled enum value: Unknown enum"); | |
743 } | |
744 } | |
745 | |
746 if (matched) | |
747 returnValue |= op.calculate(v); | |
748 } | |
749 | |
750 if (!matched) { | |
751 QString warn = QString("unmatched enum %1").arg(stringValue); | |
752 | |
753 if (meta_function != 0) { | |
754 warn += QString(" when parsing default value of '%1' in class '%2'") | |
755 .arg(meta_function->name()) | |
756 .arg(meta_function->implementingClass()->name()); | |
757 } | |
758 | |
759 ReportHandler::warning(warn); | |
760 returnValue = oldValuevalue; | |
761 } | |
762 | |
763 return returnValue; | |
764 } | |
765 | |
766 void AbstractMetaBuilder::figureOutEnumValuesForClass(AbstractMetaClass *meta_class, | |
767 QSet<AbstractMetaClass *> *classes) | |
768 { | |
769 AbstractMetaClass *base = meta_class->baseClass(); | |
770 | |
771 if (base != 0 && !classes->contains(base)) | |
772 figureOutEnumValuesForClass(base, classes); | |
773 | |
774 if (classes->contains(meta_class)) | |
775 return; | |
776 | |
777 AbstractMetaEnumList enums = meta_class->enums(); | |
778 foreach (AbstractMetaEnum *e, enums) { | |
779 if (!e) | |
780 ReportHandler::warning("bad enum in class " + meta_class->name()); | |
781 AbstractMetaEnumValueList lst = e->values(); | |
782 int value = 0; | |
783 for (int i=0; i<lst.size(); ++i) { | |
784 value = figureOutEnumValue(lst.at(i)->stringValue(), value, e); | |
785 lst.at(i)->setValue(value); | |
786 value++; | |
787 } | |
788 | |
789 // Check for duplicate values... | |
790 EnumTypeEntry *ete = e->typeEntry(); | |
791 if (!ete->forceInteger()) { | |
792 QHash<int, AbstractMetaEnumValue *> entries; | |
793 foreach (AbstractMetaEnumValue *v, lst) { | |
794 | |
795 bool vRejected = ete->isEnumValueRejected(v->name()); | |
796 | |
797 AbstractMetaEnumValue *current = entries.value(v->value()); | |
798 if (current) { | |
799 bool currentRejected = ete->isEnumValueRejected(current->name()); | |
800 if (!currentRejected && !vRejected) { | |
801 ReportHandler::warning( | |
802 QString("duplicate enum values: %1::%2, %3 and %4 are %5, already rejected: (%6)") | |
803 .arg(meta_class->name()) | |
804 .arg(e->name()) | |
805 .arg(v->name()) | |
806 .arg(entries[v->value()]->name()) | |
807 .arg(v->value()) | |
808 .arg(ete->enumValueRejections().join(", "))); | |
809 continue; | |
810 } | |
811 } | |
812 | |
813 if (!vRejected) | |
814 entries[v->value()] = v; | |
815 } | |
816 | |
817 // Entries now contain all the original entries, no | |
818 // rejected ones... Use this to generate the enumValueRedirection table. | |
819 foreach (AbstractMetaEnumValue *reject, lst) { | |
820 if (!ete->isEnumValueRejected(reject->name())) | |
821 continue; | |
822 | |
823 AbstractMetaEnumValue *used = entries.value(reject->value()); | |
824 if (!used) { | |
825 ReportHandler::warning( | |
826 QString::fromLatin1("Rejected enum has no alternative...: %1::%2\n") | |
827 .arg(meta_class->name()) | |
828 .arg(reject->name())); | |
829 continue; | |
830 } | |
831 ete->addEnumValueRedirection(reject->name(), used->name()); | |
832 } | |
833 | |
834 } | |
835 } | |
836 | |
837 | |
838 | |
839 *classes += meta_class; | |
840 } | |
841 | |
842 | |
843 void AbstractMetaBuilder::figureOutEnumValues() | |
844 { | |
845 // Keep a set of classes that we already traversed. We use this to | |
846 // enforce that we traverse base classes prior to subclasses. | |
847 QSet<AbstractMetaClass *> classes; | |
848 foreach (AbstractMetaClass *c, m_meta_classes) { | |
849 figureOutEnumValuesForClass(c, &classes); | |
850 } | |
851 } | |
852 | |
853 void AbstractMetaBuilder::figureOutDefaultEnumArguments() | |
854 { | |
855 foreach (AbstractMetaClass *meta_class, m_meta_classes) { | |
856 foreach (AbstractMetaFunction *meta_function, meta_class->functions()) { | |
857 foreach (AbstractMetaArgument *arg, meta_function->arguments()) { | |
858 | |
859 QString expr = arg->defaultValueExpression(); | |
860 if (expr.isEmpty()) | |
861 continue; | |
862 | |
863 if (!meta_function->replacedDefaultExpression(meta_function->implementingClass(), | |
864 arg->argumentIndex()+1).isEmpty()) { | |
865 continue; | |
866 } | |
867 | |
868 QString new_expr = expr; | |
869 if (arg->type()->isEnum()) { | |
870 QStringList lst = expr.split(QLatin1String("::")); | |
871 if (lst.size() == 1) { | |
872 QVector<AbstractMetaClass *> classes(1, meta_class); | |
873 AbstractMetaEnum *e = 0; | |
874 while (!classes.isEmpty() && e == 0) { | |
875 if (classes.front() != 0) { | |
876 classes << classes.front()->baseClass(); | |
877 | |
878 AbstractMetaClassList interfaces = classes.front()->interfaces(); | |
879 foreach (AbstractMetaClass *interface, interfaces) | |
880 classes << interface->primaryInterfaceImplementor(); | |
881 | |
882 e = classes.front()->findEnumForValue(expr); | |
883 } | |
884 | |
885 classes.pop_front(); | |
886 } | |
887 | |
888 if (e != 0) { | |
889 /* qtd new_expr = QString("%1.%2") | |
890 .arg(e->typeEntry()->qualifiedTargetLangName()) | |
891 .arg(expr); | |
892 */ | |
893 // new_expr = arg->type()->typeEntry()->targetLangName() + "." + expr; | |
894 new_expr = arg->type()->typeEntry()->qualifiedTargetLangName() + "." + expr; | |
895 } else { | |
896 ReportHandler::warning("Cannot find enum constant for value '" + expr + "' in '" + meta_class->name() + "' or any of its super classes"); | |
897 } | |
898 } else if (lst.size() == 2) { | |
899 AbstractMetaClass *cl = m_meta_classes.findClass(lst.at(0)); | |
900 if (!cl) { | |
901 ReportHandler::warning("missing required class for enums: " + lst.at(0)); | |
902 continue; | |
903 } | |
904 /* qtd new_expr = QString("%1.%2.%3") | |
905 .arg(cl->typeEntry()->qualifiedTargetLangName()) | |
906 .arg(arg->type()->name()) | |
907 .arg(lst.at(1)); | |
908 */ | |
909 // new_expr = arg->type()->typeEntry()->targetLangName() + "." + lst.at(1); | |
910 new_expr = arg->type()->typeEntry()->qualifiedTargetLangName() + "." + lst.at(1); | |
911 } else { | |
912 ReportHandler::warning("bad default value passed to enum " + expr); | |
913 } | |
914 | |
915 } else if(arg->type()->isFlags()) { | |
916 const FlagsTypeEntry *flagsEntry = | |
917 static_cast<const FlagsTypeEntry *>(arg->type()->typeEntry()); | |
918 EnumTypeEntry *enumEntry = flagsEntry->originator(); | |
919 AbstractMetaEnum *meta_enum = m_meta_classes.findEnum(enumEntry); | |
920 if (!meta_enum) { | |
921 ReportHandler::warning("unknown required enum " + enumEntry->qualifiedCppName()); | |
922 continue; | |
923 } | |
924 | |
925 int value = figureOutEnumValue(expr, 0, meta_enum, meta_function); | |
926 new_expr = QString::number(value); | |
927 | |
928 } else if (arg->type()->isPrimitive()) { | |
929 AbstractMetaEnumValue *value = 0; | |
930 if (expr.contains("::")) | |
931 value = m_meta_classes.findEnumValue(expr); | |
932 if (!value) | |
933 value = meta_class->findEnumValue(expr, 0); | |
934 | |
935 if (value) { | |
936 new_expr = QString::number(value->value()); | |
937 } else if (expr.contains(QLatin1Char('+'))) { | |
938 new_expr = QString::number(figureOutEnumValue(expr, 0, 0)); | |
939 | |
940 } | |
941 | |
942 | |
943 | |
944 } | |
945 | |
946 arg->setDefaultValueExpression(new_expr); | |
947 } | |
948 } | |
949 } | |
950 } | |
951 | |
952 | |
953 AbstractMetaEnum *AbstractMetaBuilder::traverseEnum(EnumModelItem enum_item, AbstractMetaClass *enclosing, const QSet<QString> &enumsDeclarations) | |
954 { | |
955 // Skipping private enums. | |
956 if (enum_item->accessPolicy() == CodeModel::Private) { | |
957 return 0; | |
958 } | |
959 | |
960 QString qualified_name = enum_item->qualifiedName().join("::"); | |
961 | |
962 TypeEntry *type_entry = TypeDatabase::instance()->findType(qualified_name); | |
963 QString enum_name = enum_item->name(); | |
964 | |
965 QString class_name; | |
966 if (m_current_class) | |
967 class_name = m_current_class->typeEntry()->qualifiedCppName(); | |
968 | |
969 if (TypeDatabase::instance()->isEnumRejected(class_name, enum_name)) { | |
970 m_rejected_enums.insert(qualified_name, GenerationDisabled); | |
971 return 0; | |
972 } | |
973 | |
974 if (!type_entry || !type_entry->isEnum()) { | |
975 QString context = m_current_class ? m_current_class->name() : QLatin1String(""); | |
976 ReportHandler::warning(QString("enum '%1' does not have a type entry or is not an enum") | |
977 .arg(qualified_name)); | |
978 m_rejected_enums.insert(qualified_name, NotInTypeSystem); | |
979 return 0; | |
980 } | |
981 | |
982 AbstractMetaEnum *meta_enum = createMetaEnum(); | |
983 if ( enumsDeclarations.contains(qualified_name) | |
984 || enumsDeclarations.contains(enum_name)) { | |
985 meta_enum->setHasQEnumsDeclaration(true); | |
986 } | |
987 | |
988 meta_enum->setTypeEntry((EnumTypeEntry *) type_entry); | |
989 switch (enum_item->accessPolicy()) { | |
990 case CodeModel::Public: *meta_enum += AbstractMetaAttributes::Public; break; | |
991 case CodeModel::Protected: *meta_enum += AbstractMetaAttributes::Protected; break; | |
992 // case CodeModel::Private: *meta_enum += AbstractMetaAttributes::Private; break; | |
993 default: break; | |
994 } | |
995 | |
996 ReportHandler::debugMedium(QString(" - traversing enum %1").arg(meta_enum->fullName())); | |
997 | |
998 foreach (EnumeratorModelItem value, enum_item->enumerators()) { | |
999 | |
1000 AbstractMetaEnumValue *meta_enum_value = createMetaEnumValue(); | |
1001 meta_enum_value->setName(value->name()); | |
1002 // Deciding the enum value... | |
1003 | |
1004 meta_enum_value->setStringValue(value->value()); | |
1005 meta_enum->addEnumValue(meta_enum_value); | |
1006 | |
1007 ReportHandler::debugFull(" - " + meta_enum_value->name() + " = " | |
1008 + meta_enum_value->value()); | |
1009 | |
1010 // Add into global register... | |
1011 if (enclosing) | |
1012 m_enum_values[enclosing->name() + "::" + meta_enum_value->name()] = meta_enum_value; | |
1013 else | |
1014 m_enum_values[meta_enum_value->name()] = meta_enum_value; | |
1015 } | |
1016 | |
1017 m_enums << meta_enum; | |
1018 | |
1019 return meta_enum; | |
1020 } | |
1021 | |
1022 AbstractMetaClass *AbstractMetaBuilder::traverseTypeAlias(TypeAliasModelItem typeAlias) | |
1023 { | |
1024 QString class_name = strip_template_args(typeAlias->name()); | |
1025 | |
1026 QString full_class_name = class_name; | |
1027 // we have an inner class | |
1028 if (m_current_class) { | |
1029 full_class_name = strip_template_args(m_current_class->typeEntry()->qualifiedCppName()) | |
1030 + "::" + full_class_name; | |
1031 } | |
1032 | |
1033 // If we haven't specified anything for the typedef, then we don't care | |
1034 ComplexTypeEntry *type = TypeDatabase::instance()->findComplexType(full_class_name); | |
1035 if (type == 0) | |
1036 return 0; | |
1037 | |
1038 if (type->isObject()) | |
1039 static_cast<ObjectTypeEntry *>(type)->setQObject(isQObject(strip_template_args(typeAlias->type().qualifiedName().join("::")))); | |
1040 | |
1041 AbstractMetaClass *meta_class = createMetaClass(); | |
1042 meta_class->setTypeAlias(true); | |
1043 meta_class->setTypeEntry(type); | |
1044 meta_class->setBaseClassNames(QStringList() << typeAlias->type().qualifiedName().join("::")); | |
1045 *meta_class += AbstractMetaAttributes::Public; | |
1046 | |
1047 // Set the default include file name | |
1048 if (!type->include().isValid()) { | |
1049 QFileInfo info(typeAlias->fileName()); | |
1050 type->setInclude(Include(Include::IncludePath, info.fileName())); | |
1051 } | |
1052 | |
1053 return meta_class; | |
1054 } | |
1055 | |
1056 AbstractMetaClass *AbstractMetaBuilder::traverseClass(ClassModelItem class_item) | |
1057 { | |
1058 QString class_name = strip_template_args(class_item->name()); | |
1059 QString full_class_name = class_name; | |
1060 | |
1061 // we have inner an class | |
1062 if (m_current_class) { | |
1063 full_class_name = strip_template_args(m_current_class->typeEntry()->qualifiedCppName()) | |
1064 + "::" + full_class_name; | |
1065 } | |
1066 | |
1067 ComplexTypeEntry *type = TypeDatabase::instance()->findComplexType(full_class_name); | |
1068 RejectReason reason = NoReason; | |
1069 | |
1070 | |
1071 if (TypeDatabase::instance()->isClassRejected(full_class_name)) { | |
1072 reason = GenerationDisabled; | |
1073 } else if (!type) { | |
1074 TypeEntry *te = TypeDatabase::instance()->findType(full_class_name); | |
1075 if (te && !te->isComplex()) | |
1076 reason = RedefinedToNotClass; | |
1077 else | |
1078 reason = NotInTypeSystem; | |
1079 } else if (type->codeGeneration() == TypeEntry::GenerateNothing) { | |
1080 reason = GenerationDisabled; | |
1081 } | |
1082 | |
1083 if (reason != NoReason) { | |
1084 m_rejected_classes.insert(full_class_name, reason); | |
1085 return 0; | |
1086 } | |
1087 | |
1088 if (type->isObject()) { | |
1089 ((ObjectTypeEntry *)type)->setQObject(isQObject(full_class_name)); | |
1090 } | |
1091 | |
1092 AbstractMetaClass *meta_class = createMetaClass(); | |
1093 meta_class->setTypeEntry(type); | |
1094 meta_class->setBaseClassNames(class_item->baseClasses()); | |
1095 *meta_class += AbstractMetaAttributes::Public; | |
1096 | |
1097 AbstractMetaClass *old_current_class = m_current_class; | |
1098 m_current_class = meta_class; | |
1099 | |
1100 if (type->isContainer()) { | |
1101 ReportHandler::debugSparse(QString("container: '%1'").arg(full_class_name)); | |
1102 } else { | |
1103 ReportHandler::debugSparse(QString("class: '%1'").arg(meta_class->fullName())); | |
1104 } | |
1105 | |
1106 TemplateParameterList template_parameters = class_item->templateParameters(); | |
1107 QList<TypeEntry *> template_args; | |
1108 template_args.clear(); | |
1109 for (int i=0; i<template_parameters.size(); ++i) { | |
1110 const TemplateParameterModelItem ¶m = template_parameters.at(i); | |
1111 TemplateArgumentEntry *param_type = new TemplateArgumentEntry(param->name()); | |
1112 param_type->setOrdinal(i); | |
1113 template_args.append(param_type); | |
1114 } | |
1115 meta_class->setTemplateArguments(template_args); | |
1116 | |
1117 parseQ_Property(meta_class, class_item->propertyDeclarations()); | |
1118 | |
1119 traverseFunctions(model_dynamic_cast<ScopeModelItem>(class_item), meta_class); | |
1120 traverseEnums(model_dynamic_cast<ScopeModelItem>(class_item), meta_class, class_item->enumsDeclarations()); | |
1121 traverseFields(model_dynamic_cast<ScopeModelItem>(class_item), meta_class); | |
1122 | |
1123 // Inner classes | |
1124 { | |
1125 QList<ClassModelItem> inner_classes = class_item->classMap().values(); | |
1126 foreach (const ClassModelItem &ci, inner_classes) { | |
1127 AbstractMetaClass *cl = traverseClass(ci); | |
1128 if (cl) { | |
1129 cl->setEnclosingClass(meta_class); | |
1130 m_meta_classes << cl; | |
1131 } | |
1132 } | |
1133 | |
1134 } | |
1135 | |
1136 // Go through all typedefs to see if we have defined any | |
1137 // specific typedefs to be used as classes. | |
1138 TypeAliasList typeAliases = class_item->typeAliases(); | |
1139 foreach (TypeAliasModelItem typeAlias, typeAliases) { | |
1140 AbstractMetaClass *cls = traverseTypeAlias(typeAlias); | |
1141 if (cls != 0) { | |
1142 cls->setEnclosingClass(meta_class); | |
1143 addAbstractMetaClass(cls); | |
1144 } | |
1145 } | |
1146 | |
1147 | |
1148 m_current_class = old_current_class; | |
1149 | |
1150 // Set the default include file name | |
1151 if (!type->include().isValid()) { | |
1152 QFileInfo info(class_item->fileName()); | |
1153 type->setInclude(Include(Include::IncludePath, info.fileName())); | |
1154 } | |
1155 | |
1156 return meta_class; | |
1157 } | |
1158 | |
1159 AbstractMetaField *AbstractMetaBuilder::traverseField(VariableModelItem field, const AbstractMetaClass *cls) | |
1160 { | |
1161 QString field_name = field->name(); | |
1162 QString class_name = m_current_class->typeEntry()->qualifiedCppName(); | |
1163 | |
1164 // Ignore friend decl. | |
1165 if (field->isFriend()) | |
1166 return 0; | |
1167 | |
1168 if (field->accessPolicy() == CodeModel::Private) | |
1169 return 0; | |
1170 | |
1171 if (TypeDatabase::instance()->isFieldRejected(class_name, field_name)) { | |
1172 m_rejected_fields.insert(class_name + "::" + field_name, GenerationDisabled); | |
1173 return 0; | |
1174 } | |
1175 | |
1176 | |
1177 AbstractMetaField *meta_field = createMetaField(); | |
1178 meta_field->setName(field_name); | |
1179 meta_field->setEnclosingClass(cls); | |
1180 | |
1181 bool ok; | |
1182 TypeInfo field_type = field->type(); | |
1183 AbstractMetaType *meta_type = translateType(field_type, &ok); | |
1184 | |
1185 if (!meta_type || !ok) { | |
1186 ReportHandler::warning(QString("skipping field '%1::%2' with unmatched type '%3'") | |
1187 .arg(m_current_class->name()) | |
1188 .arg(field_name) | |
1189 .arg(TypeInfo::resolveType(field_type, currentScope()->toItem()).qualifiedName().join("::"))); | |
1190 delete meta_field; | |
1191 return 0; | |
1192 } | |
1193 | |
1194 meta_field->setType(meta_type); | |
1195 | |
1196 uint attr = 0; | |
1197 if (field->isStatic()) | |
1198 attr |= AbstractMetaAttributes::Static; | |
1199 | |
1200 CodeModel::AccessPolicy policy = field->accessPolicy(); | |
1201 if (policy == CodeModel::Public) | |
1202 attr |= AbstractMetaAttributes::Public; | |
1203 else if (policy == CodeModel::Protected) | |
1204 attr |= AbstractMetaAttributes::Protected; | |
1205 else | |
1206 attr |= AbstractMetaAttributes::Private; | |
1207 meta_field->setAttributes(attr); | |
1208 | |
1209 return meta_field; | |
1210 } | |
1211 | |
1212 void AbstractMetaBuilder::traverseFields(ScopeModelItem scope_item, AbstractMetaClass *meta_class) | |
1213 { | |
1214 foreach (VariableModelItem field, scope_item->variables()) { | |
1215 AbstractMetaField *meta_field = traverseField(field, meta_class); | |
1216 | |
1217 if (meta_field) { | |
1218 meta_field->setOriginalAttributes(meta_field->attributes()); | |
1219 meta_class->addField(meta_field); | |
1220 } | |
1221 } | |
1222 } | |
1223 | |
1224 void AbstractMetaBuilder::setupFunctionDefaults(AbstractMetaFunction *meta_function, AbstractMetaClass *meta_class) | |
1225 { | |
1226 // Set the default value of the declaring class. This may be changed | |
1227 // in fixFunctions later on | |
1228 meta_function->setDeclaringClass(meta_class); | |
1229 | |
1230 // Some of the queries below depend on the implementing class being set | |
1231 // to function properly. Such as function modifications | |
1232 meta_function->setImplementingClass(meta_class); | |
1233 | |
1234 if (meta_function->name() == "operator_equal") | |
1235 meta_class->setHasEqualsOperator(true); | |
1236 | |
1237 if (!meta_function->isFinalInTargetLang() | |
1238 && meta_function->isRemovedFrom(meta_class, TypeSystem::TargetLangCode)) { | |
1239 *meta_function += AbstractMetaAttributes::FinalInCpp; | |
1240 } | |
1241 } | |
1242 | |
1243 void AbstractMetaBuilder::traverseFunctions(ScopeModelItem scope_item, AbstractMetaClass *meta_class) | |
1244 { | |
1245 foreach (FunctionModelItem function, scope_item->functions()) { | |
1246 AbstractMetaFunction *meta_function = traverseFunction(function); | |
1247 | |
1248 if (meta_function) { | |
148
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1249 |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1250 QList<FunctionModification> mods = meta_function->modifications(meta_class); |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1251 for (int i=0; i<mods.size(); ++i) { |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1252 if (mods.at(i).isPrivateSignal()) { |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1253 meta_function->setFunctionType(AbstractMetaFunction::SignalFunction); |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1254 } |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1255 } |
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
1
diff
changeset
|
1256 |
1 | 1257 meta_function->setOriginalAttributes(meta_function->attributes()); |
1258 if (meta_class->isNamespace()) | |
1259 *meta_function += AbstractMetaAttributes::Static; | |
1260 | |
1261 if (!meta_function->isInvalid()) { | |
1262 if (QPropertySpec *read = meta_class->propertySpecForRead(meta_function->name())) { | |
1263 if (read->type() == meta_function->type()->typeEntry()) { | |
1264 *meta_function += AbstractMetaAttributes::PropertyReader; | |
1265 meta_function->setPropertySpec(read); | |
1266 // printf("%s is reader for %s\n", | |
1267 // qPrintable(meta_function->name()), | |
1268 // qPrintable(read->name())); | |
1269 } | |
1270 } else if (QPropertySpec *write = | |
1271 meta_class->propertySpecForWrite(meta_function->name())) { | |
1272 if (write->type() == meta_function->arguments().at(0)->type()->typeEntry()) { | |
1273 *meta_function += AbstractMetaAttributes::PropertyWriter; | |
1274 meta_function->setPropertySpec(write); | |
1275 /* | |
1276 // qtd D properties syntax, rename setter from setPropertyName to just propertyName | |
1277 AbstractMetaFunction *f_copy = new AbstractMetaFunction(); | |
1278 *f_copy = *meta_function; | |
1279 | |
1280 QString f_name = meta_function->name(); | |
1281 QString new_name = f_name.midRef(3, 1).toString().toLower() + f_name.right(f_name.length() - 4); | |
1282 meta_function->setName(new_name); | |
1283 // f_copy->setOwnerClass(meta_function->ownerClass()); | |
1284 ((AbstractMetaClass*)(meta_function->ownerClass()))->addFunction(f_copy); // hack qtd2 | |
1285 // | |
1286 */ | |
1287 // printf("%s is writer for %s\n", | |
1288 // qPrintable(meta_function->name()), | |
1289 // qPrintable(write->name())); | |
1290 } | |
1291 } else if (QPropertySpec *reset = | |
1292 meta_class->propertySpecForReset(meta_function->name())) { | |
1293 *meta_function += AbstractMetaAttributes::PropertyResetter; | |
1294 meta_function->setPropertySpec(reset); | |
1295 // printf("%s is resetter for %s\n", | |
1296 // qPrintable(meta_function->name()), | |
1297 // qPrintable(reset->name())); | |
1298 } | |
1299 } | |
1300 | |
1301 | |
1302 bool isInvalidDestructor = meta_function->isDestructor() && meta_function->isPrivate(); | |
1303 bool isInvalidConstructor = meta_function->isConstructor() | |
1304 && (meta_function->isPrivate() || meta_function->isInvalid()); | |
1305 if ((isInvalidDestructor || isInvalidConstructor) | |
1306 && !meta_class->hasNonPrivateConstructor()) { | |
1307 *meta_class += AbstractMetaAttributes::Final; | |
1308 } else if (meta_function->isConstructor() && !meta_function->isPrivate()) { | |
1309 *meta_class -= AbstractMetaAttributes::Final; | |
1310 meta_class->setHasNonPrivateConstructor(true); | |
1311 } | |
1312 | |
1313 // Classes with virtual destructors should always have a shell class | |
1314 // (since we aren't registering the destructors, we need this extra check) | |
1315 if (meta_function->isDestructor() && !meta_function->isFinal()) | |
1316 meta_class->setForceShellClass(true); | |
1317 | |
1318 if (!meta_function->isDestructor() | |
1319 && !meta_function->isInvalid() | |
1320 && (!meta_function->isConstructor() || !meta_function->isPrivate())) { | |
1321 | |
1322 if (meta_class->typeEntry()->designatedInterface() && !meta_function->isPublic() | |
1323 && !meta_function->isPrivate()) { | |
1324 QString warn = QString("non-public function '%1' in interface '%2'") | |
1325 .arg(meta_function->name()).arg(meta_class->name()); | |
1326 ReportHandler::warning(warn); | |
1327 | |
1328 meta_function->setVisibility(AbstractMetaClass::Public); | |
1329 } | |
1330 | |
1331 setupFunctionDefaults(meta_function, meta_class); | |
1332 | |
1333 if (meta_function->isSignal() && meta_class->hasSignal(meta_function)) { | |
1334 QString warn = QString("signal '%1' in class '%2' is overloaded.") | |
1335 .arg(meta_function->name()).arg(meta_class->name()); | |
1336 ReportHandler::warning(warn); | |
1337 } | |
1338 | |
1339 if (meta_function->isSignal() && !meta_class->isQObject()) { | |
1340 QString warn = QString("signal '%1' in non-QObject class '%2'") | |
1341 .arg(meta_function->name()).arg(meta_class->name()); | |
1342 ReportHandler::warning(warn); | |
1343 } | |
1344 | |
1345 meta_class->addFunction(meta_function); | |
1346 } else if (meta_function->isDestructor() && !meta_function->isPublic()) { | |
1347 meta_class->setHasPublicDestructor(false); | |
1348 } | |
1349 } | |
1350 } | |
1351 } | |
1352 | |
1353 bool AbstractMetaBuilder::setupInheritance(AbstractMetaClass *meta_class) | |
1354 { | |
1355 Q_ASSERT(!meta_class->isInterface()); | |
1356 | |
1357 if (m_setup_inheritance_done.contains(meta_class)) | |
1358 return true; | |
1359 m_setup_inheritance_done.insert(meta_class); | |
1360 | |
1361 QStringList base_classes = meta_class->baseClassNames(); | |
1362 | |
1363 TypeDatabase *types = TypeDatabase::instance(); | |
1364 | |
1365 // we only support our own containers and ONLY if there is only one baseclass | |
1366 if (base_classes.size() == 1 && base_classes.first().count('<') == 1) { | |
1367 QStringList scope = meta_class->typeEntry()->qualifiedCppName().split("::"); | |
1368 scope.removeLast(); | |
1369 for (int i=scope.size(); i>=0; --i) { | |
1370 QString prefix = i > 0 ? QStringList(scope.mid(0, i)).join("::") + "::" : QString(); | |
1371 QString complete_name = prefix + base_classes.first(); | |
1372 TypeParser::Info info = TypeParser::parse(complete_name); | |
1373 QString base_name = info.qualified_name.join("::"); | |
1374 | |
1375 AbstractMetaClass *templ = 0; | |
1376 foreach (AbstractMetaClass *c, m_templates) { | |
1377 if (c->typeEntry()->name() == base_name) { | |
1378 templ = c; | |
1379 break; | |
1380 } | |
1381 } | |
1382 | |
1383 if (templ == 0) | |
1384 templ = m_meta_classes.findClass(base_name); | |
1385 | |
1386 if (templ) { | |
1387 setupInheritance(templ); | |
1388 inheritTemplate(meta_class, templ, info); | |
1389 return true; | |
1390 } | |
1391 } | |
1392 | |
1393 ReportHandler::warning(QString("template baseclass '%1' of '%2' is not known") | |
1394 .arg(base_classes.first()) | |
1395 .arg(meta_class->name())); | |
1396 return false; | |
1397 } | |
1398 | |
1399 int primary = -1; | |
1400 int primaries = 0; | |
1401 for (int i=0; i<base_classes.size(); ++i) { | |
1402 | |
1403 if (types->isClassRejected(base_classes.at(i))) | |
1404 continue; | |
1405 | |
1406 TypeEntry *base_class_entry = types->findType(base_classes.at(i)); | |
1407 if (!base_class_entry) { | |
1408 ReportHandler::warning(QString("class '%1' inherits from unknown base class '%2'") | |
1409 .arg(meta_class->name()).arg(base_classes.at(i))); | |
1410 } | |
1411 | |
1412 // true for primary base class | |
1413 else if (!base_class_entry->designatedInterface()) { | |
1414 if (primaries > 0) { | |
1415 ReportHandler::warning(QString("class '%1' has multiple primary base classes" | |
1416 " '%2' and '%3'") | |
1417 .arg(meta_class->name()) | |
1418 .arg(base_classes.at(primary)) | |
1419 .arg(base_class_entry->name())); | |
1420 return false; | |
1421 } | |
1422 primaries++; | |
1423 primary = i; | |
1424 } | |
1425 } | |
1426 | |
1427 if (primary >= 0) { | |
1428 AbstractMetaClass *base_class = m_meta_classes.findClass(base_classes.at(primary)); | |
1429 if (!base_class) { | |
1430 ReportHandler::warning(QString("unknown baseclass for '%1': '%2'") | |
1431 .arg(meta_class->name()) | |
1432 .arg(base_classes.at(primary))); | |
1433 return false; | |
1434 } | |
1435 meta_class->setBaseClass(base_class); | |
1436 | |
1437 if (meta_class->typeEntry()->designatedInterface() != 0 && meta_class->isQObject()) { | |
1438 ReportHandler::warning(QString("QObject extended by interface type '%1'. This is not supported and the generated Java code will not compile.") | |
1439 .arg(meta_class->name())); | |
1440 } else if (meta_class->typeEntry()->designatedInterface() != 0 && base_class != 0 && !base_class->isInterface()) { | |
1441 ReportHandler::warning(QString("object type '%1' extended by interface type '%2'. The resulting API will be less expressive than the original.") | |
1442 .arg(base_class->name()) | |
1443 .arg(meta_class->name())); | |
1444 } | |
1445 | |
1446 } | |
1447 | |
1448 for (int i=0; i<base_classes.size(); ++i) { | |
1449 if (types->isClassRejected(base_classes.at(i))) | |
1450 continue; | |
1451 | |
1452 if (i != primary) { | |
1453 AbstractMetaClass *base_class = m_meta_classes.findClass(base_classes.at(i)); | |
1454 if (base_class == 0) { | |
1455 ReportHandler::warning(QString("class not found for setup inheritance '%1'").arg(base_classes.at(i))); | |
1456 return false; | |
1457 } | |
1458 | |
1459 setupInheritance(base_class); | |
1460 | |
1461 QString interface_name = InterfaceTypeEntry::interfaceName(base_class->name()); | |
1462 AbstractMetaClass *iface = m_meta_classes.findClass(interface_name); | |
1463 if (!iface) { | |
1464 ReportHandler::warning(QString("unknown interface for '%1': '%2'") | |
1465 .arg(meta_class->name()) | |
1466 .arg(interface_name)); | |
1467 return false; | |
1468 } | |
1469 meta_class->addInterface(iface); | |
1470 | |
1471 AbstractMetaClassList interfaces = iface->interfaces(); | |
1472 foreach (AbstractMetaClass *iface, interfaces) | |
1473 meta_class->addInterface(iface); | |
1474 } | |
1475 } | |
1476 | |
1477 return true; | |
1478 } | |
1479 | |
1480 void AbstractMetaBuilder::traverseEnums(ScopeModelItem scope_item, AbstractMetaClass *meta_class, const QStringList &enumsDeclarations) | |
1481 { | |
1482 EnumList enums = scope_item->enums(); | |
1483 foreach (EnumModelItem enum_item, enums) { | |
1484 AbstractMetaEnum *meta_enum = traverseEnum(enum_item, meta_class, QSet<QString>::fromList(enumsDeclarations)); | |
1485 if (meta_enum) { | |
1486 meta_enum->setOriginalAttributes(meta_enum->attributes()); | |
1487 meta_class->addEnum(meta_enum); | |
1488 meta_enum->setEnclosingClass(meta_class); | |
1489 } | |
1490 } | |
1491 } | |
1492 | |
1493 AbstractMetaFunction *AbstractMetaBuilder::traverseFunction(FunctionModelItem function_item) | |
1494 { | |
1495 QString function_name = function_item->name(); | |
1496 QString class_name = m_current_class->typeEntry()->qualifiedCppName(); | |
1497 | |
1498 if (TypeDatabase::instance()->isFunctionRejected(class_name, function_name)) { | |
1499 m_rejected_functions.insert(class_name + "::" + function_name, GenerationDisabled); | |
1500 return 0; | |
1501 } | |
1502 | |
1503 | |
1504 Q_ASSERT(function_item->functionType() == CodeModel::Normal | |
1505 || function_item->functionType() == CodeModel::Signal | |
1506 || function_item->functionType() == CodeModel::Slot); | |
1507 | |
1508 if (function_item->isFriend()) | |
1509 return 0; | |
1510 | |
1511 | |
1512 QString cast_type; | |
1513 | |
1514 if (function_name.startsWith("operator")) { | |
1515 function_name = rename_operator(function_name.mid(8)); | |
1516 if (function_name.isEmpty()) { | |
1517 m_rejected_functions.insert(class_name + "::" + function_name, | |
1518 GenerationDisabled); | |
1519 return 0; | |
1520 } | |
1521 if (function_name.contains("_cast_")) | |
1522 cast_type = function_name.mid(14).trimmed(); | |
1523 } | |
1524 | |
1525 AbstractMetaFunction *meta_function = createMetaFunction(); | |
1526 meta_function->setConstant(function_item->isConstant()); | |
1527 | |
1528 ReportHandler::debugMedium(QString(" - %2()").arg(function_name)); | |
1529 | |
1530 meta_function->setName(function_name); | |
1531 meta_function->setOriginalName(function_item->name()); | |
1532 | |
1533 if (function_item->isAbstract()) | |
1534 *meta_function += AbstractMetaAttributes::Abstract; | |
1535 | |
1536 if (!meta_function->isAbstract()) | |
1537 *meta_function += AbstractMetaAttributes::Native; | |
1538 | |
1539 if (!function_item->isVirtual()) | |
1540 *meta_function += AbstractMetaAttributes::Final; | |
1541 | |
1542 if (function_item->isInvokable()) | |
1543 *meta_function += AbstractMetaAttributes::Invokable; | |
1544 | |
1545 if (function_item->isStatic()) { | |
1546 *meta_function += AbstractMetaAttributes::Static; | |
1547 *meta_function += AbstractMetaAttributes::Final; | |
1548 } | |
1549 | |
1550 // Access rights | |
1551 if (function_item->accessPolicy() == CodeModel::Public) | |
1552 *meta_function += AbstractMetaAttributes::Public; | |
1553 else if (function_item->accessPolicy() == CodeModel::Private) | |
1554 *meta_function += AbstractMetaAttributes::Private; | |
1555 else | |
1556 *meta_function += AbstractMetaAttributes::Protected; | |
1557 | |
1558 | |
1559 QString stripped_class_name = class_name; | |
1560 int cc_pos = stripped_class_name.lastIndexOf("::"); | |
1561 if (cc_pos > 0) | |
1562 stripped_class_name = stripped_class_name.mid(cc_pos + 2); | |
1563 | |
1564 TypeInfo function_type = function_item->type(); | |
1565 if (function_name.startsWith('~')) { | |
1566 meta_function->setFunctionType(AbstractMetaFunction::DestructorFunction); | |
1567 meta_function->setInvalid(true); | |
1568 } else if (strip_template_args(function_name) == stripped_class_name) { | |
1569 meta_function->setFunctionType(AbstractMetaFunction::ConstructorFunction); | |
1570 meta_function->setName(m_current_class->name()); | |
1571 } else { | |
1572 bool ok; | |
1573 AbstractMetaType *type = 0; | |
1574 | |
1575 if (!cast_type.isEmpty()) { | |
1576 TypeInfo info; | |
1577 info.setQualifiedName(QStringList(cast_type)); | |
1578 type = translateType(info, &ok); | |
1579 } else { | |
1580 type = translateType(function_type, &ok); | |
1581 } | |
1582 | |
1583 if (!ok) { | |
1584 ReportHandler::warning(QString("skipping function '%1::%2', unmatched return type '%3'") | |
1585 .arg(class_name) | |
1586 .arg(function_item->name()) | |
1587 .arg(function_item->type().toString())); | |
1588 m_rejected_functions[class_name + "::" + function_name] = | |
1589 UnmatchedReturnType; | |
1590 meta_function->setInvalid(true); | |
1591 return meta_function; | |
1592 } | |
1593 meta_function->setType(type); | |
1594 | |
1595 if (function_item->functionType() == CodeModel::Signal) | |
1596 meta_function->setFunctionType(AbstractMetaFunction::SignalFunction); | |
1597 else if (function_item->functionType() == CodeModel::Slot) | |
1598 meta_function->setFunctionType(AbstractMetaFunction::SlotFunction); | |
1599 } | |
1600 | |
1601 ArgumentList arguments = function_item->arguments(); | |
1602 AbstractMetaArgumentList meta_arguments; | |
1603 | |
1604 int first_default_argument = 0; | |
1605 for (int i=0; i<arguments.size(); ++i) { | |
1606 ArgumentModelItem arg = arguments.at(i); | |
1607 | |
1608 bool ok; | |
1609 AbstractMetaType *meta_type = translateType(arg->type(), &ok); | |
1610 if (!meta_type || !ok) { | |
1611 ReportHandler::warning(QString("skipping function '%1::%2', " | |
1612 "unmatched parameter type '%3'") | |
1613 .arg(class_name) | |
1614 .arg(function_item->name()) | |
1615 .arg(arg->type().toString())); | |
1616 m_rejected_functions[class_name + "::" + function_name] = | |
1617 UnmatchedArgumentType; | |
1618 meta_function->setInvalid(true); | |
1619 return meta_function; | |
1620 } | |
1621 AbstractMetaArgument *meta_argument = createMetaArgument(); | |
1622 meta_argument->setType(meta_type); | |
1623 meta_argument->setName(arg->name()); | |
1624 meta_argument->setArgumentIndex(i); | |
1625 meta_arguments << meta_argument; | |
1626 } | |
1627 | |
1628 meta_function->setArguments(meta_arguments); | |
1629 | |
1630 // Find the correct default values | |
1631 for (int i=0; i<arguments.size(); ++i) { | |
1632 ArgumentModelItem arg = arguments.at(i); | |
1633 AbstractMetaArgument *meta_arg = meta_arguments.at(i); | |
1634 if (arg->defaultValue()) { | |
1635 QString expr = arg->defaultValueExpression(); | |
1636 if (!expr.isEmpty()) | |
1637 meta_arg->setOriginalDefaultValueExpression(expr); | |
1638 | |
1639 expr = translateDefaultValue(arg, meta_arg->type(), meta_function, m_current_class, i); | |
1640 if (expr.isEmpty()) { | |
1641 first_default_argument = i; | |
1642 } else { | |
1643 meta_arg->setDefaultValueExpression(expr); | |
1644 } | |
1645 | |
1646 if (meta_arg->type()->isEnum() || meta_arg->type()->isFlags()) { | |
1647 m_enum_default_arguments | |
1648 << QPair<AbstractMetaArgument *, AbstractMetaFunction *>(meta_arg, meta_function); | |
1649 } | |
1650 | |
1651 } | |
1652 } | |
1653 | |
1654 // If we where not able to translate the default argument make it | |
1655 // reset all default arguments before this one too. | |
1656 for (int i=0; i<first_default_argument; ++i) | |
1657 meta_arguments[i]->setDefaultValueExpression(QString()); | |
1658 | |
1659 if (ReportHandler::debugLevel() == ReportHandler::FullDebug) | |
1660 foreach(AbstractMetaArgument *arg, meta_arguments) | |
1661 ReportHandler::debugFull(" - " + arg->toString()); | |
1662 | |
1663 return meta_function; | |
1664 } | |
1665 | |
1666 | |
1667 AbstractMetaType *AbstractMetaBuilder::translateType(const TypeInfo &_typei, bool *ok, bool resolveType, bool resolveScope) | |
1668 { | |
1669 Q_ASSERT(ok); | |
1670 *ok = true; | |
1671 | |
1672 // 1. Test the type info without resolving typedefs in case this is present in the | |
1673 // type system | |
1674 TypeInfo typei; | |
1675 if (resolveType) { | |
1676 bool ok; | |
1677 AbstractMetaType *t = translateType(_typei, &ok, false, resolveScope); | |
1678 if (t != 0 && ok) | |
1679 return t; | |
1680 } | |
1681 | |
1682 if (!resolveType) | |
1683 typei = _typei; | |
1684 else { | |
1685 // Go through all parts of the current scope (including global namespace) | |
1686 // to resolve typedefs. The parser does not properly resolve typedefs in | |
1687 // the global scope when they are referenced from inside a namespace. | |
1688 // This is a work around to fix this bug since fixing it in resolveType | |
1689 // seemed non-trivial | |
1690 int i = m_scopes.size() - 1; | |
1691 while (i >= 0) { | |
1692 typei = TypeInfo::resolveType(_typei, m_scopes.at(i--)->toItem()); | |
1693 if (typei.qualifiedName().join("::") != _typei.qualifiedName().join("::")) | |
1694 break; | |
1695 } | |
1696 | |
1697 } | |
1698 | |
1699 if (typei.isFunctionPointer()) { | |
1700 *ok = false; | |
1701 return 0; | |
1702 } | |
1703 | |
1704 TypeParser::Info typeInfo = TypeParser::parse(typei.toString()); | |
1705 if (typeInfo.is_busted) { | |
1706 *ok = false; | |
1707 return 0; | |
1708 } | |
1709 | |
1710 // 2. Handle pointers specified as arrays with unspecified size | |
1711 bool array_of_unspecified_size = false; | |
1712 if (typeInfo.arrays.size() > 0) { | |
1713 array_of_unspecified_size = true; | |
1714 for (int i=0; i<typeInfo.arrays.size(); ++i) | |
1715 array_of_unspecified_size = array_of_unspecified_size && typeInfo.arrays.at(i).isEmpty(); | |
1716 | |
1717 if (!array_of_unspecified_size) { | |
1718 TypeInfo newInfo; | |
1719 //newInfo.setArguments(typei.arguments()); | |
1720 newInfo.setIndirections(typei.indirections()); | |
1721 newInfo.setConstant(typei.isConstant()); | |
1722 newInfo.setFunctionPointer(typei.isFunctionPointer()); | |
1723 newInfo.setQualifiedName(typei.qualifiedName()); | |
1724 newInfo.setReference(typei.isReference()); | |
1725 newInfo.setVolatile(typei.isVolatile()); | |
1726 | |
1727 AbstractMetaType *elementType = translateType(newInfo, ok); | |
1728 if (!ok) | |
1729 return 0; | |
1730 | |
1731 for (int i=typeInfo.arrays.size()-1; i>=0; --i) { | |
1732 QString s = typeInfo.arrays.at(i); | |
1733 bool ok; | |
1734 | |
1735 int elems = s.toInt(&ok); | |
1736 if (!ok) | |
1737 return 0; | |
1738 | |
1739 AbstractMetaType *arrayType = createMetaType(); | |
1740 arrayType->setArrayElementCount(elems); | |
1741 arrayType->setArrayElementType(elementType); | |
1742 arrayType->setTypeEntry(new ArrayTypeEntry(elementType->typeEntry())); | |
1743 decideUsagePattern(arrayType); | |
1744 | |
1745 elementType = arrayType; | |
1746 } | |
1747 | |
1748 return elementType; | |
1749 } else { | |
1750 typeInfo.indirections += typeInfo.arrays.size(); | |
1751 } | |
1752 } | |
1753 | |
1754 QStringList qualifier_list = typeInfo.qualified_name; | |
1755 if (qualifier_list.isEmpty()) { | |
1756 ReportHandler::warning(QString("horribly broken type '%1'").arg(_typei.toString())); | |
1757 *ok = false; | |
1758 return 0; | |
1759 } | |
1760 | |
1761 QString qualified_name = qualifier_list.join("::"); | |
1762 QString name = qualifier_list.takeLast(); | |
1763 | |
1764 // 3. Special case 'void' type | |
1765 if (name == "void" && typeInfo.indirections == 0) { | |
1766 return 0; | |
1767 } | |
1768 | |
1769 // 4. Special case QFlags (include instantiation in name) | |
1770 if (qualified_name == "QFlags") | |
1771 qualified_name = typeInfo.toString(); | |
1772 | |
1773 // 5. Try to find the type | |
1774 const TypeEntry *type = TypeDatabase::instance()->findType(qualified_name); | |
1775 | |
1776 // 6. No? Try looking it up as a flags type | |
1777 if (!type) | |
1778 type = TypeDatabase::instance()->findFlagsType(qualified_name); | |
1779 | |
1780 // 7. No? Try looking it up as a container type | |
1781 if (!type) | |
1782 type = TypeDatabase::instance()->findContainerType(name); | |
1783 | |
1784 // 8. No? Check if the current class is a template and this type is one | |
1785 // of the parameters. | |
1786 if (type == 0 && m_current_class != 0) { | |
1787 QList<TypeEntry *> template_args = m_current_class->templateArguments(); | |
1788 foreach (TypeEntry *te, template_args) { | |
1789 if (te->name() == qualified_name) | |
1790 type = te; | |
1791 } | |
1792 } | |
1793 | |
1794 // 9. Try finding the type by prefixing it with the current | |
1795 // context and all baseclasses of the current context | |
1796 if (!type && !TypeDatabase::instance()->isClassRejected(qualified_name) && m_current_class != 0 && resolveScope) { | |
1797 QStringList contexts; | |
1798 contexts.append(m_current_class->qualifiedCppName()); | |
1799 contexts.append(currentScope()->qualifiedName().join("::")); | |
1800 | |
1801 | |
1802 TypeInfo info = typei; | |
1803 bool subclasses_done = false; | |
1804 while (!contexts.isEmpty() && type == 0) { | |
1805 //type = TypeDatabase::instance()->findType(contexts.at(0) + "::" + qualified_name); | |
1806 | |
1807 bool ok; | |
1808 info.setQualifiedName(QStringList() << contexts.at(0) << qualified_name); | |
1809 AbstractMetaType *t = translateType(info, &ok, true, false); | |
1810 if (t != 0 && ok) | |
1811 return t; | |
1812 | |
1813 ClassModelItem item = m_dom->findClass(contexts.at(0)); | |
1814 if (item != 0) | |
1815 contexts += item->baseClasses(); | |
1816 contexts.pop_front(); | |
1817 | |
1818 // 10. Last resort: Special cased prefix of Qt namespace since the meta object implicitly inherits this, so | |
1819 // enum types from there may be addressed without any scope resolution in properties. | |
1820 if (contexts.size() == 0 && !subclasses_done) { | |
1821 contexts << "Qt"; | |
1822 subclasses_done = true; | |
1823 } | |
1824 } | |
1825 | |
1826 } | |
1827 | |
1828 if (!type) { | |
1829 *ok = false; | |
1830 return 0; | |
1831 } | |
1832 | |
1833 // Used to for diagnostics later... | |
1834 m_used_types << type; | |
1835 | |
1836 // These are only implicit and should not appear in code... | |
1837 Q_ASSERT(!type->isInterface()); | |
1838 | |
1839 AbstractMetaType *meta_type = createMetaType(); | |
1840 meta_type->setTypeEntry(type); | |
1841 meta_type->setIndirections(typeInfo.indirections); | |
1842 meta_type->setReference(typeInfo.is_reference); | |
1843 meta_type->setConstant(typeInfo.is_constant); | |
1844 meta_type->setOriginalTypeDescription(_typei.toString()); | |
1845 decideUsagePattern(meta_type); | |
1846 | |
1847 if (meta_type->typeEntry()->isContainer()) { | |
1848 ContainerTypeEntry::Type container_type = static_cast<const ContainerTypeEntry *>(type)->type(); | |
1849 | |
1850 if (container_type == ContainerTypeEntry::StringListContainer) { | |
1851 TypeInfo info; | |
1852 info.setQualifiedName(QStringList() << "QString"); | |
1853 AbstractMetaType *targ_type = translateType(info, ok); | |
1854 | |
1855 Q_ASSERT(*ok); | |
1856 Q_ASSERT(targ_type); | |
1857 | |
1858 meta_type->addInstantiation(targ_type); | |
1859 meta_type->setInstantiationInCpp(false); | |
1860 | |
1861 } else { | |
1862 foreach (const TypeParser::Info &ta, typeInfo.template_instantiations) { | |
1863 TypeInfo info; | |
1864 info.setConstant(ta.is_constant); | |
1865 info.setReference(ta.is_reference); | |
1866 info.setIndirections(ta.indirections); | |
1867 | |
1868 info.setFunctionPointer(false); | |
1869 info.setQualifiedName(ta.instantiationName().split("::")); | |
1870 | |
1871 AbstractMetaType *targ_type = translateType(info, ok); | |
1872 if (!(*ok)) { | |
1873 delete meta_type; | |
1874 return 0; | |
1875 } | |
1876 | |
1877 meta_type->addInstantiation(targ_type); | |
1878 } | |
1879 } | |
1880 | |
1881 if (container_type == ContainerTypeEntry::ListContainer | |
1882 || container_type == ContainerTypeEntry::VectorContainer | |
1883 || container_type == ContainerTypeEntry::StringListContainer) { | |
1884 Q_ASSERT(meta_type->instantiations().size() == 1); | |
1885 } | |
1886 } | |
1887 | |
1888 return meta_type; | |
1889 } | |
1890 | |
1891 void AbstractMetaBuilder::decideUsagePattern(AbstractMetaType *meta_type) | |
1892 { | |
1893 const TypeEntry *type = meta_type->typeEntry(); | |
1894 | |
1895 if (type->isPrimitive() && (meta_type->actualIndirections() == 0 | |
1896 || (meta_type->isConstant() && meta_type->isReference() && meta_type->indirections() == 0))) { | |
1897 meta_type->setTypeUsagePattern(AbstractMetaType::PrimitivePattern); | |
1898 | |
1899 } else if (type->isVoid()) { | |
1900 meta_type->setTypeUsagePattern(AbstractMetaType::NativePointerPattern); | |
1901 | |
1902 } else if (type->isString() | |
1903 && meta_type->indirections() == 0 | |
1904 && (meta_type->isConstant() == meta_type->isReference() | |
1905 || meta_type->isConstant())) { | |
1906 meta_type->setTypeUsagePattern(AbstractMetaType::StringPattern); | |
1907 | |
1908 } else if (type->isChar() | |
1909 && meta_type->indirections() == 0 | |
1910 && meta_type->isConstant() == meta_type->isReference()) { | |
1911 meta_type->setTypeUsagePattern(AbstractMetaType::CharPattern); | |
1912 | |
1913 } else if (type->isJObjectWrapper() | |
1914 && meta_type->indirections() == 0 | |
1915 && meta_type->isConstant() == meta_type->isReference()) { | |
1916 meta_type->setTypeUsagePattern(AbstractMetaType::JObjectWrapperPattern); | |
1917 | |
1918 } else if (type->isVariant() | |
1919 && meta_type->indirections() == 0 | |
1920 && meta_type->isConstant() == meta_type->isReference()) { | |
1921 meta_type->setTypeUsagePattern(AbstractMetaType::VariantPattern); | |
1922 | |
1923 } else if (type->isEnum() && meta_type->actualIndirections() == 0) { | |
1924 meta_type->setTypeUsagePattern(AbstractMetaType::EnumPattern); | |
1925 | |
1926 } else if (type->isObject() | |
1927 && meta_type->indirections() == 0 | |
1928 && meta_type->isReference()) { | |
1929 if (((ComplexTypeEntry *) type)->isQObject()) | |
1930 meta_type->setTypeUsagePattern(AbstractMetaType::QObjectPattern); | |
1931 else | |
1932 meta_type->setTypeUsagePattern(AbstractMetaType::ObjectPattern); | |
1933 | |
1934 } else if (type->isObject() | |
1935 && meta_type->indirections() == 1) { | |
1936 if (((ComplexTypeEntry *) type)->isQObject()) | |
1937 meta_type->setTypeUsagePattern(AbstractMetaType::QObjectPattern); | |
1938 else | |
1939 meta_type->setTypeUsagePattern(AbstractMetaType::ObjectPattern); | |
1940 | |
1941 // const-references to pointers can be passed as pointers | |
1942 if (meta_type->isReference() && meta_type->isConstant()) { | |
1943 meta_type->setReference(false); | |
1944 meta_type->setConstant(false); | |
1945 } | |
1946 | |
1947 } else if (type->isContainer() && meta_type->indirections() == 0) { | |
1948 meta_type->setTypeUsagePattern(AbstractMetaType::ContainerPattern); | |
1949 | |
1950 } else if (type->isTemplateArgument()) { | |
1951 | |
1952 } else if (type->isFlags() | |
1953 && meta_type->indirections() == 0 | |
1954 && (meta_type->isConstant() == meta_type->isReference())) { | |
1955 meta_type->setTypeUsagePattern(AbstractMetaType::FlagsPattern); | |
1956 | |
1957 } else if (type->isArray()) { | |
1958 meta_type->setTypeUsagePattern(AbstractMetaType::ArrayPattern); | |
1959 | |
1960 } else if (type->isThread()) { | |
1961 Q_ASSERT(meta_type->indirections() == 1); | |
1962 meta_type->setTypeUsagePattern(AbstractMetaType::ThreadPattern); | |
1963 | |
1964 } else if (type->isValue() | |
1965 && meta_type->indirections() == 0 | |
1966 && (meta_type->isConstant() == meta_type->isReference() | |
1967 || !meta_type->isReference())) { | |
1968 meta_type->setTypeUsagePattern(AbstractMetaType::ValuePattern); | |
1969 | |
1970 } else if (type->isObject() && meta_type->actualIndirections() == 0) { | |
1971 | |
1972 ReportHandler::warning(QString("Object type '%1' passed as value. Resulting code will not compile.") | |
1973 .arg(meta_type->cppSignature())); | |
1974 meta_type->setTypeUsagePattern(AbstractMetaType::NativePointerPattern); | |
1975 | |
1976 } else { | |
1977 meta_type->setTypeUsagePattern(AbstractMetaType::NativePointerPattern); | |
1978 ReportHandler::debugFull(QString("native pointer pattern for '%1'") | |
1979 .arg(meta_type->cppSignature())); | |
1980 } | |
1981 } | |
1982 | |
1983 QString AbstractMetaBuilder::translateDefaultValue(ArgumentModelItem item, AbstractMetaType *type, | |
1984 AbstractMetaFunction *fnc, AbstractMetaClass *implementing_class, | |
1985 int argument_index) | |
1986 { | |
1987 QString function_name = fnc->name(); | |
1988 QString class_name = implementing_class->name(); | |
1989 | |
1990 QString replaced_expression = fnc->replacedDefaultExpression(implementing_class, argument_index + 1); | |
1991 if (fnc->removedDefaultExpression(implementing_class, argument_index +1)) | |
1992 return ""; | |
1993 if (!replaced_expression.isEmpty()) | |
1994 return replaced_expression; | |
1995 | |
1996 QString expr = item->defaultValueExpression(); | |
1997 if (type->isPrimitive()) { | |
1998 if (type->name() == "boolean") { | |
1999 if (expr == "false" || expr=="true") { | |
2000 return expr; | |
2001 } else { | |
2002 bool ok = false; | |
2003 int number = expr.toInt(&ok); | |
2004 if (ok && number) | |
2005 return "true"; | |
2006 else | |
2007 return "false"; | |
2008 } | |
2009 } else if (expr == "ULONG_MAX") { | |
2010 return "Long.MAX_VALUE"; | |
2011 } else if (expr == "QVariant::Invalid") { | |
2012 return QString::number(QVariant::Invalid); | |
2013 } else { | |
2014 // This can be an enum or flag so I need to delay the | |
2015 // translation untill all namespaces are completly | |
2016 // processed. This is done in figureOutEnumValues() | |
2017 return expr; | |
2018 } | |
2019 } else if (type != 0 && (type->isFlags() || type->isEnum())) { | |
2020 // Same as with enum explanation above... | |
2021 return expr; | |
2022 | |
2023 } else { | |
2024 | |
2025 // constructor or functioncall can be a bit tricky... | |
2026 if (expr == "QVariant()" /* qtd || expr == "QModelIndex()"*/) { | |
2027 return "null"; | |
2028 } else if (expr == "QString()") { | |
2029 return "null"; | |
2030 } else if (expr.endsWith(")") && expr.contains("::")) { | |
2031 TypeEntry *typeEntry = TypeDatabase::instance()->findType(expr.left(expr.indexOf("::"))); | |
2032 if (typeEntry) | |
2033 return typeEntry->qualifiedTargetLangName() + "." + expr.right(expr.length() - expr.indexOf("::") - 2); | |
2034 } else if (expr.endsWith(")") && type->isValue()) { | |
2035 int pos = expr.indexOf("("); | |
2036 | |
2037 TypeEntry *typeEntry = TypeDatabase::instance()->findType(expr.left(pos)); | |
2038 if (typeEntry) { // qtd | |
2039 if(typeEntry->isStructInD()) | |
2040 return typeEntry->qualifiedCppName() + expr.right(expr.length() - pos); | |
2041 else | |
2042 return "new " + typeEntry->qualifiedCppName() + expr.right(expr.length() - pos); | |
2043 } else | |
2044 return expr; | |
2045 } else if (expr == "0") { | |
2046 return "null"; | |
2047 } else if (type->isObject() || type->isValue() || expr.contains("::")) { // like Qt::black passed to a QColor | |
2048 TypeEntry *typeEntry = TypeDatabase::instance()->findType(expr.left(expr.indexOf("::"))); | |
2049 | |
2050 expr = expr.right(expr.length() - expr.indexOf("::") - 2); | |
2051 if (typeEntry) { | |
2052 return "new " + type->typeEntry()->qualifiedCppName() + | |
2053 "(" + typeEntry->qualifiedCppName() + "." + expr + ")"; | |
2054 } | |
2055 } | |
2056 } | |
2057 | |
2058 QString warn = QString("unsupported default value '%3' of argument in function '%1', class '%2'") | |
2059 .arg(function_name).arg(class_name).arg(item->defaultValueExpression()); | |
2060 ReportHandler::warning(warn); | |
2061 | |
2062 return QString(); | |
2063 } | |
2064 | |
2065 | |
2066 bool AbstractMetaBuilder::isQObject(const QString &qualified_name) | |
2067 { | |
2068 if (qualified_name == "QObject") | |
2069 return true; | |
2070 | |
2071 ClassModelItem class_item = m_dom->findClass(qualified_name); | |
2072 | |
2073 if (!class_item) { | |
2074 QStringList names = qualified_name.split(QLatin1String("::")); | |
2075 NamespaceModelItem ns = model_dynamic_cast<NamespaceModelItem>(m_dom); | |
2076 for (int i=0; i<names.size() - 1 && ns; ++i) | |
2077 ns = ns->namespaceMap().value(names.at(i)); | |
2078 if (ns && names.size() >= 2) | |
2079 class_item = ns->findClass(names.at(names.size() - 1)); | |
2080 } | |
2081 | |
2082 bool isqobject = class_item && class_item->extendsClass("QObject"); | |
2083 | |
2084 if (class_item && !isqobject) { | |
2085 QStringList baseClasses = class_item->baseClasses(); | |
2086 for (int i=0; i<baseClasses.count(); ++i) { | |
2087 | |
2088 isqobject = isQObject(baseClasses.at(i)); | |
2089 if (isqobject) | |
2090 break; | |
2091 } | |
2092 } | |
2093 | |
2094 return isqobject; | |
2095 } | |
2096 | |
2097 | |
2098 bool AbstractMetaBuilder::isEnum(const QStringList &qualified_name) | |
2099 { | |
2100 CodeModelItem item = m_dom->model()->findItem(qualified_name, m_dom->toItem()); | |
2101 return item && item->kind() == _EnumModelItem::__node_kind; | |
2102 } | |
2103 | |
2104 AbstractMetaType *AbstractMetaBuilder::inheritTemplateType(const QList<AbstractMetaType *> &template_types, | |
2105 AbstractMetaType *meta_type, bool *ok) | |
2106 { | |
2107 if (ok != 0) | |
2108 *ok = true; | |
2109 if (!meta_type || (!meta_type->typeEntry()->isTemplateArgument() && !meta_type->hasInstantiations())) | |
2110 return meta_type ? meta_type->copy() : 0; | |
2111 | |
2112 AbstractMetaType *returned = meta_type->copy(); | |
2113 returned->setOriginalTemplateType(meta_type->copy()); | |
2114 | |
2115 if (returned->typeEntry()->isTemplateArgument()) { | |
2116 const TemplateArgumentEntry *tae = static_cast<const TemplateArgumentEntry *>(returned->typeEntry()); | |
2117 | |
2118 // If the template is intantiated with void we special case this as rejecting the functions that use this | |
2119 // parameter from the instantiation. | |
2120 if (template_types.size() <= tae->ordinal() || template_types.at(tae->ordinal())->typeEntry()->name() == "void") { | |
2121 if (ok != 0) | |
2122 *ok = false; | |
2123 return 0; | |
2124 } | |
2125 | |
2126 AbstractMetaType *t = returned->copy(); | |
2127 t->setTypeEntry(template_types.at(tae->ordinal())->typeEntry()); | |
2128 t->setIndirections(template_types.at(tae->ordinal())->indirections() + t->indirections() | |
2129 ? 1 | |
2130 : 0); | |
2131 decideUsagePattern(t); | |
2132 | |
2133 delete returned; | |
2134 returned = inheritTemplateType(template_types, t, ok); | |
2135 if (ok != 0 && !(*ok)) | |
2136 return 0; | |
2137 } | |
2138 | |
2139 if (returned->hasInstantiations()) { | |
2140 QList<AbstractMetaType *> instantiations = returned->instantiations(); | |
2141 for (int i=0; i<instantiations.count(); ++i) { | |
2142 instantiations[i] = inheritTemplateType(template_types, instantiations.at(i), ok); | |
2143 if (ok != 0 && !(*ok)) | |
2144 return 0; | |
2145 } | |
2146 returned->setInstantiations(instantiations); | |
2147 } | |
2148 | |
2149 return returned; | |
2150 } | |
2151 | |
2152 bool AbstractMetaBuilder::inheritTemplate(AbstractMetaClass *subclass, | |
2153 const AbstractMetaClass *template_class, | |
2154 const TypeParser::Info &info) | |
2155 { | |
2156 QList<TypeParser::Info> targs = info.template_instantiations; | |
2157 | |
2158 QList<AbstractMetaType *> template_types; | |
2159 foreach (const TypeParser::Info &i, targs) { | |
2160 TypeEntry *t = TypeDatabase::instance()->findType(i.qualified_name.join("::")); | |
2161 | |
2162 if (t != 0) { | |
2163 AbstractMetaType *temporary_type = createMetaType(); | |
2164 temporary_type->setTypeEntry(t); | |
2165 temporary_type->setConstant(i.is_constant); | |
2166 temporary_type->setReference(i.is_reference); | |
2167 temporary_type->setIndirections(i.indirections); | |
2168 template_types << temporary_type; | |
2169 } | |
2170 } | |
2171 | |
2172 AbstractMetaFunctionList funcs = subclass->functions(); | |
2173 foreach (const AbstractMetaFunction *function, template_class->functions()) { | |
2174 | |
2175 if (function->isModifiedRemoved(TypeSystem::All)) | |
2176 continue; | |
2177 | |
2178 AbstractMetaFunction *f = function->copy(); | |
2179 f->setArguments(AbstractMetaArgumentList()); | |
2180 | |
2181 bool ok = true; | |
2182 AbstractMetaType *ftype = function->type(); | |
2183 f->setType(inheritTemplateType(template_types, ftype, &ok)); | |
2184 if (!ok) { | |
2185 delete f; | |
2186 continue; | |
2187 } | |
2188 | |
2189 foreach (AbstractMetaArgument *argument, function->arguments()) { | |
2190 AbstractMetaType *atype = argument->type(); | |
2191 | |
2192 AbstractMetaArgument *arg = argument->copy(); | |
2193 arg->setType(inheritTemplateType(template_types, atype, &ok)); | |
2194 if (!ok) | |
2195 break; | |
2196 f->addArgument(arg); | |
2197 } | |
2198 | |
2199 if (!ok) { | |
2200 delete f; | |
2201 continue ; | |
2202 } | |
2203 | |
2204 // There is no base class in java to inherit from here, so the | |
2205 // template instantiation is the class that implements the function.. | |
2206 f->setImplementingClass(subclass); | |
2207 | |
2208 // We also set it as the declaring class, since the superclass is | |
2209 // supposed to disappear. This allows us to make certain function modifications | |
2210 // on the inherited functions. | |
2211 f->setDeclaringClass(subclass); | |
2212 | |
2213 | |
2214 if (f->isConstructor() && subclass->isTypeAlias()) { | |
2215 f->setName(subclass->name()); | |
2216 } else if (f->isConstructor()) { | |
2217 delete f; | |
2218 continue; | |
2219 } | |
2220 | |
2221 // if the instantiation has a function named the same as an existing | |
2222 // function we have shadowing so we need to skip it. | |
2223 bool found = false; | |
2224 for (int i=0; i<funcs.size(); ++i) { | |
2225 if (funcs.at(i)->name() == f->name()) { | |
2226 found = true; | |
2227 continue; | |
2228 } | |
2229 } | |
2230 if (found) { | |
2231 delete f; | |
2232 continue; | |
2233 } | |
2234 | |
2235 ComplexTypeEntry *te = subclass->typeEntry(); | |
2236 FunctionModificationList mods = function->modifications(template_class); | |
2237 for (int i=0; i<mods.size(); ++i) { | |
2238 FunctionModification mod = mods.at(i); | |
2239 mod.signature = f->minimalSignature(); | |
2240 | |
2241 // If we ever need it... Below is the code to do | |
2242 // substitution of the template instantation type inside | |
2243 // injected code.. | |
2244 #if 0 | |
2245 if (mod.modifiers & Modification::CodeInjection) { | |
2246 for (int j=0; j<template_types.size(); ++j) { | |
2247 CodeSnip &snip = mod.snips.last(); | |
2248 QString code = snip.code(); | |
2249 code.replace(QString::fromLatin1("$$QT_TEMPLATE_%1$$").arg(j), | |
2250 template_types.at(j)->typeEntry()->qualifiedCppName()); | |
2251 snip.codeList.clear(); | |
2252 snip.addCode(code); | |
2253 } | |
2254 } | |
2255 #endif | |
2256 te->addFunctionModification(mod); | |
2257 } | |
2258 | |
2259 subclass->addFunction(f); | |
2260 } | |
2261 | |
2262 // Clean up | |
2263 foreach (AbstractMetaType *type, template_types) { | |
2264 delete type; | |
2265 } | |
2266 | |
2267 | |
2268 { | |
2269 subclass->setTemplateBaseClass(template_class); | |
2270 | |
2271 subclass->setInterfaces(template_class->interfaces()); | |
2272 subclass->setBaseClass(template_class->baseClass()); | |
2273 } | |
2274 | |
2275 return true; | |
2276 } | |
2277 | |
2278 void AbstractMetaBuilder::parseQ_Property(AbstractMetaClass *meta_class, const QStringList &declarations) | |
2279 { | |
2280 for (int i=0; i<declarations.size(); ++i) { | |
2281 QString p = declarations.at(i); | |
2282 | |
2283 QStringList l = p.split(QLatin1String(" ")); | |
2284 | |
2285 | |
2286 QStringList qualifiedScopeName = currentScope()->qualifiedName(); | |
2287 bool ok = false; | |
2288 AbstractMetaType *type = 0; | |
2289 QString scope; | |
2290 for (int j=qualifiedScopeName.size(); j>=0; --j) { | |
2291 scope = j > 0 ? QStringList(qualifiedScopeName.mid(0, j)).join("::") + "::" : QString(); | |
2292 TypeInfo info; | |
2293 info.setQualifiedName((scope + l.at(0)).split("::")); | |
2294 | |
2295 type = translateType(info, &ok); | |
2296 if (type != 0 && ok) { | |
2297 break; | |
2298 } | |
2299 } | |
2300 | |
2301 if (type == 0 || !ok) { | |
2302 ReportHandler::warning(QString("Unable to decide type of property: '%1' in class '%2'") | |
2303 .arg(l.at(0)).arg(meta_class->name())); | |
2304 continue; | |
2305 } | |
2306 | |
2307 QString typeName = scope + l.at(0); | |
2308 | |
2309 QPropertySpec *spec = new QPropertySpec(type->typeEntry()); | |
2310 spec->setName(l.at(1)); | |
2311 spec->setIndex(i); | |
2312 | |
2313 for (int pos=2; pos+1<l.size(); pos+=2) { | |
2314 if (l.at(pos) == QLatin1String("READ")) | |
2315 spec->setRead(l.at(pos+1)); | |
2316 else if (l.at(pos) == QLatin1String("WRITE")) | |
2317 spec->setWrite(l.at(pos+1)); | |
2318 else if (l.at(pos) == QLatin1String("DESIGNABLE")) | |
2319 spec->setDesignable(l.at(pos+1)); | |
2320 else if (l.at(pos) == QLatin1String("RESET")) | |
2321 spec->setReset(l.at(pos+1)); | |
2322 } | |
2323 | |
2324 meta_class->addPropertySpec(spec); | |
2325 delete type; | |
2326 } | |
2327 } | |
2328 | |
2329 static void hide_functions(const AbstractMetaFunctionList &l) { | |
2330 foreach (AbstractMetaFunction *f, l) { | |
2331 FunctionModification mod; | |
2332 mod.signature = f->minimalSignature(); | |
2333 mod.modifiers = FunctionModification::Private; | |
2334 ((ComplexTypeEntry *) f->implementingClass()->typeEntry())->addFunctionModification(mod); | |
2335 } | |
2336 } | |
2337 | |
2338 static void remove_function(AbstractMetaFunction *f) { | |
2339 FunctionModification mod; | |
2340 mod.removal = TypeSystem::All; | |
2341 mod.signature = f->minimalSignature(); | |
2342 ((ComplexTypeEntry *) f->implementingClass()->typeEntry())->addFunctionModification(mod); | |
2343 } | |
2344 | |
2345 static AbstractMetaFunctionList filter_functions(const AbstractMetaFunctionList &lst, QSet<QString> *signatures) | |
2346 { | |
2347 AbstractMetaFunctionList functions; | |
2348 foreach (AbstractMetaFunction *f, lst) { | |
2349 QString signature = f->minimalSignature(); | |
2350 int start = signature.indexOf(QLatin1Char('(')) + 1; | |
2351 int end = signature.lastIndexOf(QLatin1Char(')')); | |
2352 signature = signature.mid(start, end - start); | |
2353 if (signatures->contains(signature)) { | |
2354 remove_function(f); | |
2355 continue; | |
2356 } | |
2357 (*signatures) << signature; | |
2358 functions << f; | |
2359 } | |
2360 return functions; | |
2361 } | |
2362 | |
2363 void AbstractMetaBuilder::setupEquals(AbstractMetaClass *cls) | |
2364 { | |
2365 AbstractMetaFunctionList equals; | |
2366 AbstractMetaFunctionList nequals; | |
2367 | |
2368 QString op_equals = QLatin1String("operator_equal"); | |
2369 QString op_nequals = QLatin1String("operator_not_equal"); | |
2370 | |
2371 AbstractMetaFunctionList functions = cls->queryFunctions(AbstractMetaClass::ClassImplements | |
2372 | AbstractMetaClass::NotRemovedFromTargetLang); | |
2373 foreach (AbstractMetaFunction *f, functions) { | |
2374 if (f->name() == op_equals) | |
2375 equals << f; | |
2376 else if (f->name() == op_nequals) | |
2377 nequals << f; | |
2378 } | |
2379 | |
2380 if (equals.size() || nequals.size()) { | |
2381 if (!cls->hasHashFunction()) { | |
2382 ReportHandler::warning(QString::fromLatin1("Class '%1' has equals operators but no qHash() function") | |
2383 .arg(cls->name())); | |
2384 } | |
2385 | |
2386 hide_functions(equals); | |
2387 hide_functions(nequals); | |
2388 | |
2389 // We only need == if we have both == and !=, and one == for | |
2390 // each signature type, like QDateTime::==(QDate) and (QTime) | |
2391 // if such a thing exists... | |
2392 QSet<QString> func_signatures; | |
2393 cls->setEqualsFunctions(filter_functions(equals, &func_signatures)); | |
2394 cls->setNotEqualsFunctions(filter_functions(nequals, &func_signatures)); | |
2395 } | |
2396 } | |
2397 | |
2398 void AbstractMetaBuilder::setupComparable(AbstractMetaClass *cls) | |
2399 { | |
2400 AbstractMetaFunctionList greater; | |
2401 AbstractMetaFunctionList greaterEquals; | |
2402 AbstractMetaFunctionList less; | |
2403 AbstractMetaFunctionList lessEquals; | |
2404 | |
2405 QString op_greater = QLatin1String("operator_greater"); | |
2406 QString op_greater_eq = QLatin1String("operator_greater_or_equal"); | |
2407 QString op_less = QLatin1String("operator_less"); | |
2408 QString op_less_eq = QLatin1String("operator_less_or_equal"); | |
2409 | |
2410 AbstractMetaFunctionList functions = cls->queryFunctions(AbstractMetaClass::ClassImplements | |
2411 | AbstractMetaClass::NotRemovedFromTargetLang); | |
2412 foreach (AbstractMetaFunction *f, functions) { | |
2413 if (f->name() == op_greater) | |
2414 greater << f; | |
2415 else if (f->name() == op_greater_eq) | |
2416 greaterEquals << f; | |
2417 else if (f->name() == op_less) | |
2418 less << f; | |
2419 else if (f->name() == op_less_eq) | |
2420 lessEquals << f; | |
2421 } | |
2422 | |
2423 bool hasEquals = cls->equalsFunctions().size() || cls->notEqualsFunctions().size(); | |
2424 | |
2425 // Conditions for comparable is: | |
2426 // >, ==, < - The basic case | |
2427 // >, == - Less than becomes else case | |
2428 // <, == - Greater than becomes else case | |
2429 // >=, <= - if (<= && >=) -> equal | |
2430 bool mightBeComparable = greater.size() || greaterEquals.size() || less.size() || lessEquals.size() | |
2431 || greaterEquals.size() == 1 || lessEquals.size() == 1; | |
2432 | |
2433 if (mightBeComparable) { | |
2434 QSet<QString> signatures; | |
2435 | |
2436 // We only hide the original functions if we are able to make a compareTo() method | |
2437 bool wasComparable = false; | |
2438 | |
2439 // The three upper cases, prefer the <, == approach | |
2440 if (hasEquals && (greater.size() || less.size())) { | |
2441 cls->setLessThanFunctions(filter_functions(less, &signatures)); | |
2442 cls->setGreaterThanFunctions(filter_functions(greater, &signatures)); | |
2443 filter_functions(greaterEquals, &signatures); | |
2444 filter_functions(lessEquals, &signatures); | |
2445 wasComparable = true; | |
2446 } else if (hasEquals && (greaterEquals.size() || lessEquals.size())) { | |
2447 cls->setLessThanEqFunctions(filter_functions(lessEquals, &signatures)); | |
2448 cls->setGreaterThanEqFunctions(filter_functions(greaterEquals, &signatures)); | |
2449 wasComparable = true; | |
2450 } else if (greaterEquals.size() == 1 || lessEquals.size() == 1) { | |
2451 cls->setGreaterThanEqFunctions(greaterEquals); | |
2452 cls->setLessThanEqFunctions(lessEquals); | |
2453 filter_functions(less, &signatures); | |
2454 filter_functions(greater, &signatures); | |
2455 wasComparable = true; | |
2456 } | |
2457 | |
2458 if (wasComparable) { | |
2459 hide_functions(greater); | |
2460 hide_functions(greaterEquals); | |
2461 hide_functions(less); | |
2462 hide_functions(lessEquals); | |
2463 } | |
2464 } | |
2465 | |
2466 } | |
2467 | |
2468 void AbstractMetaBuilder::setupClonable(AbstractMetaClass *cls) | |
2469 { | |
2470 // All value types are required to have a copy constructor, | |
2471 // or they will not work as value types (it won't even compile, | |
2472 // because of calls to qRegisterMetaType(). Thus all value types | |
2473 // should be cloneable. | |
2474 if (cls->typeEntry()->isValue()) { | |
2475 cls->setHasCloneOperator(true); | |
2476 return; | |
2477 } else { | |
2478 QString op_assign = QLatin1String("operator_assign"); | |
2479 | |
2480 AbstractMetaFunctionList functions = cls->queryFunctions(AbstractMetaClass::ClassImplements); | |
2481 foreach (AbstractMetaFunction *f, functions) { | |
2482 if ((f->name() == op_assign || f->isConstructor()) && f->isPublic()) { | |
2483 AbstractMetaArgumentList arguments = f->arguments(); | |
2484 if (f->actualMinimumArgumentCount() == 1) { | |
2485 if (cls->typeEntry()->qualifiedCppName() == arguments.at(0)->type()->typeEntry()->qualifiedCppName()) { | |
2486 if (cls->typeEntry()->isValue()) { | |
2487 cls->setHasCloneOperator(true); | |
2488 return; | |
2489 } | |
2490 } | |
2491 } | |
2492 } | |
2493 } | |
2494 } | |
2495 } | |
2496 | |
2497 static void write_reject_log_file(const QString &name, | |
2498 const QMap<QString, AbstractMetaBuilder::RejectReason> &rejects) | |
2499 { | |
2500 QFile f(name); | |
2501 if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) { | |
2502 ReportHandler::warning(QString("failed to write log file: '%1'") | |
2503 .arg(f.fileName())); | |
2504 return; | |
2505 } | |
2506 | |
2507 QTextStream s(&f); | |
2508 | |
2509 | |
2510 for (int reason=0; reason<AbstractMetaBuilder::NoReason; ++reason) { | |
2511 s << QString(72, '*') << endl; | |
2512 switch (reason) { | |
2513 case AbstractMetaBuilder::NotInTypeSystem: | |
2514 s << "Not in type system"; | |
2515 break; | |
2516 case AbstractMetaBuilder::GenerationDisabled: | |
2517 s << "Generation disabled by type system"; | |
2518 break; | |
2519 case AbstractMetaBuilder::RedefinedToNotClass: | |
2520 s << "Type redefined to not be a class"; | |
2521 break; | |
2522 | |
2523 case AbstractMetaBuilder::UnmatchedReturnType: | |
2524 s << "Unmatched return type"; | |
2525 break; | |
2526 | |
2527 case AbstractMetaBuilder::UnmatchedArgumentType: | |
2528 s << "Unmatched argument type"; | |
2529 break; | |
2530 | |
2531 default: | |
2532 s << "unknown reason"; | |
2533 break; | |
2534 } | |
2535 | |
2536 s << endl; | |
2537 | |
2538 for (QMap<QString, AbstractMetaBuilder::RejectReason>::const_iterator it = rejects.constBegin(); | |
2539 it != rejects.constEnd(); ++it) { | |
2540 if (it.value() != reason) | |
2541 continue; | |
2542 s << " - " << it.key() << endl; | |
2543 } | |
2544 | |
2545 s << QString(72, '*') << endl << endl; | |
2546 } | |
2547 | |
2548 } | |
2549 | |
2550 | |
2551 void AbstractMetaBuilder::dumpLog() | |
2552 { | |
2553 write_reject_log_file("mjb_rejected_classes.log", m_rejected_classes); | |
2554 write_reject_log_file("mjb_rejected_enums.log", m_rejected_enums); | |
2555 write_reject_log_file("mjb_rejected_functions.log", m_rejected_functions); | |
2556 write_reject_log_file("mjb_rejected_fields.log", m_rejected_fields); | |
2557 } | |
2558 | |
2559 AbstractMetaClassList AbstractMetaBuilder::classesTopologicalSorted() const | |
2560 { | |
2561 AbstractMetaClassList res; | |
2562 | |
2563 AbstractMetaClassList classes = m_meta_classes; | |
2564 qSort(classes); | |
2565 | |
2566 QSet<AbstractMetaClass*> noDependency; | |
2567 QHash<AbstractMetaClass*, QSet<AbstractMetaClass* >* > hash; | |
2568 foreach (AbstractMetaClass *cls, classes) { | |
2569 QSet<AbstractMetaClass* > *depends = new QSet<AbstractMetaClass* >(); | |
2570 | |
2571 if (cls->baseClass()) | |
2572 depends->insert(cls->baseClass()); | |
2573 | |
2574 foreach (AbstractMetaClass *interface, cls->interfaces()) { | |
2575 depends->insert(interface); | |
2576 } | |
2577 | |
2578 if (depends->empty()) { | |
2579 noDependency.insert(cls); | |
2580 } else { | |
2581 hash.insert(cls, depends); | |
2582 } | |
2583 } | |
2584 | |
2585 while (!noDependency.empty()) { | |
2586 foreach (AbstractMetaClass *cls, noDependency.values()) { | |
2587 if(!cls->isInterface()) | |
2588 res.append(cls); | |
2589 noDependency.remove(cls); | |
2590 QHashIterator<AbstractMetaClass*, QSet<AbstractMetaClass* >* > i(hash); | |
2591 while (i.hasNext()) { | |
2592 i.next(); | |
2593 i.value()->remove(cls); | |
2594 if (i.value()->empty()) { | |
2595 AbstractMetaClass *key = i.key(); | |
2596 noDependency.insert(key); | |
2597 hash.remove(key); | |
2598 delete(i.value()); | |
2599 } | |
2600 } | |
2601 } | |
2602 } | |
2603 | |
2604 if (!noDependency.empty() || !hash.empty()) { | |
2605 qWarning("dependency graph was cyclic."); | |
2606 } | |
2607 | |
2608 return res; | |
2609 } |