aboutsummaryrefslogtreecommitdiffstats
path: root/src/qmlcompiler/qqmljsregistercontent.cpp
blob: aec772ca5bcfd47ebee59827dadac3e221a4bc68 (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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "qqmljsregistercontent_p.h"
#include "qqmljstyperesolver_p.h"

QT_BEGIN_NAMESPACE

using namespace Qt::StringLiterals;

QString QQmlJSRegisterContent::descriptiveName() const
{
    if (m_storedType.isNull())
        return u"(invalid type)"_s;

    QString result = m_storedType->internalName() + u" of "_s;
    const auto scope = [this]() -> QString {
        if (m_scope.isNull())
            return u"(invalid type)::"_s;
        return (m_scope->internalName().isEmpty()
                        ? (m_scope->filePath().isEmpty()
                                   ? u"??"_s
                                   : (u"(component in "_s + m_scope->filePath() + u")"_s))
                        : m_scope->internalName())
                + u"::"_s;
    };

    switch (m_content.index()) {
    case Type:
        return result + std::get<QQmlJSScope::ConstPtr>(m_content)->internalName();
    case Property: {
        const QQmlJSMetaProperty prop = std::get<QQmlJSMetaProperty>(m_content);
        return result + scope() + prop.propertyName() + u" with type "_s + prop.typeName();
    }
    case Method: {
        const auto methods = std::get<QList<QQmlJSMetaMethod>>(m_content);
        if (methods.isEmpty())
            return result + scope() + u"(unknown method)"_s;
        else
            return result + scope() + methods[0].methodName() + u"(...)"_s;
    }
    case Enum: {
        const auto e = std::get<std::pair<QQmlJSMetaEnum, QString>>(m_content);
        if (e.second.isEmpty())
            return result + scope() + e.first.name();
        else
            return result + scope() + e.first.name() + u"::"_s + e.second;
    }
    case ImportNamespace: {
        return u"import namespace %1"_s.arg(std::get<uint>(m_content));
    }
    case Conversion: {
        return u"conversion to %1"_s.arg(
                    std::get<ConvertedTypes>(m_content).result->internalName());
    }
    }
    Q_UNREACHABLE();
    return result + u"wat?"_s;
}

bool QQmlJSRegisterContent::isList() const
{
    switch (m_content.index()) {
    case Type:
        return std::get<QQmlJSScope::ConstPtr>(m_content)->accessSemantics()
                == QQmlJSScope::AccessSemantics::Sequence;
    case Property: {
        const auto prop = std::get<QQmlJSMetaProperty>(m_content);
        return prop.isList()
                || prop.type()->accessSemantics() == QQmlJSScope::AccessSemantics::Sequence;
    }
    case Conversion:
        return std::get<ConvertedTypes>(m_content).result->accessSemantics()
                == QQmlJSScope::AccessSemantics::Sequence;
    default:
        return false;
    }
}

bool QQmlJSRegisterContent::isWritable() const
{
    switch (m_content.index()) {
    case Property:
        return std::get<QQmlJSMetaProperty>(m_content).isWritable();

    // TODO: What can we actually write?
    default:
        break;
    }

    return true;
}

QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType,
                                                    const QQmlJSScope::ConstPtr &type,
                                                    QQmlJSRegisterContent::ContentVariant variant,
                                                    const QQmlJSScope::ConstPtr &scope)
{
    QQmlJSRegisterContent result(storedType, scope, variant);
    result.m_content = type;
    return result;
}

QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType,
                                                    const QQmlJSMetaProperty &property,
                                                    QQmlJSRegisterContent::ContentVariant variant,
                                                    const QQmlJSScope::ConstPtr &scope)
{
    QQmlJSRegisterContent result(storedType, scope, variant);
    result.m_content = property;
    return result;
}

QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType,
                                                    const QQmlJSMetaEnum &enumeration,
                                                    const QString &enumMember,
                                                    QQmlJSRegisterContent::ContentVariant variant,
                                                    const QQmlJSScope::ConstPtr &scope)
{
    QQmlJSRegisterContent result(storedType, scope, variant);
    result.m_content = std::make_pair(enumeration, enumMember);
    return result;
}

QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType,
                                                    const QList<QQmlJSMetaMethod> &methods,
                                                    QQmlJSRegisterContent::ContentVariant variant,
                                                    const QQmlJSScope::ConstPtr &scope)
{
    QQmlJSRegisterContent result(storedType, scope, variant);
    result.m_content = methods;
    return result;
}

QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType,
                                                    uint importNamespaceStringId,
                                                    QQmlJSRegisterContent::ContentVariant variant,
                                                    const QQmlJSScope::ConstPtr &scope)
{
    QQmlJSRegisterContent result(storedType, scope, variant);
    result.m_content = importNamespaceStringId;
    return result;
}

QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType,
                                                    const QList<QQmlJSScope::ConstPtr> origins,
                                                    const QQmlJSScope::ConstPtr &conversion,
                                                    ContentVariant variant,
                                                    const QQmlJSScope::ConstPtr &scope)
{
    QQmlJSRegisterContent result(storedType, scope, variant);
    result.m_content = ConvertedTypes { origins, conversion };
    return result;
}

QT_END_NAMESPACE