1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "qqmljsregistercontent_p.h" |
5 | #include "qqmljstyperesolver_p.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | using namespace Qt::StringLiterals; |
10 | |
11 | QString QQmlJSRegisterContent::descriptiveName() const |
12 | { |
13 | if (m_storedType.isNull()) |
14 | return u"(invalid type)"_s ; |
15 | |
16 | QString result = m_storedType->internalName() + u" of "_s ; |
17 | const auto scope = [this]() -> QString { |
18 | if (m_scope.isNull()) |
19 | return u"(invalid type)::"_s ; |
20 | return (m_scope->internalName().isEmpty() |
21 | ? (m_scope->filePath().isEmpty() |
22 | ? u"??"_s |
23 | : (u"(component in "_s + m_scope->filePath() + u")"_s )) |
24 | : m_scope->internalName()) |
25 | + u"::"_s ; |
26 | }; |
27 | |
28 | switch (m_content.index()) { |
29 | case Type: |
30 | return result + std::get<QQmlJSScope::ConstPtr>(v: m_content)->internalName(); |
31 | case Property: { |
32 | const QQmlJSMetaProperty prop = std::get<QQmlJSMetaProperty>(v: m_content); |
33 | return result + scope() + prop.propertyName() + u" with type "_s + prop.typeName(); |
34 | } |
35 | case Method: { |
36 | const auto methods = std::get<QList<QQmlJSMetaMethod>>(v: m_content); |
37 | if (methods.isEmpty()) |
38 | return result + scope() + u"(unknown method)"_s ; |
39 | else |
40 | return result + scope() + methods[0].methodName() + u"(...)"_s ; |
41 | } |
42 | case Enum: { |
43 | const auto e = std::get<std::pair<QQmlJSMetaEnum, QString>>(v: m_content); |
44 | if (e.second.isEmpty()) |
45 | return result + scope() + e.first.name(); |
46 | else |
47 | return result + scope() + e.first.name() + u"::"_s + e.second; |
48 | } |
49 | case ImportNamespace: { |
50 | return u"import namespace %1"_s .arg(a: std::get<uint>(v: m_content)); |
51 | } |
52 | case Conversion: { |
53 | return u"conversion to %1"_s .arg( |
54 | a: std::get<ConvertedTypes>(v: m_content).result->internalName()); |
55 | } |
56 | } |
57 | Q_UNREACHABLE_RETURN(result + u"wat?"_s ); |
58 | } |
59 | |
60 | bool QQmlJSRegisterContent::isList() const |
61 | { |
62 | switch (m_content.index()) { |
63 | case Type: |
64 | return std::get<QQmlJSScope::ConstPtr>(v: m_content)->accessSemantics() |
65 | == QQmlJSScope::AccessSemantics::Sequence; |
66 | case Property: |
67 | return std::get<QQmlJSMetaProperty>(v: m_content).type()->accessSemantics() |
68 | == QQmlJSScope::AccessSemantics::Sequence; |
69 | case Conversion: |
70 | return std::get<ConvertedTypes>(v: m_content).result->accessSemantics() |
71 | == QQmlJSScope::AccessSemantics::Sequence; |
72 | default: |
73 | return false; |
74 | } |
75 | } |
76 | |
77 | bool QQmlJSRegisterContent::isWritable() const |
78 | { |
79 | switch (m_content.index()) { |
80 | case Property: |
81 | return std::get<QQmlJSMetaProperty>(v: m_content).isWritable(); |
82 | |
83 | // TODO: What can we actually write? |
84 | default: |
85 | break; |
86 | } |
87 | |
88 | return true; |
89 | } |
90 | |
91 | QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType, |
92 | const QQmlJSScope::ConstPtr &type, |
93 | QQmlJSRegisterContent::ContentVariant variant, |
94 | const QQmlJSScope::ConstPtr &scope) |
95 | { |
96 | QQmlJSRegisterContent result(storedType, scope, variant); |
97 | result.m_content = type; |
98 | return result; |
99 | } |
100 | |
101 | QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType, |
102 | const QQmlJSMetaProperty &property, |
103 | QQmlJSRegisterContent::ContentVariant variant, |
104 | const QQmlJSScope::ConstPtr &scope) |
105 | { |
106 | QQmlJSRegisterContent result(storedType, scope, variant); |
107 | result.m_content = property; |
108 | return result; |
109 | } |
110 | |
111 | QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType, |
112 | const QQmlJSMetaEnum &enumeration, |
113 | const QString &enumMember, |
114 | QQmlJSRegisterContent::ContentVariant variant, |
115 | const QQmlJSScope::ConstPtr &scope) |
116 | { |
117 | QQmlJSRegisterContent result(storedType, scope, variant); |
118 | result.m_content = std::make_pair(x: enumeration, y: enumMember); |
119 | return result; |
120 | } |
121 | |
122 | QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType, |
123 | const QList<QQmlJSMetaMethod> &methods, |
124 | QQmlJSRegisterContent::ContentVariant variant, |
125 | const QQmlJSScope::ConstPtr &scope) |
126 | { |
127 | QQmlJSRegisterContent result(storedType, scope, variant); |
128 | result.m_content = methods; |
129 | return result; |
130 | } |
131 | |
132 | QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType, |
133 | uint importNamespaceStringId, |
134 | QQmlJSRegisterContent::ContentVariant variant, |
135 | const QQmlJSScope::ConstPtr &scope) |
136 | { |
137 | QQmlJSRegisterContent result(storedType, scope, variant); |
138 | result.m_content = importNamespaceStringId; |
139 | return result; |
140 | } |
141 | |
142 | QQmlJSRegisterContent QQmlJSRegisterContent::create(const QQmlJSScope::ConstPtr &storedType, |
143 | const QList<QQmlJSScope::ConstPtr> origins, |
144 | const QQmlJSScope::ConstPtr &conversion, |
145 | const QQmlJSScope::ConstPtr &conversionScope, |
146 | ContentVariant variant, |
147 | const QQmlJSScope::ConstPtr &scope) |
148 | { |
149 | QQmlJSRegisterContent result(storedType, scope, variant); |
150 | result.m_content = ConvertedTypes { .origins: origins, .result: conversion, .resultScope: conversionScope }; |
151 | return result; |
152 | } |
153 | |
154 | QT_END_NAMESPACE |
155 | |