From 8ff427ff4f3a03c401b8c071b79b0f1500eb2ca6 Mon Sep 17 00:00:00 2001 From: Brett Stottlemyer Date: Sun, 22 Aug 2021 16:46:55 -0400 Subject: Move class signature handling to parser.g This refactors the signature handling so the AST knows the signatures of the included types. This, in turn, allows the parser headers to be used directly for interpreting .rep files, without needing to includes pieces from the repc tool itself just for the signature. This also augments the signatures to use more fields * Handle individual elements of template parameters. For instance, "QHash" will have a signature based on the flag's signature, the POD's signature and the string "QHash". * Flags include the signature of the enum they are based on * Enum signatures will include the type (if specified) and scope. Pick-to: 6.2 Change-Id: Id4e68e61a1c2690939e368e1e8e7555eb81fd68a Reviewed-by: Michael Brasser --- tools/repc/repcodegenerator.cpp | 90 +---------------------------------------- tools/repc/repcodegenerator.h | 1 - 2 files changed, 2 insertions(+), 89 deletions(-) (limited to 'tools') diff --git a/tools/repc/repcodegenerator.cpp b/tools/repc/repcodegenerator.cpp index 45acfc9..71be53a 100644 --- a/tools/repc/repcodegenerator.cpp +++ b/tools/repc/repcodegenerator.cpp @@ -132,90 +132,9 @@ RepCodeGenerator::RepCodeGenerator(QIODevice *outputDevice, const AST &ast) { } -static QByteArray enumSignature(const ASTEnum &e) -{ - QByteArray ret; - ret += e.name.toLatin1(); - for (const ASTEnumParam ¶m : e.params) - ret += param.name.toLatin1() + QByteArray::number(param.value); - return ret; -} - -static QByteArray typeData(const QString &type, const QHash &specialTypes) -{ - QHash::const_iterator it = specialTypes.find(type); - if (it != specialTypes.end()) - return it.value(); - const auto pos = type.lastIndexOf(QLatin1String("::")); - if (pos > 0) - return typeData(type.mid(pos + 2), specialTypes); - return type.toLatin1(); -} - -static QByteArray functionsData(const QList &functions, - const QHash &specialTypes) -{ - QByteArray ret; - for (const ASTFunction &func : functions) { - ret += func.name.toLatin1(); - for (const ASTDeclaration ¶m : func.params) { - ret += param.name.toLatin1(); - ret += typeData(param.type, specialTypes); - ret += QByteArray(reinterpret_cast(¶m.variableType), - sizeof(param.variableType)); - } - ret += typeData(func.returnType, specialTypes); - } - return ret; -} - QByteArray RepCodeGenerator::classSignature(const ASTClass &ac) { - if (m_globalTypes.contains(ac.name)) - return m_globalTypes[ac.name]; - - QCryptographicHash checksum(QCryptographicHash::Sha1); - QHash localTypes = m_globalTypes; - for (const ASTEnum &e : ac.enums) // add local enums - localTypes[e.name] = enumSignature(e); - - checksum.addData(ac.name.toLatin1()); - - // Checksum properties - QSet classIndices{ ac.subClassPropertyIndices.begin(), - ac.subClassPropertyIndices.end() }; - int propertyIndex = -1; - int modelIndex = 0; - for (const ASTProperty &p : ac.properties) { - propertyIndex++; - checksum.addData(p.name.toLatin1()); - if (p.type == QLatin1String("QAbstractItemModel")) { - QByteArrayList roles; - for (const auto &role : ac.modelMetadata[modelIndex++].roles) - roles << role.name.toLatin1(); - std::sort(roles.begin(), roles.end()); - checksum.addData(roles.join('_')); - } else if (classIndices.contains(propertyIndex)) { - checksum.addData(m_globalTypes[p.type]); - } else { - checksum.addData(typeData(p.type, localTypes)); - } - ASTProperty::Modifier m = p.modifier; - // Treat ReadOnly and SourceOnlySetter the same (interface-wise they are) - if (m == ASTProperty::SourceOnlySetter) - m = ASTProperty::ReadOnly; - checksum.addData(reinterpret_cast(&m), sizeof(m)); - } - - // Checksum signals - checksum.addData(functionsData(ac.signalsList, localTypes)); - - // Checksum slots - checksum.addData(functionsData(ac.slotsList, localTypes)); - - m_globalTypes[ac.name] = checksum.result().toHex(); - - return m_globalTypes[ac.name]; + return m_ast.typeSignatures[ac.name]; } void RepCodeGenerator::generate(Mode mode, QString fileName) @@ -483,13 +402,9 @@ void RepCodeGenerator::generateSimpleSetter(const ASTProperty &property, bool ge void RepCodeGenerator::generatePOD(const POD &pod) { - QByteArray podData = pod.name.toLatin1(); QStringList equalityCheck; - for (const PODAttribute &attr : pod.attributes) { + for (const PODAttribute &attr : pod.attributes) equalityCheck << QStringLiteral("left.%1() == right.%1()").arg(attr.name); - podData += attr.name.toLatin1() + typeData(attr.type, m_globalTypes); - } - m_globalTypes[pod.name] = podData; m_stream << "class " << pod.name << "\n" "{\n" " Q_GADGET\n" @@ -545,7 +460,6 @@ void RepCodeGenerator::generateDeclarationsForEnums(const QList &enums, } for (const ASTEnum &en : enums) { - m_globalTypes[en.name] = enumSignature(en); m_stream << " enum " << (en.isScoped ? "class " : "") << en.name << (en.type.isEmpty() ? "" : " : ") << en.type << " {\n"; for (const ASTEnumParam &p : en.params) diff --git a/tools/repc/repcodegenerator.h b/tools/repc/repcodegenerator.h index 3e8a744..caf94c2 100644 --- a/tools/repc/repcodegenerator.h +++ b/tools/repc/repcodegenerator.h @@ -79,7 +79,6 @@ private: void generateSourceAPI(const ASTClass &astClass); private: - QHash m_globalTypes; QTextStream m_stream; AST m_ast; }; -- cgit v1.2.3