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());
125bool QQmlJSLookupSignaturesRecorder::isUnnamedCompositeType(
const QQmlJSScope::ConstPtr &type)
const
127 return type->isComposite() && !type->isFileRootComponent() && !type->isInlineComponent();
133 const auto &ms = type->ownMethods();
134 return std::count_if(ms.cbegin(), ms.cend(), [&](
const QQmlJSMetaMethod &m) {
135 Q_ASSERT(m.isConstructor() || m.methodIndex() != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
136 return m.methodType() != QQmlSA::MethodType::Signal && !m.isConstructor()
137 &&
int(m.methodIndex()) < index;
144 const auto &ms = type->ownMethods();
145 return std::count_if(ms.cbegin(), ms.cend(), [&](
const QQmlJSMetaMethod &m) {
146 Q_ASSERT(m.isConstructor() || m.methodIndex() != QQmlJSMetaMethod::RelativeFunctionIndex::Invalid);
147 return m.methodType() == QQmlSA::MethodType::Signal &&
int(m.methodIndex()) > index;
154 const auto &ps = type->ownProperties();
155 return std::count_if(ps.cbegin(), ps.cend(), [&](
const auto &p) {
156 return p.isAlias() && p.index() < index;
163 const auto &ps = type->ownProperties();
164 return std::count_if(ps.cbegin(), ps.cend(), [&](
const auto &p) {
165 return !p.isAlias() && p.index() > index;
169bool QQmlJSLookupSignaturesRecorder::recordPropertyLookup(
const QQmlJSScope::ConstPtr &base,
170 const QQmlJSMetaProperty &property)
172 using namespace QQmlPrivate::AOTLookupValidation;
174 const QString &name = property.propertyName();
175 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfProperty(base, name);
176 if (base->isScript() || safeBase(base) || cantDesync(owner))
187 if (isUnnamedCompositeType(base) && owner == base)
190 PropertySignature propertySignature;
194 propertySignature.relativeIndex = property.isAlias()
195 ? property.index() + ownRegularPropertyCountAfterIndex(owner, property.index())
196 : property.index() - ownAliasCountBeforeIndex(owner, property.index());
200 lookup.member = name;
202 m_signatures.insert(lookup, propertySignature);
206bool QQmlJSLookupSignaturesRecorder::recordMethodLookup(
const QQmlJSScope::ConstPtr &base,
207 const QQmlJSMetaMethod &method)
209 using namespace QQmlPrivate::AOTLookupValidation;
211 const QString &name = method.methodName();
212 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfMethod(base, name);
213 if (base->isScript() || safeBase(base) || cantDesync(owner))
217 if (isUnnamedCompositeType(base) && owner == base)
221 if (name == QStringLiteral(
"destroy") || name == QStringLiteral(
"toString"))
224 MethodSignature methodSignature;
226 if (method.methodType() == QQmlSA::MethodType::Signal) {
227 methodSignature.isSignal = IsSignal::Yes;
228 int index =
int(method.methodIndex());
229 methodSignature.relativeIndex = index - ownRegularMethodCountBeforeIndex(owner, index);
231 methodSignature.isSignal = IsSignal::No;
232 int index =
int(method.methodIndex());
233 methodSignature.relativeIndex = index + ownSignalCountAfterIndex(owner, index);
236 methodSignature.types.push_back({});
238 for (
const auto ¶m : method.parameters()) {
239 methodSignature.paramNames.push_back(param.name());
240 methodSignature.types.push_back({});
246 lookup.member = name;
248 m_signatures.insert(lookup, methodSignature);
252bool QQmlJSLookupSignaturesRecorder::recordEnumKeyLookup(
const QQmlJSScope::ConstPtr &base,
253 const QQmlJSMetaEnum &metaEnum,
254 const QString &keyName)
256 using namespace QQmlPrivate::AOTLookupValidation;
258 const auto [owner, extensionSpecifier] = QQmlJSScope::ownerOfEnum(base, metaEnum.name());
259 if (base->isScript() || safeBase(base) || cantDesync(owner))
262 EnumKeySignature enumSignature;
264 enumSignature.value = quint64(metaEnum.value(keyName));
265 if (metaEnum.isFlag())
266 enumSignature.isFlag = IsFlag::Yes;
270 lookup.member = keyName;
271 lookup.enumName = metaEnum.name();
272 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)