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