diff options
Diffstat (limited to 'src/tools/moc/moc.cpp')
-rw-r--r-- | src/tools/moc/moc.cpp | 203 |
1 files changed, 136 insertions, 67 deletions
diff --git a/src/tools/moc/moc.cpp b/src/tools/moc/moc.cpp index 9dbc22dc2c..edef9d3f04 100644 --- a/src/tools/moc/moc.cpp +++ b/src/tools/moc/moc.cpp @@ -43,30 +43,9 @@ QT_BEGIN_NAMESPACE // only moc needs this function -static QByteArray normalizeType(const QByteArray &ba, bool fixScope = false) +static QByteArray normalizeType(const QByteArray &ba) { - const char *s = ba.constData(); - int len = ba.size(); - char stackbuf[64]; - char *buf = (len >= 64 ? new char[len + 1] : stackbuf); - char *d = buf; - char last = 0; - while(*s && is_space(*s)) - s++; - while (*s) { - while (*s && !is_space(*s)) - last = *d++ = *s++; - while (*s && is_space(*s)) - s++; - if (*s && ((is_ident_char(*s) && is_ident_char(last)) - || ((*s == ':') && (last == '<')))) { - last = *d++ = ' '; - } - } - *d = '\0'; - QByteArray result = normalizeTypeInternal(buf, d, fixScope); - if (buf != stackbuf) - delete [] buf; + QByteArray result = normalizeTypeInternal(ba.constBegin(), ba.constEnd()); return result; } @@ -328,6 +307,11 @@ void Moc::parseFunctionArguments(FunctionDef *def) def->arguments.removeLast(); def->isPrivateSignal = true; } + if (def->arguments.size() == 1 + && def->arguments.constLast().normalizedType == "QMethodRawArguments") { + def->arguments.removeLast(); + def->isRawSlot = true; + } } bool Moc::testFunctionAttribute(FunctionDef *def) @@ -371,17 +355,42 @@ bool Moc::skipCxxAttributes() return false; } -bool Moc::testFunctionRevision(FunctionDef *def) +QTypeRevision Moc::parseRevision() { - if (test(Q_REVISION_TOKEN)) { - next(LPAREN); - QByteArray revision = lexemUntil(RPAREN); - revision.remove(0, 1); - revision.chop(1); + next(LPAREN); + QByteArray revisionString = lexemUntil(RPAREN); + revisionString.remove(0, 1); + revisionString.chop(1); + const QList<QByteArray> majorMinor = revisionString.split(','); + switch (majorMinor.length()) { + case 1: { bool ok = false; - def->revision = revision.toInt(&ok); - if (!ok || def->revision < 0) + const int revision = revisionString.toInt(&ok); + if (!ok || !QTypeRevision::isValidSegment(revision)) error("Invalid revision"); + return QTypeRevision::fromMinorVersion(revision); + } + case 2: { // major.minor + bool ok = false; + const int major = majorMinor[0].toInt(&ok); + if (!ok || !QTypeRevision::isValidSegment(major)) + error("Invalid major version"); + const int minor = majorMinor[1].toInt(&ok); + if (!ok || !QTypeRevision::isValidSegment(minor)) + error("Invalid minor version"); + return QTypeRevision::fromVersion(major, minor); + } + default: + error("Invalid revision"); + return QTypeRevision(); + } +} + +bool Moc::testFunctionRevision(FunctionDef *def) +{ + + if (test(Q_REVISION_TOKEN)) { + def->revision = parseRevision().toEncodedVersion<int>(); return true; } @@ -555,6 +564,32 @@ bool Moc::parseMaybeFunction(const ClassDef *cdef, FunctionDef *def) return true; } + +// Try to parse QProperty<MyType> propertName; members +bool Moc::parseMaybeQProperty(ClassDef *def) +{ + if (!test(IDENTIFIER)) + return false; + + if (lexem() != "QProperty") + return false; + + if (!test(LANGLE)) + return false; + + until(RANGLE); + + next(); + const auto propName = lexem(); + + if (!test(SEMIC)) + return false; + + def->qPropertyMembers.insert(propName); + + return true; +} + void Moc::parse() { QVector<NamespaceDef> namespaceList; @@ -648,6 +683,11 @@ void Moc::parse() case Q_CLASSINFO_TOKEN: parseClassInfo(&def); break; + case Q_MOC_INCLUDE_TOKEN: + // skip it, the namespace is parsed twice + next(LPAREN); + lexemUntil(RPAREN); + break; case ENUM: { EnumDef enumDef; if (parseEnum(&enumDef)) @@ -691,6 +731,9 @@ void Moc::parse() case Q_DECLARE_METATYPE_TOKEN: parseDeclareMetatype(); break; + case Q_MOC_INCLUDE_TOKEN: + parseMocInclude(); + break; case USING: if (test(NAMESPACE)) { while (test(SCOPE) || test(IDENTIFIER)) @@ -823,6 +866,9 @@ void Moc::parse() case Q_CLASSINFO_TOKEN: parseClassInfo(&def); break; + case Q_MOC_INCLUDE_TOKEN: + parseMocInclude(); + break; case Q_INTERFACES_TOKEN: parseInterfaces(&def); break; @@ -889,7 +935,9 @@ void Moc::parse() } } } else { - index = rewind; + index = rewind - 1; + if (!parseMaybeQProperty(&def)) + index = rewind; } } } @@ -1084,17 +1132,9 @@ void Moc::generate(FILE *out, FILE *jsonOutput) void Moc::parseSlots(ClassDef *def, FunctionDef::Access access) { - int defaultRevision = -1; - if (test(Q_REVISION_TOKEN)) { - next(LPAREN); - QByteArray revision = lexemUntil(RPAREN); - revision.remove(0, 1); - revision.chop(1); - bool ok = false; - defaultRevision = revision.toInt(&ok); - if (!ok || defaultRevision < 0) - error("Invalid revision"); - } + QTypeRevision defaultRevision; + if (test(Q_REVISION_TOKEN)) + defaultRevision = parseRevision(); next(COLON); while (inClass(def) && hasNext()) { @@ -1123,8 +1163,8 @@ void Moc::parseSlots(ClassDef *def, FunctionDef::Access access) continue; if (funcDef.revision > 0) { ++def->revisionedMethods; - } else if (defaultRevision != -1) { - funcDef.revision = defaultRevision; + } else if (defaultRevision.isValid()) { + funcDef.revision = defaultRevision.toEncodedVersion<int>(); ++def->revisionedMethods; } def->slotList += funcDef; @@ -1138,17 +1178,9 @@ void Moc::parseSlots(ClassDef *def, FunctionDef::Access access) void Moc::parseSignals(ClassDef *def) { - int defaultRevision = -1; - if (test(Q_REVISION_TOKEN)) { - next(LPAREN); - QByteArray revision = lexemUntil(RPAREN); - revision.remove(0, 1); - revision.chop(1); - bool ok = false; - defaultRevision = revision.toInt(&ok); - if (!ok || defaultRevision < 0) - error("Invalid revision"); - } + QTypeRevision defaultRevision; + if (test(Q_REVISION_TOKEN)) + defaultRevision = parseRevision(); next(COLON); while (inClass(def) && hasNext()) { @@ -1179,8 +1211,8 @@ void Moc::parseSignals(ClassDef *def) error("Not a signal declaration"); if (funcDef.revision > 0) { ++def->revisionedMethods; - } else if (defaultRevision != -1) { - funcDef.revision = defaultRevision; + } else if (defaultRevision.isValid()) { + funcDef.revision = defaultRevision.toEncodedVersion<int>(); ++def->revisionedMethods; } def->signalList += funcDef; @@ -1194,11 +1226,14 @@ void Moc::parseSignals(ClassDef *def) void Moc::createPropertyDef(PropertyDef &propDef) { + propDef.location = index; + QByteArray type = parseType().name; if (type.isEmpty()) error(); propDef.designable = propDef.scriptable = propDef.stored = "true"; propDef.user = "false"; + /* The Q_PROPERTY construct cannot contain any commas, since commas separate macro arguments. We therefore expect users @@ -1230,6 +1265,7 @@ void Moc::createPropertyDef(PropertyDef &propDef) next(); propDef.name = lexem(); + while (test(IDENTIFIER)) { const QByteArray l = lexem(); if (l[0] == 'C' && l == "CONSTANT") { @@ -1241,6 +1277,10 @@ void Moc::createPropertyDef(PropertyDef &propDef) } else if (l[0] == 'R' && l == "REQUIRED") { propDef.required = true; continue; + } else if (l[0] == 'R' && l == "REVISION" && test(LPAREN)) { + prev(); + propDef.revision = parseRevision().toEncodedVersion<int>(); + continue; } QByteArray v, v2; @@ -1273,9 +1313,10 @@ void Moc::createPropertyDef(PropertyDef &propDef) propDef.reset = v + v2; else if (l == "REVISION") { bool ok = false; - propDef.revision = v.toInt(&ok); - if (!ok || propDef.revision < 0) + const int minor = v.toInt(&ok); + if (!ok || !QTypeRevision::isValidSegment(minor)) error(1); + propDef.revision = QTypeRevision::fromMinorVersion(minor).toEncodedVersion<int>(); } else error(2); break; @@ -1314,11 +1355,6 @@ void Moc::createPropertyDef(PropertyDef &propDef) error(2); } } - if (propDef.read.isNull() && propDef.member.isNull()) { - const QByteArray msg = "Property declaration " + propDef.name - + " has no READ accessor function or associated MEMBER variable. The property will be invalid."; - warning(msg.constData()); - } if (propDef.constant && !propDef.write.isNull()) { const QByteArray msg = "Property declaration " + propDef.name + " is both WRITEable and CONSTANT. CONSTANT will be ignored."; @@ -1486,6 +1522,8 @@ void Moc::parseClassInfo(BaseDef *def) next(COMMA); if (test(STRING_LITERAL)) { infoDef.value = symbol().unquotedLexem(); + } else if (test(Q_REVISION_TOKEN)) { + infoDef.value = QByteArray::number(parseRevision().toEncodedVersion<quint16>()); } else { // support Q_CLASSINFO("help", QT_TR_NOOP("blah")) next(IDENTIFIER); @@ -1563,6 +1601,16 @@ void Moc::parseDeclareMetatype() metaTypes.append(typeName); } +void Moc::parseMocInclude() +{ + next(LPAREN); + QByteArray include = lexemUntil(RPAREN); + // remove parentheses + include.remove(0, 1); + include.chop(1); + includeFiles.append(include); +} + void Moc::parseSlotInPrivate(ClassDef *def, FunctionDef::Access access) { next(LPAREN); @@ -1751,14 +1799,34 @@ void Moc::checkProperties(ClassDef *cdef) QSet<QByteArray> definedProperties; for (int i = 0; i < cdef->propertyList.count(); ++i) { PropertyDef &p = cdef->propertyList[i]; - if (p.read.isEmpty() && p.member.isEmpty()) - continue; if (definedProperties.contains(p.name)) { QByteArray msg = "The property '" + p.name + "' is defined multiple times in class " + cdef->classname + "."; warning(msg.constData()); } definedProperties.insert(p.name); + if (p.read.isEmpty() && p.member.isEmpty()) { + if (!cdef->qPropertyMembers.contains(p.name)) { + const int rewind = index; + if (p.location >= 0) + index = p.location; + QByteArray msg = "Property declaration " + p.name + " has neither an associated QProperty<> member" + ", nor a READ accessor function nor an associated MEMBER variable. The property will be invalid."; + warning(msg.constData()); + index = rewind; + if (p.write.isEmpty()) { + cdef->propertyList.removeAt(i); + --i; + } + continue; + } + p.read = p.name + ".value"; + p.write = p.name + ".setValue"; + p.isQProperty = true; + p.designable = p.scriptable = p.stored = "true"; + p.user = "false"; + } + for (int j = 0; j < cdef->publicList.count(); ++j) { const FunctionDef &f = cdef->publicList.at(j); if (f.name != p.read) @@ -1971,6 +2039,7 @@ QJsonObject PropertyDef::toJson() const prop[QLatin1String("constant")] = constant; prop[QLatin1String("final")] = final; prop[QLatin1String("required")] = required; + prop[QLatin1String("isQProperty")] = isQProperty; if (revision > 0) prop[QLatin1String("revision")] = revision; |