aboutsummaryrefslogtreecommitdiffstats
path: root/tools/shared
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2020-08-12 12:36:15 +0200
committerUlf Hermann <ulf.hermann@qt.io>2020-08-13 14:00:59 +0200
commit9a79791e7975de811102f89686a5d631eeac9d16 (patch)
tree07ad3c63b4b0bc7cd72d1bb083effd0c4fd74053 /tools/shared
parent1a3738dfa85d6aa6771893c2a2b3cf874b492524 (diff)
Move functionality to read types from QML files out of qmllint
We want to re-use this in other tools. Change-Id: I2a8cd104d54c1fa9b2898213b0b9e09719e42bca Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tools/shared')
-rw-r--r--tools/shared/importedmembersvisitor.cpp169
-rw-r--r--tools/shared/importedmembersvisitor.h71
-rw-r--r--tools/shared/qmljstypereader.cpp131
-rw-r--r--tools/shared/qmljstypereader.h70
-rw-r--r--tools/shared/shared.pri4
5 files changed, 445 insertions, 0 deletions
diff --git a/tools/shared/importedmembersvisitor.cpp b/tools/shared/importedmembersvisitor.cpp
new file mode 100644
index 0000000000..2162044f99
--- /dev/null
+++ b/tools/shared/importedmembersvisitor.cpp
@@ -0,0 +1,169 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the tools applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "importedmembersvisitor.h"
+#include "scopetree.h"
+
+using namespace QQmlJS::AST;
+
+ScopeTree::Ptr ImportedMembersVisitor::result(const QString &scopeName) const
+{
+ ScopeTree::Ptr result = ScopeTree::create();
+ result->setClassName(scopeName);
+ result->setSuperclassName(m_rootObject->superclassName());
+ const auto properties = m_rootObject->properties();
+ for (auto property : properties) {
+ if (property.isAlias()) {
+ const auto it = m_objects.find(property.typeName());
+ if (it != m_objects.end())
+ property.setType(*it);
+ result->addProperty(property);
+ } else {
+ result->addProperty(property);
+ }
+ }
+
+ for (const auto &method : m_rootObject->methods())
+ result->addMethod(method);
+
+ for (const auto &enumerator : m_rootObject->enums())
+ result->addEnum(enumerator);
+
+ return result;
+}
+
+bool ImportedMembersVisitor::visit(UiObjectDefinition *definition)
+{
+ ScopeTree::Ptr scope = ScopeTree::create();
+ QString superType;
+ for (auto segment = definition->qualifiedTypeNameId; segment; segment = segment->next) {
+ if (!superType.isEmpty())
+ superType.append('.');
+ superType.append(segment->name.toString());
+ }
+ scope->setSuperclassName(superType);
+ if (!m_rootObject)
+ m_rootObject = scope;
+ m_currentObjects.append(scope);
+ return true;
+}
+
+void ImportedMembersVisitor::endVisit(UiObjectDefinition *)
+{
+ m_currentObjects.pop_back();
+}
+
+bool ImportedMembersVisitor::visit(UiPublicMember *publicMember)
+{
+ switch (publicMember->type) {
+ case UiPublicMember::Signal: {
+ UiParameterList *param = publicMember->parameters;
+ MetaMethod method;
+ method.setMethodType(MetaMethod::Signal);
+ method.setMethodName(publicMember->name.toString());
+ while (param) {
+ method.addParameter(param->name.toString(), param->type->name.toString());
+ param = param->next;
+ }
+ currentObject()->addMethod(method);
+ break;
+ }
+ case UiPublicMember::Property: {
+ auto typeName = publicMember->memberType->name;
+ const bool isAlias = (typeName == QLatin1String("alias"));
+ if (isAlias) {
+ const auto expression = cast<ExpressionStatement *>(publicMember->statement);
+ if (const auto idExpression = cast<IdentifierExpression *>(expression->expression))
+ typeName = idExpression->name;
+ }
+ MetaProperty prop {
+ publicMember->name.toString(),
+ typeName.toString(),
+ false,
+ false,
+ false,
+ isAlias,
+ 0
+ };
+ currentObject()->addProperty(prop);
+ break;
+ }
+ }
+ return true;
+}
+
+bool ImportedMembersVisitor::visit(UiSourceElement *sourceElement)
+{
+ if (FunctionExpression *fexpr = sourceElement->sourceElement->asFunctionDefinition()) {
+ MetaMethod method;
+ method.setMethodName(fexpr->name.toString());
+ method.setMethodType(MetaMethod::Method);
+ FormalParameterList *parameters = fexpr->formals;
+ while (parameters) {
+ method.addParameter(parameters->element->bindingIdentifier.toString(), "");
+ parameters = parameters->next;
+ }
+ currentObject()->addMethod(method);
+ } else if (ClassExpression *clexpr = sourceElement->sourceElement->asClassDefinition()) {
+ MetaProperty prop { clexpr->name.toString(), "", false, false, false, false, 1 };
+ currentObject()->addProperty(prop);
+ } else if (cast<VariableStatement *>(sourceElement->sourceElement)) {
+ // nothing to do
+ } else {
+ const auto loc = sourceElement->firstSourceLocation();
+ m_errors.append(
+ "unsupportedd sourceElement at "
+ + QString::fromLatin1("%1:%2: ").arg(loc.startLine).arg(loc.startColumn)
+ + QString::number(sourceElement->sourceElement->kind));
+ }
+ return true;
+}
+
+bool ImportedMembersVisitor::visit(UiScriptBinding *scriptBinding)
+{
+ if (scriptBinding->qualifiedId->name == QLatin1String("id")) {
+ const auto *statement = cast<ExpressionStatement *>(scriptBinding->statement);
+ const auto *idExprension = cast<IdentifierExpression *>(statement->expression);
+ m_objects.insert(idExprension->name.toString(), currentObject());
+ }
+ return true;
+}
+
+bool ImportedMembersVisitor::visit(QQmlJS::AST::UiEnumDeclaration *uied)
+{
+ MetaEnum qmlEnum(uied->name.toString());
+ for (const auto *member = uied->members; member; member = member->next)
+ qmlEnum.addKey(member->member.toString());
+ currentObject()->addEnum(qmlEnum);
+ return true;
+}
+
+void ImportedMembersVisitor::throwRecursionDepthError()
+{
+ m_errors.append(QStringLiteral("Maximum statement or expression depth exceeded"));
+}
diff --git a/tools/shared/importedmembersvisitor.h b/tools/shared/importedmembersvisitor.h
new file mode 100644
index 0000000000..ab44deda9b
--- /dev/null
+++ b/tools/shared/importedmembersvisitor.h
@@ -0,0 +1,71 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the tools applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef IMPORTEDMEMBERSVISITOR_H
+#define IMPORTEDMEMBERSVISITOR_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+
+#include "scopetree.h"
+#include "qcoloroutput.h"
+
+#include <private/qqmljsast_p.h>
+
+class ImportedMembersVisitor : public QQmlJS::AST::Visitor
+{
+public:
+ ScopeTree::Ptr result(const QString &scopeName) const;
+ QStringList errors() const { return m_errors; }
+
+private:
+ bool visit(QQmlJS::AST::UiObjectDefinition *) override;
+ void endVisit(QQmlJS::AST::UiObjectDefinition *) override;
+ bool visit(QQmlJS::AST::UiPublicMember *) override;
+ bool visit(QQmlJS::AST::UiSourceElement *) override;
+ bool visit(QQmlJS::AST::UiScriptBinding *) override;
+ bool visit(QQmlJS::AST::UiEnumDeclaration *uied) override;
+ void throwRecursionDepthError() override;
+
+ ScopeTree::Ptr currentObject() const { return m_currentObjects.back(); }
+
+ QVector<ScopeTree::Ptr> m_currentObjects;
+ ScopeTree::ConstPtr m_rootObject;
+ QHash<QString, ScopeTree::Ptr> m_objects;
+
+ QStringList m_errors;
+};
+
+#endif // IMPORTEDMEMBERSVISITOR_H
diff --git a/tools/shared/qmljstypereader.cpp b/tools/shared/qmljstypereader.cpp
new file mode 100644
index 0000000000..3825535d44
--- /dev/null
+++ b/tools/shared/qmljstypereader.cpp
@@ -0,0 +1,131 @@
+/****************************************************************************
+**
+** Copyright (C) 2020 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the tools applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qmljstypereader.h"
+#include "importedmembersvisitor.h"
+
+#include <QtQml/private/qqmljsast_p.h>
+#include <QtQml/private/qqmljsengine_p.h>
+#include <QtQml/private/qqmljslexer_p.h>
+#include <QtQml/private/qqmljsparser_p.h>
+#include <QtQml/private/qqmlimportresolver_p.h>
+
+#include <QtCore/qfileinfo.h>
+#include <QtCore/qdebug.h>
+
+static QList<QmlJSTypeReader::Import> parseHeaders(QQmlJS::AST::UiHeaderItemList *header)
+{
+ using namespace QQmlJS::AST;
+ QList<QmlJSTypeReader::Import> imports;
+
+ for (; header; header = header->next) {
+ auto import = cast<UiImport *>(header->headerItem);
+ if (!import)
+ continue;
+
+ QString path;
+ auto uri = import->importUri;
+ while (uri) {
+ path.append(uri->name);
+ path.append(QLatin1Char('/'));
+ uri = uri->next;
+ }
+ path.chop(1);
+ imports.append({
+ path,
+ import->version ? import->version->version : QTypeRevision(),
+ import->asToken.isValid() ? import->importId.toString() : QString()
+ });
+ }
+
+ return imports;
+}
+
+static ScopeTree::Ptr parseProgram(QQmlJS::AST::Program *program, const QString &name)
+{
+ using namespace QQmlJS::AST;
+ ScopeTree::Ptr result = ScopeTree::create(ScopeType::JSLexicalScope, name);
+ for (auto *statement = program->statements; statement; statement = statement->next) {
+ if (auto *function = cast<FunctionDeclaration *>(statement->statement)) {
+ MetaMethod method(function->name.toString());
+ method.setMethodType(MetaMethod::Method);
+ for (auto *parameters = function->formals; parameters; parameters = parameters->next)
+ method.addParameter(parameters->element->bindingIdentifier.toString(), QString());
+ result->addMethod(method);
+ }
+ }
+ return result;
+}
+
+ScopeTree::Ptr QmlJSTypeReader::operator()()
+{
+ using namespace QQmlJS::AST;
+ const QFileInfo info { m_file };
+ QString baseName = info.baseName();
+ const QString scopeName = baseName.endsWith(QStringLiteral(".ui")) ? baseName.chopped(3)
+ : baseName;
+
+ QQmlJS::Engine engine;
+ QQmlJS::Lexer lexer(&engine);
+
+ const QString lowerSuffix = info.suffix().toLower();
+ const bool isESModule = lowerSuffix == QLatin1String("mjs");
+ const bool isJavaScript = isESModule || lowerSuffix == QLatin1String("js");
+
+ QFile file(m_file);
+ if (!file.open(QFile::ReadOnly)) {
+ return ScopeTree::create(isJavaScript ? ScopeType::JSLexicalScope : ScopeType::QMLScope,
+ scopeName);
+ }
+
+ QString code = QString::fromUtf8(file.readAll());
+ file.close();
+
+ lexer.setCode(code, /*line = */ 1, /*qmlMode=*/ !isJavaScript);
+ QQmlJS::Parser parser(&engine);
+
+ const bool success = isJavaScript ? (isESModule ? parser.parseModule()
+ : parser.parseProgram())
+ : parser.parse();
+ if (!success) {
+ return ScopeTree::create(isJavaScript ? ScopeType::JSLexicalScope : ScopeType::QMLScope,
+ scopeName);
+ }
+
+ if (!isJavaScript) {
+ QQmlJS::AST::UiProgram *program = parser.ast();
+ m_imports = parseHeaders(program->headers);
+ ImportedMembersVisitor membersVisitor;
+ program->members->accept(&membersVisitor);
+ m_errors = membersVisitor.errors();
+ return membersVisitor.result(scopeName);
+ }
+
+ // TODO: Anything special to do with ES modules here?
+ return parseProgram(QQmlJS::AST::cast<QQmlJS::AST::Program *>(parser.rootNode()), scopeName);
+}
diff --git a/tools/shared/qmljstypereader.h b/tools/shared/qmljstypereader.h
new file mode 100644
index 0000000000..8485f6be6b
--- /dev/null
+++ b/tools/shared/qmljstypereader.h
@@ -0,0 +1,70 @@
+/****************************************************************************
+**
+** Copyright (C) 2020 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the tools applications of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QMLJSTYPERADER_H
+#define QMLJSTYPERADER_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+
+#include "scopetree.h"
+
+#include <QtQml/private/qqmljsastfwd_p.h>
+
+#include <QtCore/qpair.h>
+#include <QtCore/qset.h>
+
+class QmlJSTypeReader
+{
+public:
+ struct Import {
+ QString path;
+ QTypeRevision version;
+ QString prefix;
+ };
+
+ QmlJSTypeReader(const QString &file) : m_file(file) {}
+
+ ScopeTree::Ptr operator()();
+ QList<Import> imports() const { return m_imports; }
+ QStringList errors() const { return m_errors; }
+
+private:
+ QString m_file;
+ QList<Import> m_imports;
+ QStringList m_errors;
+};
+
+#endif // QMLJSTYPEREADER_H
diff --git a/tools/shared/shared.pri b/tools/shared/shared.pri
index 1b3cd4d37b..4ee3704bfb 100644
--- a/tools/shared/shared.pri
+++ b/tools/shared/shared.pri
@@ -11,11 +11,15 @@ RESOURCEFILEMAPPER_HEADERS = \
METATYPEREADER_SOURCES = \
$$PWD/componentversion.cpp \
+ $$PWD/importedmembersvisitor.cpp \
+ $$PWD/qmljstypereader.cpp \
$$PWD/scopetree.cpp \
$$PWD/typedescriptionreader.cpp
METATYPEREADER_HEADERS = \
$$PWD/componentversion.h \
+ $$PWD/importedmembersvisitor.h \
+ $$PWD/qmljstypereader.h \
$$PWD/metatypes.h \
$$PWD/scopetree.h \
$$PWD/typedescriptionreader.h