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