7#include <private/qqmljstyperesolver_p.h>
9using namespace Qt::StringLiterals;
13QQmlJSLookupSignaturesRecorder::QQmlJSLookupSignaturesRecorder(
14 const QString ¤tFilePath,
const QQmlJSTypeResolver *typeResolver)
15 : m_currentFilePath(currentFilePath), m_typeResolver(typeResolver)
17 Q_ASSERT(!m_currentFilePath.isEmpty());
18 Q_ASSERT(m_typeResolver);
21#define CHECKED_OPT_ASSIGN(var, expr)
23 const auto opt = expr;
29std::optional<QQmlPrivate::AOTLookupValidation::Type>
30QQmlJSLookupSignaturesRecorder::type(
const QQmlJSScope::ConstPtr &type)
32 using namespace QQmlPrivate::AOTLookupValidation;
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();
40 res.name = type->internalName();
46 res.isComposite = IsComposite::Yes;
47 if (isUnnamedCompositeType(type)) {
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()));
57 return QQmlJSLookupSignaturesRecorder::type(type->baseType());
60 QQmlJSScope::ConstPtr root = type;
61 while (!root->isFileRootComponent())
62 root = root->parentScope();
64 bool isFileBeingCompiled = root->filePath() == m_typeResolver->logger()->filePath();
66 Q_ASSERT(!root->internalName().isEmpty());
67 res.module = isFileBeingCompiled ? s_thisCuModule : root->moduleName();
68 res.name = isFileBeingCompiled ? s_thisCuType : root->internalName();
70 if (type->isInlineComponent()) {
71 res.isInlineComponent = IsIC::Yes;
72 res.icNameOrExtensionTypeName = *type->inlineComponentName();
79bool QQmlJSLookupSignaturesRecorder::cantDesync(
const QQmlJSScope::ConstPtr &type)
const
81 Q_ASSERT(!m_typeResolver->logger()->filePath().isEmpty());
83 return type->filePath() == m_typeResolver->logger()->filePath();
86bool QQmlJSLookupSignaturesRecorder::safeBase(
const QQmlJSScope::ConstPtr &base)
const
88 return base == m_typeResolver->varType() || base == m_typeResolver->jsValueType()
89 || base->inherits(m_typeResolver->qmlPropertyMapType());
92bool QQmlJSLookupSignaturesRecorder::isUnnamedCompositeType(
const QQmlJSScope::ConstPtr &type)
const
94 return type->isComposite() && !type->isFileRootComponent() && !type->isInlineComponent();
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;
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;
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;
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;
136bool QQmlJSLookupSignaturesRecorder::recordPropertyLookup(
const QQmlJSScope::ConstPtr &base,
137 const QQmlJSMetaProperty &property)
139 using namespace QQmlPrivate::AOTLookupValidation;
141 const QString &name = property.propertyName();
142 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfProperty(base, name);
143 if (base->isScript() || safeBase(base) || cantDesync(owner))
154 if (isUnnamedCompositeType(base) && owner == base)
157 PropertySignature propertySignature;
161 propertySignature.relativeIndex = property.isAlias()
162 ? property.index() + ownRegularPropertyCountAfterIndex(owner, property.index())
163 : property.index() - ownAliasCountBeforeIndex(owner, property.index());
167 lookup.member = name;
169 m_signatures.insert(lookup, propertySignature);
173bool QQmlJSLookupSignaturesRecorder::recordMethodLookup(
const QQmlJSScope::ConstPtr &base,
174 const QQmlJSMetaMethod &method)
176 using namespace QQmlPrivate::AOTLookupValidation;
178 const QString &name = method.methodName();
179 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfMethod(base, name);
180 if (base->isScript() || safeBase(base) || cantDesync(owner))
184 if (isUnnamedCompositeType(base) && owner == base)
188 if (name == QStringLiteral(
"destroy") || name == QStringLiteral(
"toString"))
191 MethodSignature methodSignature;
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);
198 methodSignature.isSignal = IsSignal::No;
199 int index =
int(method.methodIndex());
200 methodSignature.relativeIndex = index + ownSignalCountAfterIndex(owner, index);
203 methodSignature.types.push_back({});
205 for (
const auto ¶m : method.parameters()) {
206 methodSignature.paramNames.push_back(param.name());
207 methodSignature.types.push_back({});
213 lookup.member = name;
215 m_signatures.insert(lookup, methodSignature);
219bool QQmlJSLookupSignaturesRecorder::recordEnumKeyLookup(
const QQmlJSScope::ConstPtr &base,
220 const QQmlJSMetaEnum &metaEnum,
221 const QString &keyName)
223 using namespace QQmlPrivate::AOTLookupValidation;
225 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfEnum(base, metaEnum.name());
226 if (base->isScript() || safeBase(base) || cantDesync(owner))
229 EnumKeySignature enumSignature;
231 enumSignature.value = quint64(metaEnum.value(keyName));
232 if (metaEnum.isFlag())
233 enumSignature.isFlag = IsFlag::Yes;
237 lookup.member = keyName;
238 lookup.enumName = metaEnum.name();
239 m_signatures.insert(lookup, enumSignature);
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)