Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmljslookupsignatures.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3// Qt-Security score:significant
4
6
7#include <private/qqmljstyperesolver_p.h>
8
9using namespace Qt::StringLiterals;
10
11QT_BEGIN_NAMESPACE
12
13QQmlJSLookupSignaturesRecorder::QQmlJSLookupSignaturesRecorder(
14 const QString &currentFilePath, const QQmlJSTypeResolver *typeResolver)
15 : m_currentFilePath(currentFilePath), m_typeResolver(typeResolver)
16{
17 Q_ASSERT(!m_currentFilePath.isEmpty());
18 Q_ASSERT(m_typeResolver);
19}
20
21#define CHECKED_OPT_ASSIGN(var, expr)
22 {
23 const auto opt = expr;
24 if (!opt.has_value())
25 return false;
26 var = opt.value();
27 }
28
29std::optional<QQmlPrivate::AOTLookupValidation::Type>
30QQmlJSLookupSignaturesRecorder::type(const QQmlJSScope::ConstPtr &type)
31{
32 using namespace QQmlPrivate::AOTLookupValidation;
33
34 QQmlPrivate::AOTLookupValidation::Type res;
35 if (!type->isComposite()) {
36 if (type->accessSemantics() == QQmlSA::AccessSemantics::Value && type->isSelfExtension()) {
37 res.name = type->internalName();
38 res.icNameOrExtensionTypeName = type->extensionTypeName();
39 } else {
40 res.name = type->internalName();
41 }
42
43 return res;
44 }
45
46 res.isComposite = IsComposite::Yes;
47 if (isUnnamedCompositeType(type)) {
48 // An unnamed type can only be used as the base of a lookup, not the type in a property or
49 // method declaration. Assume the target property/method is declared on a base type, and
50 // record the base type of \a type as the base of the lookup. Otherwise, we would have
51 // discarded the lookup earlier. See recordPropertyLookup.
52 if (!type->baseType()) {
53 const QString msg = "unknown base type for composite object at %1:%2"_L1;
54 m_rejectMessage = msg.arg(type->filePath(), QString::number(type->lineNumber()));
55 return {};
56 }
57 return QQmlJSLookupSignaturesRecorder::type(type->baseType());
58 }
59
60 QQmlJSScope::ConstPtr root = type;
61 while (!root->isFileRootComponent())
62 root = root->parentScope();
63
64 bool isFileBeingCompiled = root->filePath() == m_typeResolver->logger()->filePath();
65
66 Q_ASSERT(!root->internalName().isEmpty());
67 res.module = isFileBeingCompiled ? s_thisCuModule : root->moduleName();
68 res.name = isFileBeingCompiled ? s_thisCuType : root->internalName();
69
70 if (type->isInlineComponent()) {
71 res.isInlineComponent = IsIC::Yes;
72 res.icNameOrExtensionTypeName = *type->inlineComponentName();
73 return res;
74 }
75
76 return res;
77}
78
79bool QQmlJSLookupSignaturesRecorder::cantDesync(const QQmlJSScope::ConstPtr &type) const
80{
81 Q_ASSERT(!m_typeResolver->logger()->filePath().isEmpty());
82 // Same file -> If one changes the other also gets recompiled and adapts to the change
83 return type->filePath() == m_typeResolver->logger()->filePath();
84}
85
86bool QQmlJSLookupSignaturesRecorder::safeBase(const QQmlJSScope::ConstPtr &base) const
87{
88 return base == m_typeResolver->varType() || base == m_typeResolver->jsValueType()
89 || base->inherits(m_typeResolver->qmlPropertyMapType());
90}
91
92bool QQmlJSLookupSignaturesRecorder::isUnnamedCompositeType(const QQmlJSScope::ConstPtr &type) const
93{
94 return type->isComposite() && !type->isFileRootComponent() && !type->isInlineComponent();
95}
96
97// On the MetaObject, signals come before regular functions
98static int ownRegularMethodCountBeforeIndex(const QQmlJSScope::ConstPtr &type, int index)
99{
100 const auto &ms = type->ownMethods();
101 return std::count_if(ms.cbegin(), ms.cend(), [&](const QQmlJSMetaMethod &m) {
102 Q_ASSERT(m.isConstructor() || m.methodIndex() != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
103 return m.methodType() != QQmlSA::MethodType::Signal && !m.isConstructor()
104 && int(m.methodIndex()) < index;
105 });
106}
107
108// On the MetaObject, signals come before regular functions
109static int ownSignalCountAfterIndex(const QQmlJSScope::ConstPtr &type, int index)
110{
111 const auto &ms = type->ownMethods();
112 return std::count_if(ms.cbegin(), ms.cend(), [&](const QQmlJSMetaMethod &m) {
113 Q_ASSERT(m.isConstructor() || m.methodIndex() != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
114 return m.methodType() == QQmlSA::MethodType::Signal && int(m.methodIndex()) > index;
115 });
116}
117
118// On the MetaObject, regular properties come before aliases
119static int ownAliasCountBeforeIndex(const QQmlJSScope::ConstPtr &type, int index)
120{
121 const auto &ps = type->ownProperties();
122 return std::count_if(ps.cbegin(), ps.cend(), [&](const auto &p) {
123 return p.isAlias() && p.index() < index;
124 });
125}
126
127// On the MetaObject, regular properties come before aliases
128static int ownRegularPropertyCountAfterIndex(const QQmlJSScope::ConstPtr &type, int index)
129{
130 const auto &ps = type->ownProperties();
131 return std::count_if(ps.cbegin(), ps.cend(), [&](const auto &p) {
132 return !p.isAlias() && p.index() > index;
133 });
134}
135
136bool QQmlJSLookupSignaturesRecorder::recordPropertyLookup(const QQmlJSScope::ConstPtr &base,
137 const QQmlJSMetaProperty &property)
138{
139 using namespace QQmlPrivate::AOTLookupValidation;
140
141 const QString &name = property.propertyName();
142 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfProperty(base, name);
143 if (base->isScript() || safeBase(base) || cantDesync(owner))
144 return true;
145
146 // When performing a lookup on an inner object of unnamed type, one of two things is true:
147 // 1) The target property (or method) is declared within the object. Then, because the object
148 // is unnamed, it must be defined in the same file as the lookup and they cannot desync and
149 // we don't have to record it.
150 // 2) Or the target property is declared on a base type of the inner object. Then we can
151 // simply record the lookup as a being performed on the base type instead. The addition of
152 // a property on the inner object that would shadow the base type's property can only be
153 // introduced by recompiling the lookup as well.
154 if (isUnnamedCompositeType(base) && owner == base)
155 return true;
156
157 PropertySignature propertySignature;
158 CHECKED_OPT_ASSIGN(propertySignature.type, type(property.type()))
159
160 // Compiler indexes follow document order. On the MO, aliases come after regular properties.
161 propertySignature.relativeIndex = property.isAlias()
162 ? property.index() + ownRegularPropertyCountAfterIndex(owner, property.index())
163 : property.index() - ownAliasCountBeforeIndex(owner, property.index());
164
165 Lookup lookup;
166 CHECKED_OPT_ASSIGN(lookup.base, type(base))
167 lookup.member = name;
168
169 m_signatures.insert(lookup, propertySignature);
170 return true;
171}
172
173bool QQmlJSLookupSignaturesRecorder::recordMethodLookup(const QQmlJSScope::ConstPtr &base,
174 const QQmlJSMetaMethod &method)
175{
176 using namespace QQmlPrivate::AOTLookupValidation;
177
178 const QString &name = method.methodName();
179 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfMethod(base, name);
180 if (base->isScript() || safeBase(base) || cantDesync(owner))
181 return true;
182
183 // See recordPropertyLookup
184 if (isUnnamedCompositeType(base) && owner == base)
185 return true;
186
187 // destroy and toString are special?
188 if (name == QStringLiteral("destroy") || name == QStringLiteral("toString"))
189 return true;
190
191 MethodSignature methodSignature;
192
193 if (method.methodType() == QQmlSA::MethodType::Signal) {
194 methodSignature.isSignal = IsSignal::Yes;
195 int index = int(method.methodIndex());
196 methodSignature.relativeIndex = index - ownRegularMethodCountBeforeIndex(owner, index);
197 } else {
198 methodSignature.isSignal = IsSignal::No;
199 int index = int(method.methodIndex());
200 methodSignature.relativeIndex = index + ownSignalCountAfterIndex(owner, index);
201 }
202
203 methodSignature.types.push_back({});
204 CHECKED_OPT_ASSIGN(methodSignature.types.back(), type(method.returnType()))
205 for (const auto &param : method.parameters()) {
206 methodSignature.paramNames.push_back(param.name());
207 methodSignature.types.push_back({});
208 CHECKED_OPT_ASSIGN(methodSignature.types.back(), type(param.type()))
209 }
210
211 Lookup lookup;
212 CHECKED_OPT_ASSIGN(lookup.base, type(base))
213 lookup.member = name;
214
215 m_signatures.insert(lookup, methodSignature);
216 return true;
217}
218
219bool QQmlJSLookupSignaturesRecorder::recordEnumKeyLookup(const QQmlJSScope::ConstPtr &base,
220 const QQmlJSMetaEnum &metaEnum,
221 const QString &keyName)
222{
223 using namespace QQmlPrivate::AOTLookupValidation;
224
225 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfEnum(base, metaEnum.name());
226 if (base->isScript() || safeBase(base) || cantDesync(owner))
227 return true;
228
229 EnumKeySignature enumSignature;
230 // QTBUG-145053: enums can only hold ints in the Compiler
231 enumSignature.value = quint64(metaEnum.value(keyName));
232 if (metaEnum.isFlag())
233 enumSignature.isFlag = IsFlag::Yes;
234
235 Lookup lookup;
236 CHECKED_OPT_ASSIGN(lookup.base, type(base))
237 lookup.member = keyName;
238 lookup.enumName = metaEnum.name();
239 m_signatures.insert(lookup, enumSignature);
240 return true;
241}
242
243QT_END_NAMESPACE
static int ownAliasCountBeforeIndex(const QQmlJSScope::ConstPtr &type, int index)
static int ownRegularMethodCountBeforeIndex(const QQmlJSScope::ConstPtr &type, int index)
static int ownRegularPropertyCountAfterIndex(const QQmlJSScope::ConstPtr &type, int index)
#define CHECKED_OPT_ASSIGN(var, expr)
static int ownSignalCountAfterIndex(const QQmlJSScope::ConstPtr &type, int index)