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 if (isFileBeingCompiled) {
68 res.module = s_thisCuModule;
69 res.name = s_thisCuType;
74 const auto imports = m_typeResolver->importer()->versionedImports(root->filePath());
75 const auto import = std::find_if(imports.cbegin(), imports.cend(),
76 [&](
const QQmlJSImporter::VersionedImport &import) {
77 return import.module == root->moduleName();
79 if (import == imports.cend()) {
80 m_rejectMessage =
"%1 is not imported through its module %2"_L1.arg(root->filePath(),
85 res.module = import->module;
86 res.name = import->name;
87 res.version = import->version;
91 const QQmlJSScope::ConstPtr found =
92 m_typeResolver->importer()
93 ->importModule(res.module, 0, QString(), res.version)
96 if (found && !found->isComposite()) {
97 m_rejectMessage =
"%1 is shadowed by C++ type %2 in module %3"_L1.arg(
98 root->filePath(), res.name, res.module);
103 if (type->isInlineComponent()) {
104 res.isInlineComponent = IsIC::Yes;
105 res.icNameOrExtensionTypeName = *type->inlineComponentName();
112bool QQmlJSLookupSignaturesRecorder::cantDesync(
const QQmlJSScope::ConstPtr &type)
const
114 Q_ASSERT(!m_typeResolver->logger()->filePath().isEmpty());
116 return type->filePath() == m_typeResolver->logger()->filePath();
119bool QQmlJSLookupSignaturesRecorder::safeBase(
const QQmlJSScope::ConstPtr &base)
const
121 return base == m_typeResolver->varType() || base == m_typeResolver->jsValueType()
122 || base->inherits(m_typeResolver->qmlPropertyMapType())
123 || base->inherits(m_typeResolver->variantMapType())
124 || base->inherits(m_typeResolver->jsPrimitiveType());
127bool QQmlJSLookupSignaturesRecorder::isUnnamedCompositeType(
const QQmlJSScope::ConstPtr &type)
const
129 return type->isComposite() && !type->isFileRootComponent() && !type->isInlineComponent();
135 const auto &ms = type->ownMethods();
136 return std::count_if(ms.cbegin(), ms.cend(), [&](
const QQmlJSMetaMethod &m) {
137 Q_ASSERT(m.isConstructor() || m.methodIndex() != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
138 return m.methodType() != QQmlSA::MethodType::Signal && !m.isConstructor()
139 &&
int(m.methodIndex()) < index;
146 const auto &ms = type->ownMethods();
147 return std::count_if(ms.cbegin(), ms.cend(), [&](
const QQmlJSMetaMethod &m) {
148 Q_ASSERT(m.isConstructor() || m.methodIndex() != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
149 return m.methodType() == QQmlSA::MethodType::Signal &&
int(m.methodIndex()) > index;
156 const auto &ps = type->ownProperties();
157 return std::count_if(ps.cbegin(), ps.cend(), [&](
const auto &p) {
158 return p.isAlias() && p.index() < index;
165 const auto &ps = type->ownProperties();
166 return std::count_if(ps.cbegin(), ps.cend(), [&](
const auto &p) {
167 return !p.isAlias() && p.index() > index;
171bool QQmlJSLookupSignaturesRecorder::recordPropertyLookup(
const QQmlJSScope::ConstPtr &base,
172 const QQmlJSMetaProperty &property)
174 using namespace QQmlPrivate::AOTLookupValidation;
176 const QString &name = property.propertyName();
177 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfProperty(base, name);
178 if (base->isScript() || safeBase(base) || cantDesync(owner))
189 if (isUnnamedCompositeType(base) && owner == base)
192 PropertySignature propertySignature;
196 propertySignature.relativeIndex = property.isAlias()
197 ? property.index() + ownRegularPropertyCountAfterIndex(owner, property.index())
198 : property.index() - ownAliasCountBeforeIndex(owner, property.index());
202 lookup.member = name;
204 m_signatures.insert(lookup, propertySignature);
208bool QQmlJSLookupSignaturesRecorder::recordMethodLookup(
const QQmlJSScope::ConstPtr &base,
209 const QQmlJSMetaMethod &method)
211 using namespace QQmlPrivate::AOTLookupValidation;
213 const QString &name = method.methodName();
214 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfMethod(base, name);
215 if (base->isScript() || safeBase(base) || cantDesync(owner))
219 if (isUnnamedCompositeType(base) && owner == base)
223 if (name == QStringLiteral(
"destroy") || name == QStringLiteral(
"toString"))
226 MethodSignature methodSignature;
228 if (method.methodType() == QQmlSA::MethodType::Signal) {
229 methodSignature.isSignal = IsSignal::Yes;
230 int index =
int(method.methodIndex());
231 methodSignature.relativeIndex = index - ownRegularMethodCountBeforeIndex(owner, index);
233 methodSignature.isSignal = IsSignal::No;
234 int index =
int(method.methodIndex());
235 methodSignature.relativeIndex = index + ownSignalCountAfterIndex(owner, index);
238 methodSignature.types.push_back({});
240 for (
const auto ¶m : method.parameters()) {
241 methodSignature.paramNames.push_back(param.name());
242 methodSignature.types.push_back({});
248 lookup.member = name;
250 m_signatures.insert(lookup, methodSignature);
254bool QQmlJSLookupSignaturesRecorder::recordEnumKeyLookup(
const QQmlJSScope::ConstPtr &base,
255 const QQmlJSMetaEnum &metaEnum,
256 const QString &keyName)
258 using namespace QQmlPrivate::AOTLookupValidation;
260 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfEnum(base, metaEnum.name());
261 if (base->isScript() || safeBase(base) || cantDesync(owner))
264 EnumKeySignature enumSignature;
266 enumSignature.value = quint64(metaEnum.value(keyName));
267 if (metaEnum.isFlag())
268 enumSignature.isFlag = IsFlag::Yes;
272 lookup.member = keyName;
273 lookup.enumName = metaEnum.name();
274 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)