aboutsummaryrefslogtreecommitdiffstats
path: root/tools/qmllint/importedmembersvisitor.cpp
blob: c5214f2bb94fca9e140f0ec574976cbf6027fc5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/****************************************************************************
**
** 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 *ImportedMembersVisitor::result(const QString &scopeName) const
{
    ScopeTree *result = new ScopeTree(ScopeType::QMLScope);
    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->get());
            result->addProperty(property);
        } else {
            result->addProperty(property);
        }
    }

    for (const auto &method : m_rootObject->methods())
        result->addMethod(method);

    return result;
}

bool ImportedMembersVisitor::visit(UiObjectDefinition *definition)
{
    ScopeTree::Ptr scope(new ScopeTree(ScopeType::QMLScope));
    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_colorOut->writeUncolored(
                    "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;
}

void ImportedMembersVisitor::throwRecursionDepthError()
{
    m_colorOut->write(QStringLiteral("Error"), Error);
    m_colorOut->write(QStringLiteral("Maximum statement or expression depth exceeded"), Error);
}