1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the tools applications of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include "generator.h"
44 #include "qdatetime.h"
46 #include "outputrevision.h"
47 #include <QtCore/qfile.h>
48 #include <QtCore/qfileinfo.h>
49 #include <QtCore/qdir.h>
51 // for normalizeTypeInternal
52 #include <private/qmetaobject_moc_p.h>
56 // only moc needs this function
57 static QByteArray normalizeType(const QByteArray &ba, bool fixScope = false)
59 const char *s = ba.constData();
62 char *buf = (len >= 64 ? new char[len + 1] : stackbuf);
65 while(*s && is_space(*s))
68 while (*s && !is_space(*s))
70 while (*s && is_space(*s))
72 if (*s && ((is_ident_char(*s) && is_ident_char(last))
73 || ((*s == ':') && (last == '<')))) {
79 if (strncmp("void", buf, d - buf) != 0)
80 result = normalizeTypeInternal(buf, d, fixScope);
86 bool Moc::parseClassHead(ClassDef *def)
88 // figure out whether this is a class declaration, or only a
89 // forward or variable declaration.
94 if (token == COLON || token == LBRACE)
96 if (token == SEMIC || token == RANGLE)
100 if (!test(IDENTIFIER)) // typedef struct { ... }
102 QByteArray name = lexem();
104 // support "class IDENT name" and "class IDENT(IDENT) name"
107 if (!test(IDENTIFIER))
110 } else if (test(IDENTIFIER)) {
114 def->qualified += name;
115 while (test(SCOPE)) {
116 def->qualified += lexem();
117 if (test(IDENTIFIER)) {
119 def->qualified += name;
122 def->classname = name;
126 FunctionDef::Access access = FunctionDef::Public;
128 access = FunctionDef::Private;
129 else if (test(PROTECTED))
130 access = FunctionDef::Protected;
134 const QByteArray type = parseType().name;
135 // ignore the 'class Foo : BAR(Baz)' case
139 def->superclassList += qMakePair(type, access);
141 } while (test(COMMA));
145 def->begin = index - 1;
146 bool foundRBrace = until(RBRACE);
148 index = def->begin + 1;
152 Type Moc::parseType()
155 bool hasSignedOrUnsigned = false;
157 type.firstToken = lookup();
162 hasSignedOrUnsigned = true;
166 type.name += lexem();
168 if (lookup(0) == VOLATILE)
169 type.isVolatile = true;
171 case Q_MOC_COMPAT_TOKEN:
172 case Q_INVOKABLE_TOKEN:
173 case Q_SCRIPTABLE_TOKEN:
174 case Q_SIGNALS_TOKEN:
178 type.name += lexem();
186 test(ENUM) || test(CLASS) || test(STRUCT);
190 // void mySlot(unsigned myArg)
191 if (hasSignedOrUnsigned) {
199 type.name += lexem();
200 // preserve '[unsigned] long long', 'short int', 'long int', 'long double'
201 if (test(LONG) || test(INT) || test(DOUBLE)) {
211 type.name += lexem();
212 isVoid |= (lookup(0) == VOID);
219 QByteArray templ = lexemUntil(RANGLE);
220 for (int i = 0; i < templ.size(); ++i) {
221 type.name += templ.at(i);
222 if ((templ.at(i) == '<' && i+1 < templ.size() && templ.at(i+1) == ':')
223 || (templ.at(i) == '>' && i+1 < templ.size() && templ.at(i+1) == '>')) {
229 type.name += lexem();
230 type.isScoped = true;
235 while (test(CONST) || test(VOLATILE) || test(SIGNED) || test(UNSIGNED)
236 || test(STAR) || test(AND) || test(ANDAND)) {
238 type.name += lexem();
239 if (lookup(0) == AND)
240 type.referenceType = Type::Reference;
241 else if (lookup(0) == ANDAND)
242 type.referenceType = Type::RValueReference;
243 else if (lookup(0) == STAR)
244 type.referenceType = Type::Pointer;
246 type.rawName = type.name;
247 // transform stupid things like 'const void' or 'void const' into 'void'
248 if (isVoid && type.referenceType == Type::NoReference) {
254 bool Moc::parseEnum(EnumDef *def)
256 bool isTypdefEnum = false; // typedef enum { ... } Foo;
259 def->isEnumClass = true;
261 if (test(IDENTIFIER)) {
264 if (lookup(-1) != TYPEDEF)
265 return false; // anonymous enum
268 if (test(COLON)) { // C++11 strongly typed enum
269 // enum Foo : unsigned long { ... };
270 parseType(); //ignore the result
275 if (lookup() == RBRACE) // accept trailing comma
278 def->values += lexem();
279 } while (test(EQ) ? until(COMMA) : test(COMMA));
282 if (!test(IDENTIFIER))
289 void Moc::parseFunctionArguments(FunctionDef *def)
294 arg.type = parseType();
295 if (arg.type.name == "void")
297 if (test(IDENTIFIER))
299 while (test(LBRACK)) {
300 arg.rightType += lexemUntil(RBRACK);
302 if (test(CONST) || test(VOLATILE)) {
303 arg.rightType += ' ';
304 arg.rightType += lexem();
306 arg.normalizedType = normalizeType(QByteArray(arg.type.name + ' ' + arg.rightType));
307 arg.typeNameForCast = normalizeType(QByteArray(noRef(arg.type.name) + "(*)" + arg.rightType));
309 arg.isDefault = true;
310 def->arguments += arg;
316 bool Moc::testFunctionAttribute(FunctionDef *def)
318 if (index < symbols.size() && testFunctionAttribute(symbols.at(index).token, def)) {
325 bool Moc::testFunctionAttribute(Token tok, FunctionDef *def)
328 case Q_MOC_COMPAT_TOKEN:
329 def->isCompat = true;
331 case Q_INVOKABLE_TOKEN:
332 def->isInvokable = true;
335 def->isSignal = true;
340 case Q_SCRIPTABLE_TOKEN:
341 def->isInvokable = def->isScriptable = true;
348 bool Moc::testFunctionRevision(FunctionDef *def)
350 if (test(Q_REVISION_TOKEN)) {
352 QByteArray revision = lexemUntil(RPAREN);
353 revision.remove(0, 1);
356 def->revision = revision.toInt(&ok);
357 if (!ok || def->revision < 0)
358 error("Invalid revision");
365 // returns false if the function should be ignored
366 bool Moc::parseFunction(FunctionDef *def, bool inMacro)
368 def->isVirtual = false;
369 def->isStatic = false;
370 //skip modifiers and attributes
371 while (test(INLINE) || (test(STATIC) && (def->isStatic = true)) ||
372 (test(VIRTUAL) && (def->isVirtual = true)) //mark as virtual
373 || testFunctionAttribute(def) || testFunctionRevision(def)) {}
374 bool templateFunction = (lookup() == TEMPLATE);
375 def->type = parseType();
376 if (def->type.name.isEmpty()) {
377 if (templateFunction)
378 error("Template function as signal or slot");
382 bool scopedFunctionName = false;
384 def->name = def->type.name;
385 scopedFunctionName = def->type.isScoped;
386 def->type = Type("int");
388 Type tempType = parseType();;
389 while (!tempType.name.isEmpty() && lookup() != LPAREN) {
390 if (testFunctionAttribute(def->type.firstToken, def))
392 else if (def->type.firstToken == Q_SIGNALS_TOKEN)
394 else if (def->type.firstToken == Q_SLOTS_TOKEN)
397 if (!def->tag.isEmpty())
399 def->tag += def->type.name;
401 def->type = tempType;
402 tempType = parseType();
404 next(LPAREN, "Not a signal or slot declaration");
405 def->name = tempType.name;
406 scopedFunctionName = tempType.isScoped;
409 // we don't support references as return types, it's too dangerous
410 if (def->type.referenceType == Type::Reference) {
411 QByteArray rawName = def->type.rawName;
412 def->type = Type("void");
413 def->type.rawName = rawName;
416 def->normalizedType = normalizeType(def->type.name);
419 parseFunctionArguments(def);
423 // support optional macros with compiler specific options
424 while (test(IDENTIFIER))
427 def->isConst = test(CONST);
429 while (test(IDENTIFIER))
442 else if ((def->inlineCode = test(LBRACE)))
444 else if ((def->isAbstract = test(EQ)))
450 if (scopedFunctionName) {
451 QByteArray msg("Function declaration ");
453 msg += " contains extra qualification. Ignoring as signal or slot.";
454 warning(msg.constData());
460 // like parseFunction, but never aborts with an error
461 bool Moc::parseMaybeFunction(const ClassDef *cdef, FunctionDef *def)
463 def->isVirtual = false;
464 def->isStatic = false;
465 //skip modifiers and attributes
466 while (test(EXPLICIT) || test(INLINE) || (test(STATIC) && (def->isStatic = true)) ||
467 (test(VIRTUAL) && (def->isVirtual = true)) //mark as virtual
468 || testFunctionAttribute(def) || testFunctionRevision(def)) {}
469 bool tilde = test(TILDE);
470 def->type = parseType();
471 if (def->type.name.isEmpty())
473 bool scopedFunctionName = false;
475 def->name = def->type.name;
476 scopedFunctionName = def->type.isScoped;
477 if (def->name == cdef->classname) {
478 def->isDestructor = tilde;
479 def->isConstructor = !tilde;
482 def->type = Type("int");
485 Type tempType = parseType();;
486 while (!tempType.name.isEmpty() && lookup() != LPAREN) {
487 if (testFunctionAttribute(def->type.firstToken, def))
489 else if (def->type.name == "Q_SIGNAL")
490 def->isSignal = true;
491 else if (def->type.name == "Q_SLOT")
494 if (!def->tag.isEmpty())
496 def->tag += def->type.name;
498 def->type = tempType;
499 tempType = parseType();
503 def->name = tempType.name;
504 scopedFunctionName = tempType.isScoped;
507 // we don't support references as return types, it's too dangerous
508 if (def->type.referenceType == Type::Reference) {
509 QByteArray rawName = def->type.rawName;
510 def->type = Type("void");
511 def->type.rawName = rawName;
514 def->normalizedType = normalizeType(def->type.name);
517 parseFunctionArguments(def);
521 def->isConst = test(CONST);
522 if (scopedFunctionName
523 && (def->isSignal || def->isSlot || def->isInvokable)) {
524 QByteArray msg("parsemaybe: Function declaration ");
526 msg += " contains extra qualification. Ignoring as signal or slot.";
527 warning(msg.constData());
536 QList<NamespaceDef> namespaceList;
537 bool templateClass = false;
543 if (test(IDENTIFIER)) {
545 // namespace Foo = Bar::Baz;
547 } else if (!test(SEMIC)) {
551 def.begin = index - 1;
554 index = def.begin + 1;
555 namespaceList += def;
563 templateClass = false;
566 templateClass = true;
568 case MOC_INCLUDE_BEGIN:
569 currentFilenames.push(symbol().unquotedLexem());
571 case MOC_INCLUDE_END:
572 currentFilenames.pop();
574 case Q_DECLARE_INTERFACE_TOKEN:
575 parseDeclareInterface();
577 case Q_DECLARE_METATYPE_TOKEN:
578 parseDeclareMetatype();
581 if (test(NAMESPACE)) {
582 while (test(SCOPE) || test(IDENTIFIER))
589 if (currentFilenames.size() <= 1)
593 if (!parseClassHead(&def))
596 while (inClass(&def) && hasNext()) {
597 if (next() == Q_OBJECT_TOKEN) {
598 def.hasQObject = true;
606 for (int i = namespaceList.size() - 1; i >= 0; --i)
607 if (inNamespace(&namespaceList.at(i)))
608 def.qualified.prepend(namespaceList.at(i).name + "::");
610 knownQObjectClasses.insert(def.classname);
611 knownQObjectClasses.insert(def.qualified);
616 if ((t != CLASS && t != STRUCT)|| currentFilenames.size() > 1)
619 if (parseClassHead(&def)) {
620 FunctionDef::Access access = FunctionDef::Private;
621 for (int i = namespaceList.size() - 1; i >= 0; --i)
622 if (inNamespace(&namespaceList.at(i)))
623 def.qualified.prepend(namespaceList.at(i).name + "::");
624 while (inClass(&def) && hasNext()) {
625 switch ((t = next())) {
627 access = FunctionDef::Private;
628 if (test(Q_SIGNALS_TOKEN))
629 error("Signals cannot have access specifier");
632 access = FunctionDef::Protected;
633 if (test(Q_SIGNALS_TOKEN))
634 error("Signals cannot have access specifier");
637 access = FunctionDef::Public;
638 if (test(Q_SIGNALS_TOKEN))
639 error("Signals cannot have access specifier");
643 if (parseClassHead(&nestedDef)) {
644 while (inClass(&nestedDef) && inClass(&def)) {
646 if (t >= Q_META_TOKEN_BEGIN && t < Q_META_TOKEN_END)
647 error("Meta object features not supported for nested classes");
651 case Q_SIGNALS_TOKEN:
655 switch (lookup(-1)) {
659 parseSlots(&def, access);
662 error("Missing access specifier for slots");
666 def.hasQObject = true;
668 error("Template classes not supported by Q_OBJECT");
669 if (def.classname != "Qt" && def.classname != "QObject" && def.superclassList.isEmpty())
670 error("Class contains Q_OBJECT macro but does not inherit from QObject");
673 def.hasQGadget = true;
675 error("Template classes not supported by Q_GADGET");
677 case Q_PROPERTY_TOKEN:
680 case Q_PLUGIN_METADATA_TOKEN:
681 parsePluginData(&def);
684 parseEnumOrFlag(&def, false);
687 parseEnumOrFlag(&def, true);
689 case Q_DECLARE_FLAGS_TOKEN:
692 case Q_CLASSINFO_TOKEN:
693 parseClassInfo(&def);
695 case Q_INTERFACES_TOKEN:
696 parseInterfaces(&def);
698 case Q_PRIVATE_SLOT_TOKEN:
699 parseSlotInPrivate(&def, access);
701 case Q_PRIVATE_PROPERTY_TOKEN:
702 parsePrivateProperty(&def);
706 if (parseEnum(&enumDef))
707 def.enumList += enumDef;
714 funcDef.access = access;
715 int rewind = index--;
716 if (parseMaybeFunction(&def, &funcDef)) {
717 if (funcDef.isConstructor) {
718 if ((access == FunctionDef::Public) && funcDef.isInvokable) {
719 def.constructorList += funcDef;
720 while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) {
721 funcDef.wasCloned = true;
722 funcDef.arguments.removeLast();
723 def.constructorList += funcDef;
726 } else if (funcDef.isDestructor) {
727 // don't care about destructors
729 if (access == FunctionDef::Public)
730 def.publicList += funcDef;
731 if (funcDef.isSlot) {
732 def.slotList += funcDef;
733 while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) {
734 funcDef.wasCloned = true;
735 funcDef.arguments.removeLast();
736 def.slotList += funcDef;
738 if (funcDef.revision > 0)
739 ++def.revisionedMethods;
740 } else if (funcDef.isSignal) {
741 def.signalList += funcDef;
742 while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) {
743 funcDef.wasCloned = true;
744 funcDef.arguments.removeLast();
745 def.signalList += funcDef;
747 if (funcDef.revision > 0)
748 ++def.revisionedMethods;
749 } else if (funcDef.isInvokable) {
750 def.methodList += funcDef;
751 while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) {
752 funcDef.wasCloned = true;
753 funcDef.arguments.removeLast();
754 def.methodList += funcDef;
756 if (funcDef.revision > 0)
757 ++def.revisionedMethods;
768 if (!def.hasQObject && !def.hasQGadget && def.signalList.isEmpty() && def.slotList.isEmpty()
769 && def.propertyList.isEmpty() && def.enumDeclarations.isEmpty())
770 continue; // no meta object code required
773 if (!def.hasQObject && !def.hasQGadget)
774 error("Class declarations lacks Q_OBJECT macro.");
776 checkSuperClasses(&def);
777 checkProperties(&def);
780 knownQObjectClasses.insert(def.classname);
781 knownQObjectClasses.insert(def.qualified);
786 void Moc::generate(FILE *out)
789 QDateTime dt = QDateTime::currentDateTime();
790 QByteArray dstr = dt.toString().toLatin1();
791 QByteArray fn = filename;
792 int i = filename.length()-1;
793 while (i>0 && filename[i-1] != '/' && filename[i-1] != '\\')
796 fn = filename.mid(i);
797 fprintf(out, "/****************************************************************************\n"
798 "** Meta object code from reading C++ file '%s'\n**\n" , fn.constData());
799 fprintf(out, "** Created: %s\n"
800 "** by: The Qt Meta Object Compiler version %d (Qt %s)\n**\n" , dstr.data(), mocOutputRevision, QT_VERSION_STR);
801 fprintf(out, "** WARNING! All changes made in this file will be lost!\n"
802 "*****************************************************************************/\n\n");
806 if (includePath.size() && !includePath.endsWith('/'))
808 for (int i = 0; i < includeFiles.size(); ++i) {
809 QByteArray inc = includeFiles.at(i);
810 if (inc[0] != '<' && inc[0] != '"') {
811 if (includePath.size() && includePath != "./")
812 inc.prepend(includePath);
813 inc = '\"' + inc + '\"';
815 fprintf(out, "#include %s\n", inc.constData());
818 if (classList.size() && classList.first().classname == "Qt")
819 fprintf(out, "#include <QtCore/qobject.h>\n");
821 fprintf(out, "#include <QtCore/qbytearray.h>\n"); // For QByteArrayData
822 fprintf(out, "#include <QtCore/qmetatype.h>\n"); // For QMetaType::Type
823 if (mustIncludeQPluginH)
824 fprintf(out, "#include <QtCore/qplugin.h>\n");
826 fprintf(out, "#if !defined(Q_MOC_OUTPUT_REVISION)\n"
827 "#error \"The header file '%s' doesn't include <QObject>.\"\n", fn.constData());
828 fprintf(out, "#elif Q_MOC_OUTPUT_REVISION != %d\n", mocOutputRevision);
829 fprintf(out, "#error \"This file was generated using the moc from %s."
830 " It\"\n#error \"cannot be used with the include files from"
831 " this version of Qt.\"\n#error \"(The moc has changed too"
832 " much.)\"\n", QT_VERSION_STR);
833 fprintf(out, "#endif\n\n");
835 fprintf(out, "QT_BEGIN_MOC_NAMESPACE\n");
837 for (i = 0; i < classList.size(); ++i) {
838 Generator generator(&classList[i], metaTypes, out);
839 generator.generateCode();
842 fprintf(out, "QT_END_MOC_NAMESPACE\n");
845 void Moc::parseSlots(ClassDef *def, FunctionDef::Access access)
847 int defaultRevision = -1;
848 if (test(Q_REVISION_TOKEN)) {
850 QByteArray revision = lexemUntil(RPAREN);
851 revision.remove(0, 1);
854 defaultRevision = revision.toInt(&ok);
855 if (!ok || defaultRevision < 0)
856 error("Invalid revision");
860 while (inClass(def) && hasNext()) {
865 case Q_SIGNALS_TOKEN:
875 error("'using' directive not supported in 'slots' section");
881 funcDef.access = access;
882 if (!parseFunction(&funcDef))
884 if (funcDef.revision > 0) {
885 ++def->revisionedMethods;
886 } else if (defaultRevision != -1) {
887 funcDef.revision = defaultRevision;
888 ++def->revisionedMethods;
890 def->slotList += funcDef;
891 while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) {
892 funcDef.wasCloned = true;
893 funcDef.arguments.removeLast();
894 def->slotList += funcDef;
899 void Moc::parseSignals(ClassDef *def)
901 int defaultRevision = -1;
902 if (test(Q_REVISION_TOKEN)) {
904 QByteArray revision = lexemUntil(RPAREN);
905 revision.remove(0, 1);
908 defaultRevision = revision.toInt(&ok);
909 if (!ok || defaultRevision < 0)
910 error("Invalid revision");
914 while (inClass(def) && hasNext()) {
919 case Q_SIGNALS_TOKEN:
929 error("'using' directive not supported in 'signals' section");
934 funcDef.access = FunctionDef::Protected;
935 parseFunction(&funcDef);
936 if (funcDef.isVirtual)
937 warning("Signals cannot be declared virtual");
938 if (funcDef.inlineCode)
939 error("Not a signal declaration");
940 if (funcDef.revision > 0) {
941 ++def->revisionedMethods;
942 } else if (defaultRevision != -1) {
943 funcDef.revision = defaultRevision;
944 ++def->revisionedMethods;
946 def->signalList += funcDef;
947 while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) {
948 funcDef.wasCloned = true;
949 funcDef.arguments.removeLast();
950 def->signalList += funcDef;
955 void Moc::createPropertyDef(PropertyDef &propDef)
957 QByteArray type = parseType().name;
960 propDef.designable = propDef.scriptable = propDef.stored = "true";
961 propDef.user = "false";
963 The Q_PROPERTY construct cannot contain any commas, since
964 commas separate macro arguments. We therefore expect users
965 to type "QMap" instead of "QMap<QString, QVariant>". For
966 coherence, we also expect the same for
967 QValueList<QVariant>, the other template class supported by
970 type = normalizeType(type);
972 type = "QMap<QString,QVariant>";
973 else if (type == "QValueList")
974 type = "QValueList<QVariant>";
975 else if (type == "LongLong")
977 else if (type == "ULongLong")
983 propDef.name = lexem();
984 while (test(IDENTIFIER)) {
985 QByteArray l = lexem();
986 if (l[0] == 'C' && l == "CONSTANT") {
987 propDef.constant = true;
989 } else if(l[0] == 'F' && l == "FINAL") {
990 propDef.final = true;
996 v = lexemUntil(RPAREN);
997 } else if (test(INTEGER_LITERAL)) {
1005 v2 = lexemUntil(RPAREN);
1006 else if (v != "true" && v != "false")
1013 else if (l == "RESET")
1014 propDef.reset = v + v2;
1015 else if (l == "REVISION") {
1017 propDef.revision = v.toInt(&ok);
1018 if (!ok || propDef.revision < 0)
1024 if (l == "SCRIPTABLE")
1025 propDef.scriptable = v + v2;
1026 else if (l == "STORED")
1027 propDef.stored = v + v2;
1031 case 'W': if (l != "WRITE") error(2);
1034 case 'D': if (l != "DESIGNABLE") error(2);
1035 propDef.designable = v + v2;
1037 case 'E': if (l != "EDITABLE") error(2);
1038 propDef.editable = v + v2;
1040 case 'N': if (l != "NOTIFY") error(2);
1043 case 'U': if (l != "USER") error(2);
1044 propDef.user = v + v2;
1050 if (propDef.read.isNull()) {
1052 msg += "Property declaration ";
1053 msg += propDef.name;
1054 msg += " has no READ accessor function. The property will be invalid.";
1055 warning(msg.constData());
1057 if (propDef.constant && !propDef.write.isNull()) {
1059 msg += "Property declaration ";
1060 msg += propDef.name;
1061 msg += " is both WRITEable and CONSTANT. CONSTANT will be ignored.";
1062 propDef.constant = false;
1063 warning(msg.constData());
1065 if (propDef.constant && !propDef.notify.isNull()) {
1067 msg += "Property declaration ";
1068 msg += propDef.name;
1069 msg += " is both NOTIFYable and CONSTANT. CONSTANT will be ignored.";
1070 propDef.constant = false;
1071 warning(msg.constData());
1075 void Moc::parseProperty(ClassDef *def)
1078 PropertyDef propDef;
1079 createPropertyDef(propDef);
1083 if(!propDef.notify.isEmpty())
1084 def->notifyableProperties++;
1085 if (propDef.revision > 0)
1086 ++def->revisionedProperties;
1087 def->propertyList += propDef;
1090 void Moc::parsePluginData(ClassDef *def)
1093 QByteArray metaData;
1094 while (test(IDENTIFIER)) {
1095 QByteArray l = lexem();
1097 next(STRING_LITERAL);
1098 def->pluginData.iid = unquotedLexem();
1099 } else if (l == "FILE") {
1100 next(STRING_LITERAL);
1101 QByteArray metaDataFile = unquotedLexem();
1102 QFileInfo fi(QFileInfo(QString::fromLocal8Bit(currentFilenames.top().constData())).dir(), QString::fromLocal8Bit(metaDataFile.constData()));
1105 msg += "Plugin Metadata file ";
1107 msg += " does not exist. Declaration will be ignored";
1108 warning(msg.constData());
1111 QFile file(fi.canonicalFilePath());
1112 file.open(QFile::ReadOnly);
1113 metaData = file.readAll();
1117 if (!metaData.isEmpty()) {
1118 def->pluginData.metaData = QJsonDocument::fromJson(metaData);
1119 if (!def->pluginData.metaData.isObject()) {
1121 msg += "Plugin Metadata file ";
1123 msg += " does not contain a valid JSON object. Declaration will be ignored";
1124 warning(msg.constData());
1125 def->pluginData.iid = QByteArray();
1130 mustIncludeQPluginH = true;
1134 void Moc::parsePrivateProperty(ClassDef *def)
1137 PropertyDef propDef;
1139 propDef.inPrivateClass = lexem();
1140 while (test(SCOPE)) {
1141 propDef.inPrivateClass += lexem();
1143 propDef.inPrivateClass += lexem();
1145 // also allow void functions
1148 propDef.inPrivateClass += "()";
1153 createPropertyDef(propDef);
1155 if(!propDef.notify.isEmpty())
1156 def->notifyableProperties++;
1157 if (propDef.revision > 0)
1158 ++def->revisionedProperties;
1160 def->propertyList += propDef;
1163 void Moc::parseEnumOrFlag(ClassDef *def, bool isFlag)
1166 QByteArray identifier;
1167 while (test(IDENTIFIER)) {
1168 identifier = lexem();
1169 while (test(SCOPE) && test(IDENTIFIER)) {
1171 identifier += lexem();
1173 def->enumDeclarations[identifier] = isFlag;
1178 void Moc::parseFlag(ClassDef *def)
1181 QByteArray flagName, enumName;
1182 while (test(IDENTIFIER)) {
1184 while (test(SCOPE) && test(IDENTIFIER)) {
1186 flagName += lexem();
1190 while (test(IDENTIFIER)) {
1192 while (test(SCOPE) && test(IDENTIFIER)) {
1194 enumName += lexem();
1198 def->flagAliases.insert(enumName, flagName);
1202 void Moc::parseClassInfo(ClassDef *def)
1205 ClassInfoDef infoDef;
1206 next(STRING_LITERAL);
1207 infoDef.name = symbol().unquotedLexem();
1209 if (test(STRING_LITERAL)) {
1210 infoDef.value = symbol().unquotedLexem();
1212 // support Q_CLASSINFO("help", QT_TR_NOOP("blah"))
1215 next(STRING_LITERAL);
1216 infoDef.value = symbol().unquotedLexem();
1220 def->classInfoList += infoDef;
1223 void Moc::parseInterfaces(ClassDef *def)
1226 while (test(IDENTIFIER)) {
1227 QList<ClassDef::Interface> iface;
1228 iface += ClassDef::Interface(lexem());
1229 while (test(SCOPE)) {
1230 iface.last().className += lexem();
1232 iface.last().className += lexem();
1234 while (test(COLON)) {
1236 iface += ClassDef::Interface(lexem());
1237 while (test(SCOPE)) {
1238 iface.last().className += lexem();
1240 iface.last().className += lexem();
1243 // resolve from classnames to interface ids
1244 for (int i = 0; i < iface.count(); ++i) {
1245 const QByteArray iid = interface2IdMap.value(iface.at(i).className);
1247 error("Undefined interface");
1249 iface[i].interfaceId = iid;
1251 def->interfaceList += iface;
1256 void Moc::parseDeclareInterface()
1259 QByteArray interface;
1261 interface += lexem();
1262 while (test(SCOPE)) {
1263 interface += lexem();
1265 interface += lexem();
1269 if (test(STRING_LITERAL)) {
1275 interface2IdMap.insert(interface, iid);
1279 void Moc::parseDeclareMetatype()
1282 QByteArray typeName = lexemUntil(RPAREN);
1283 typeName.remove(0, 1);
1285 metaTypes.append(typeName);
1288 void Moc::parseSlotInPrivate(ClassDef *def, FunctionDef::Access access)
1291 FunctionDef funcDef;
1293 funcDef.inPrivateClass = lexem();
1294 // also allow void functions
1297 funcDef.inPrivateClass += "()";
1300 funcDef.access = access;
1301 parseFunction(&funcDef, true);
1302 def->slotList += funcDef;
1303 while (funcDef.arguments.size() > 0 && funcDef.arguments.last().isDefault) {
1304 funcDef.wasCloned = true;
1305 funcDef.arguments.removeLast();
1306 def->slotList += funcDef;
1308 if (funcDef.revision > 0)
1309 ++def->revisionedMethods;
1313 QByteArray Moc::lexemUntil(Token target)
1318 while (from <= index) {
1319 QByteArray n = symbols.at(from++-1).lexem();
1320 if (s.size() && n.size()
1321 && is_ident_char(s.at(s.size()-1))
1322 && is_ident_char(n.at(0)))
1329 bool Moc::until(Token target) {
1335 switch(symbols.at(index-1).token) {
1336 case LBRACE: ++braceCount; break;
1337 case LBRACK: ++brackCount; break;
1338 case LPAREN: ++parenCount; break;
1339 case LANGLE: ++angleCount; break;
1344 //when searching commas within the default argument, we should take care of template depth (anglecount)
1345 // unfortunatelly, we do not have enough semantic information to know if '<' is the operator< or
1346 // the beginning of a template type. so we just use heuristics.
1349 while (index < symbols.size()) {
1350 Token t = symbols.at(index++).token;
1352 case LBRACE: ++braceCount; break;
1353 case RBRACE: --braceCount; break;
1354 case LBRACK: ++brackCount; break;
1355 case RBRACK: --brackCount; break;
1356 case LPAREN: ++parenCount; break;
1357 case RPAREN: --parenCount; break;
1358 case LANGLE: ++angleCount; break;
1359 case RANGLE: --angleCount; break;
1360 case GTGT: angleCount -= 2; t = RANGLE; break;
1367 && (target != RANGLE || angleCount <= 0)) {
1368 if (target != COMMA || angleCount <= 0)
1373 if (target == COMMA && t == EQ && possible != -1) {
1378 if (braceCount < 0 || brackCount < 0 || parenCount < 0
1379 || (target == RANGLE && angleCount < 0)) {
1385 if(target == COMMA && angleCount != 0 && possible != -1) {
1393 void Moc::checkSuperClasses(ClassDef *def)
1395 const QByteArray firstSuperclass = def->superclassList.value(0).first;
1397 if (!knownQObjectClasses.contains(firstSuperclass)) {
1398 // enable once we /require/ include paths
1402 msg += def->className;
1403 msg += " contains the Q_OBJECT macro and inherits from ";
1404 msg += def->superclassList.value(0);
1405 msg += " but that is not a known QObject subclass. You may get compilation errors.";
1406 warning(msg.constData());
1410 for (int i = 1; i < def->superclassList.count(); ++i) {
1411 const QByteArray superClass = def->superclassList.at(i).first;
1412 if (knownQObjectClasses.contains(superClass)) {
1415 msg += def->classname;
1416 msg += " inherits from two QObject subclasses ";
1417 msg += firstSuperclass;
1420 msg += ". This is not supported!";
1421 warning(msg.constData());
1424 if (interface2IdMap.contains(superClass)) {
1425 bool registeredInterface = false;
1426 for (int i = 0; i < def->interfaceList.count(); ++i)
1427 if (def->interfaceList.at(i).first().className == superClass) {
1428 registeredInterface = true;
1432 if (!registeredInterface) {
1435 msg += def->classname;
1436 msg += " implements the interface ";
1438 msg += " but does not list it in Q_INTERFACES. qobject_cast to ";
1440 msg += " will not work!";
1441 warning(msg.constData());
1447 void Moc::checkProperties(ClassDef *cdef)
1450 // specify get function, for compatibiliy we accept functions
1451 // returning pointers, or const char * for QByteArray.
1453 for (int i = 0; i < cdef->propertyList.count(); ++i) {
1454 PropertyDef &p = cdef->propertyList[i];
1455 if (p.read.isEmpty())
1457 for (int j = 0; j < cdef->publicList.count(); ++j) {
1458 const FunctionDef &f = cdef->publicList.at(j);
1459 if (f.name != p.read)
1461 if (!f.isConst) // get functions must be const
1463 if (f.arguments.size()) // and must not take any arguments
1465 PropertyDef::Specification spec = PropertyDef::ValueSpec;
1466 QByteArray tmp = f.normalizedType;
1467 if (p.type == "QByteArray" && tmp == "const char *")
1469 if (tmp.left(6) == "const ")
1471 if (p.type != tmp && tmp.endsWith('*')) {
1473 spec = PropertyDef::PointerSpec;
1474 } else if (f.type.name.endsWith('&')) { // raw type, not normalized type
1475 spec = PropertyDef::ReferenceSpec;
1482 if(!p.notify.isEmpty()) {
1484 for (int j = 0; j < cdef->signalList.count(); ++j) {
1485 const FunctionDef &f = cdef->signalList.at(j);
1486 if(f.name != p.notify) {
1489 notifyId = j /* Signal indexes start from 0 */;
1493 p.notifyId = notifyId;
1494 if (notifyId == -1) {
1495 QByteArray msg = "NOTIFY signal '" + p.notify + "' of property '" + p.name
1496 + "' does not exist in class " + cdef->classname + ".";
1497 error(msg.constData());