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
97static QQmlJSMetaMethod::RelativeFunctionIndex methodIndex(const QQmlJSMetaMethod &m)
98{
99 return m.otherMethodIndex() == QQmlJSMetaMethod::RelativeFunctionIndex::Invalid
100 ? m.methodIndex() : m.otherMethodIndex();
101}
102
103// On the MetaObject, signals come before regular functions
104static int ownRegularMethodCountBeforeIndex(const QQmlJSScope::ConstPtr &type, int index)
105{
106 const auto &ms = type->ownMethods();
107 return std::count_if(ms.cbegin(), ms.cend(), [&](const QQmlJSMetaMethod &m) {
108 Q_ASSERT(m.isConstructor() || methodIndex(m) != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
109 return m.methodType() != QQmlSA::MethodType::Signal && !m.isConstructor()
110 && int(methodIndex(m)) < index;
111 });
112}
113
114// On the MetaObject, signals come before regular functions
115static int ownSignalCountAfterIndex(const QQmlJSScope::ConstPtr &type, int index)
116{
117 const auto &ms = type->ownMethods();
118 return std::count_if(ms.cbegin(), ms.cend(), [&](const QQmlJSMetaMethod &m) {
119 Q_ASSERT(m.isConstructor() || methodIndex(m) != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
120 return m.methodType() == QQmlSA::MethodType::Signal && int(methodIndex(m)) > index;
121 });
122}
123
124// On the MetaObject, regular properties come before aliases
125static int ownAliasCountBeforeIndex(const QQmlJSScope::ConstPtr &type, int index)
126{
127 const auto &ps = type->ownProperties();
128 return std::count_if(ps.cbegin(), ps.cend(), [&](const auto &p) {
129 return p.isAlias() && p.index() < index;
130 });
131}
132
133// On the MetaObject, regular properties come before aliases
134static int ownRegularPropertyCountAfterIndex(const QQmlJSScope::ConstPtr &type, int index)
135{
136 const auto &ps = type->ownProperties();
137 return std::count_if(ps.cbegin(), ps.cend(), [&](const auto &p) {
138 return !p.isAlias() && p.index() > index;
139 });
140}
141
142bool QQmlJSLookupSignaturesRecorder::recordPropertyLookup(const QQmlJSScope::ConstPtr &base,
143 const QQmlJSMetaProperty &property)
144{
145 using namespace QQmlPrivate::AOTLookupValidation;
146
147 const QString &name = property.propertyName();
148 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfProperty(base, name);
149 if (base->isScript() || safeBase(base) || cantDesync(owner))
150 return true;
151
152 // When performing a lookup on an inner object of unnamed type, one of two things is true:
153 // 1) The target property (or method) is declared within the object. Then, because the object
154 // is unnamed, it must be defined in the same file as the lookup and they cannot desync and
155 // we don't have to record it.
156 // 2) Or the target property is declared on a base type of the inner object. Then we can
157 // simply record the lookup as a being performed on the base type instead. The addition of
158 // a property on the inner object that would shadow the base type's property can only be
159 // introduced by recompiling the lookup as well.
160 if (isUnnamedCompositeType(base) && owner == base)
161 return true;
162
163 PropertySignature propertySignature;
164 CHECKED_OPT_ASSIGN(propertySignature.type, type(property.type()))
165
166 // Compiler indexes follow document order. On the MO, aliases come after regular properties.
167 propertySignature.relativeIndex = property.isAlias()
168 ? property.index() + ownRegularPropertyCountAfterIndex(owner, property.index())
169 : property.index() - ownAliasCountBeforeIndex(owner, property.index());
170
171 Lookup lookup;
172 CHECKED_OPT_ASSIGN(lookup.base, type(base))
173 lookup.member = name;
174
175 m_signatures.insert(lookup, propertySignature);
176 return true;
177}
178
179bool QQmlJSLookupSignaturesRecorder::recordMethodLookup(const QQmlJSScope::ConstPtr &base,
180 const QQmlJSMetaMethod &method)
181{
182 using namespace QQmlPrivate::AOTLookupValidation;
183
184 const QString &name = method.methodName();
185 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfMethod(base, name);
186 if (base->isScript() || safeBase(base) || cantDesync(owner))
187 return true;
188
189 // See recordPropertyLookup
190 if (isUnnamedCompositeType(base) && owner == base)
191 return true;
192
193 // destroy and toString are special?
194 if (name == QStringLiteral("destroy") || name == QStringLiteral("toString"))
195 return true;
196
197 MethodSignature methodSignature;
198
199 if (method.methodType() == QQmlSA::MethodType::Signal) {
200 methodSignature.isSignal = IsSignal::Yes;
201 int index = int(methodIndex(method));
202 methodSignature.relativeIndex = index - ownRegularMethodCountBeforeIndex(owner, index);
203 } else {
204 methodSignature.isSignal = IsSignal::No;
205 int index = int(methodIndex(method));
206 methodSignature.relativeIndex = index + ownSignalCountAfterIndex(owner, index);
207 }
208
209 methodSignature.types.push_back({});
210 CHECKED_OPT_ASSIGN(methodSignature.types.back(), type(method.returnType()))
211 for (const auto &param : method.parameters()) {
212 methodSignature.paramNames.push_back(param.name());
213 methodSignature.types.push_back({});
214 CHECKED_OPT_ASSIGN(methodSignature.types.back(), type(param.type()))
215 }
216
217 Lookup lookup;
218 CHECKED_OPT_ASSIGN(lookup.base, type(base))
219 lookup.member = name;
220
221 m_signatures.insert(lookup, methodSignature);
222 return true;
223}
224
225bool QQmlJSLookupSignaturesRecorder::recordEnumKeyLookup(const QQmlJSScope::ConstPtr &base,
226 const QQmlJSMetaEnum &metaEnum,
227 const QString &keyName)
228{
229 using namespace QQmlPrivate::AOTLookupValidation;
230
231 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfEnum(base, metaEnum.name());
232 if (base->isScript() || safeBase(base) || cantDesync(owner))
233 return true;
234
235 EnumKeySignature enumSignature;
236 // QTBUG-145053: enums can only hold ints in the Compiler
237 enumSignature.value = quint64(metaEnum.value(keyName));
238 if (metaEnum.isFlag())
239 enumSignature.isFlag = IsFlag::Yes;
240
241 Lookup lookup;
242 CHECKED_OPT_ASSIGN(lookup.base, type(base))
243 lookup.member = keyName;
244 lookup.enumName = metaEnum.name();
245 m_signatures.insert(lookup, enumSignature);
246 return true;
247}
248
249QT_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 QQmlJSMetaMethod::RelativeFunctionIndex methodIndex(const QQmlJSMetaMethod &m)
static int ownSignalCountAfterIndex(const QQmlJSScope::ConstPtr &type, int index)