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
qqmljsscope_p.h
Go to the documentation of this file.
1// Copyright (C) 2019 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
5#ifndef QQMLJSSCOPE_P_H
6#define QQMLJSSCOPE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17
18#include <qtqmlcompilerexports.h>
19
24#include "qqmlsa_p.h"
25
26#include <QtQml/private/qqmljssourcelocation_p.h>
27
28#include <QtCore/qfileinfo.h>
29#include <QtCore/qhash.h>
30#include <QtCore/qset.h>
31#include <QtCore/qstring.h>
32#include <QtCore/qversionnumber.h>
34
35#include <optional>
36
37QT_BEGIN_NAMESPACE
38
39class QQmlJSImporter;
40
41namespace QQmlJS {
42
44
45class Export {
46public:
47 Export() = default;
48 Export(QString package, QString type, QTypeRevision version, QTypeRevision revision);
49
50 bool isValid() const;
51
52 QString package() const { return m_package; }
53 QString type() const { return m_type; }
54 QTypeRevision version() const { return m_version; }
55 QTypeRevision revision() const { return m_revision; }
56
57private:
58 QString m_package;
59 QString m_type;
60 QTypeRevision m_version;
61 QTypeRevision m_revision;
62};
63
64template<typename Pointer>
66 Pointer scope;
68};
69
70template<typename Pointer>
75
76struct ContextualTypes;
77
78} // namespace QQmlJS
79
80class Q_QMLCOMPILER_EXPORT QQmlJSScope
81{
82 friend QQmlSA::Element;
83
84public:
85 using Ptr = QDeferredSharedPointer<QQmlJSScope>;
86 using WeakPtr = QDeferredWeakPointer<QQmlJSScope>;
87 using ConstPtr = QDeferredSharedPointer<const QQmlJSScope>;
88 using WeakConstPtr = QDeferredWeakPointer<const QQmlJSScope>;
89
90 using AccessSemantics = QQmlSA::AccessSemantics;
91 using ScopeType = QQmlSA::ScopeType;
92
93 using InlineComponentNameType = QString;
94 using RootDocumentNameType = std::monostate; // an empty type that has std::hash
95 /*!
96 * A Hashable type to differentiate document roots from different inline components.
97 */
98 using InlineComponentOrDocumentRootName =
99 std::variant<InlineComponentNameType, RootDocumentNameType>;
100
101 enum Flag {
102 Creatable = 0x1,
103 Composite = 0x2,
104 JavaScriptBuiltin = 0x4,
105 Singleton = 0x8,
106 Script = 0x10,
107 CustomParser = 0x20,
108 Array = 0x40,
109 InlineComponent = 0x80,
110 WrappedInImplicitComponent = 0x100,
111 HasBaseTypeError = 0x200,
112 ExtensionIsNamespace = 0x400,
113 IsListProperty = 0x800,
114 Structured = 0x1000,
115 ExtensionIsJavaScript = 0x2000,
116 EnforcesScopedEnums = 0x4000,
117 FileRootComponent = 0x8000,
118 AssignedToUnknownProperty = 0x10000,
119 };
120 Q_DECLARE_FLAGS(Flags, Flag)
121
122 using Export = QQmlJS::Export;
123 template <typename Pointer>
124 using ImportedScope = QQmlJS::ImportedScope<Pointer>;
125 template <typename Pointer>
126 using ExportedScope = QQmlJS::ExportedScope<Pointer>;
127
128 struct JavaScriptIdentifier
129 {
130 enum Kind {
131 Parameter,
132 FunctionScoped,
133 LexicalScoped,
134 Injected
135 };
136
137 Kind kind = FunctionScoped;
138 QQmlJS::SourceLocation location;
139 std::optional<QString> typeName;
140 bool isConst = false;
141 QQmlJSScope::WeakConstPtr scope = {};
142 };
143
144 enum BindingTargetSpecifier {
145 SimplePropertyTarget, // e.g. `property int p: 42`
146 ListPropertyTarget, // e.g. `property list<Item> pList: [ Text {} ]`
147 UnnamedPropertyTarget // default property bindings, where property name is unspecified
148 };
149
150 template <typename Key, typename Value>
151 using QMultiHashRange = std::pair<typename QMultiHash<Key, Value>::iterator,
152 typename QMultiHash<Key, Value>::iterator>;
153
154 static QQmlJSScope::Ptr create() { return QSharedPointer<QQmlJSScope>(new QQmlJSScope); }
155 static QQmlJSScope::Ptr clone(const QQmlJSScope::ConstPtr &origin);
156
157 static void cloneInto(QQmlJSScope::Ptr &origin,
158 const QQmlJSScope::Ptr &target)
159 {
160 *target = std::move(*clone(origin));
161 }
162
163 //! \internal This should probably restricted, only needed in prepareTargetForVisit
164 void resetForReparse() {
165 const QString moduleName = this->moduleName();
166 *this = QQmlJSScope { this->internalName() };
167 setOwnModuleName(moduleName);
168 }
169
170 static QQmlJSScope::ConstPtr findCurrentQMLScope(const QQmlJSScope::ConstPtr &scope);
171
172 QQmlJSScope::Ptr parentScope();
173 QQmlJSScope::ConstPtr parentScope() const;
174 static void reparent(const QQmlJSScope::Ptr &parentScope, const QQmlJSScope::Ptr &childScope);
175
176 void insertJSIdentifier(const QString &name, const JavaScriptIdentifier &identifier);
177 QHash<QString, JavaScriptIdentifier> ownJSIdentifiers() const;
178 void insertPropertyIdentifier(const QQmlJSMetaProperty &prop);
179
180 ScopeType scopeType() const { return m_scopeType; }
181 void setScopeType(ScopeType type) { m_scopeType = type; }
182
183 void addOwnMethod(const QQmlJSMetaMethod &method) { m_methods.insert(method.methodName(), method); }
184 QMultiHashRange<QString, QQmlJSMetaMethod> mutableOwnMethodsRange(const QString &name)
185 {
186 return m_methods.equal_range(name);
187 }
188 QMultiHash<QString, QQmlJSMetaMethod> ownMethods() const { return m_methods; }
189 QList<QQmlJSMetaMethod> ownMethods(const QString &name) const { return m_methods.values(name); }
190 bool hasOwnMethod(const QString &name) const { return m_methods.contains(name); }
191
192 bool hasMethod(const QString &name) const;
193 QHash<QString, QQmlJSMetaMethod> methods() const;
194 QList<QQmlJSMetaMethod> methods(const QString &name) const;
195 QList<QQmlJSMetaMethod> methods(const QString &name, QQmlJSMetaMethodType type) const;
196
197 void addOwnEnumeration(const QQmlJSMetaEnum &enumeration) { m_enumerations.insert(enumeration.name(), enumeration); }
198 QHash<QString, QQmlJSMetaEnum> ownEnumerations() const { return m_enumerations; }
199 QQmlJSMetaEnum ownEnumeration(const QString &name) const { return m_enumerations.value(name); }
200 bool hasOwnEnumeration(const QString &name) const { return m_enumerations.contains(name); }
201
202 bool hasEnumeration(const QString &name) const;
203 bool hasEnumerationKey(const QString &name) const;
204 bool hasOwnEnumerationKey(const QString &name) const;
205 QQmlJSMetaEnum enumeration(const QString &name) const;
206 QHash<QString, QQmlJSMetaEnum> enumerations() const;
207
208 void setAnnotations(const QList<QQmlJSAnnotation> &annotation) { m_annotations = std::move(annotation); }
209 const QList<QQmlJSAnnotation> &annotations() const { return m_annotations; }
210
211 QString filePath() const { return m_filePath; }
212 void setFilePath(const QString &file) { m_filePath = file; }
213
214 quint32 lineNumber() const { return m_sourceLocation.startLine; }
215 void setLineNumber(quint32 lineNumber);
216
217 // The name the type uses to refer to itself. Either C++ class name or base name of
218 // QML file. isComposite tells us if this is a C++ or a QML name.
219 QString internalName() const { return m_internalName; }
220 void setInternalName(const QString &internalName) { m_internalName = internalName; }
221 QString augmentedInternalName() const;
222
223 // This returns a more user readable version of internalName / baseTypeName
224 static QString prettyName(QAnyStringView name);
225
226 enum class IsComponentRoot : quint8 { No = 0, Yes, Maybe };
227 IsComponentRoot componentRootStatus() const;
228
229 void setAliases(const QStringList &aliases) { m_aliases = aliases; }
230 QStringList aliases() const { return m_aliases; }
231
232 void setInterfaceNames(const QStringList& interfaces) { m_interfaceNames = interfaces; }
233 QStringList interfaceNames() const { return m_interfaceNames; }
234
235 bool hasInterface(const QString &name) const;
236 bool hasOwnInterface(const QString &name) const { return m_interfaceNames.contains(name); }
237
238 void setOwnDeferredNames(const QStringList &names) { m_ownDeferredNames = names; }
239 QStringList ownDeferredNames() const { return m_ownDeferredNames; }
240 void setOwnImmediateNames(const QStringList &names) { m_ownImmediateNames = names; }
241 QStringList ownImmediateNames() const { return m_ownImmediateNames; }
242
243 bool isNameDeferred(const QString &name) const;
244
245 // If isComposite(), this is the QML/JS name of the prototype. Otherwise it's the
246 // relevant base class (in the hierarchy starting from QObject) of a C++ type.
247 void setBaseTypeName(const QString &baseTypeName);
248 QString baseTypeName() const;
249
250 QQmlJSScope::ConstPtr baseType() const { return m_baseType.scope; }
251 QTypeRevision baseTypeRevision() const { return m_baseType.revision; }
252
253 QString moduleName() const;
254 QString ownModuleName() const { return m_moduleName; }
255 void setOwnModuleName(const QString &moduleName) { m_moduleName = moduleName; }
256
257 void clearBaseType() { m_baseType = {}; }
258 void setBaseTypeError(const QString &baseTypeError);
259 QString baseTypeError() const;
260
261 void addOwnProperty(const QQmlJSMetaProperty &prop) { m_properties.insert(prop.propertyName(), prop); }
262 QHash<QString, QQmlJSMetaProperty> ownProperties() const { return m_properties; }
263 QQmlJSMetaProperty ownProperty(const QString &name) const { return m_properties.value(name); }
264 bool hasOwnProperty(const QString &name) const { return m_properties.contains(name); }
265
266 bool hasProperty(const QString &name) const;
267 QQmlJSMetaProperty property(const QString &name) const;
268 QHash<QString, QQmlJSMetaProperty> properties() const;
269
270 void setPropertyLocallyRequired(const QString &name, bool isRequired);
271 bool isPropertyRequired(const QString &name) const;
272 bool isPropertyLocallyRequired(const QString &name) const;
273
274 void addOwnPropertyBinding(
275 const QQmlJSMetaPropertyBinding &binding,
276 BindingTargetSpecifier specifier = BindingTargetSpecifier::SimplePropertyTarget);
277 QMultiHash<QString, QQmlJSMetaPropertyBinding> ownPropertyBindings() const;
278 std::pair<QMultiHash<QString, QQmlJSMetaPropertyBinding>::const_iterator,
279 QMultiHash<QString, QQmlJSMetaPropertyBinding>::const_iterator>
280 ownPropertyBindings(const QString &name) const;
281 QList<QQmlJSMetaPropertyBinding> ownPropertyBindingsInQmlIROrder() const;
282 bool hasOwnPropertyBindings(const QString &name) const;
283
284 bool hasPropertyBindings(const QString &name) const;
285 QList<QQmlJSMetaPropertyBinding> propertyBindings(const QString &name) const;
286
287 struct AnnotatedScope; // defined later
288 static AnnotatedScope ownerOfProperty(const QQmlJSScope::ConstPtr &self, const QString &name);
289 static AnnotatedScope ownerOfMethod(const QQmlJSScope::ConstPtr &self, const QString &name);
290
291 bool isResolved() const;
292 bool isFullyResolved() const;
293
294 QString ownDefaultPropertyName() const { return m_defaultPropertyName; }
295 void setOwnDefaultPropertyName(const QString &name) { m_defaultPropertyName = name; }
296 QString defaultPropertyName() const;
297
298 QString ownParentPropertyName() const { return m_parentPropertyName; }
299 void setOwnParentPropertyName(const QString &name) { m_parentPropertyName = name; }
300 QString parentPropertyName() const;
301
302 QString ownAttachedTypeName() const { return m_attachedTypeName; }
303 void setOwnAttachedTypeName(const QString &name) { m_attachedTypeName = name; }
304 QQmlJSScope::ConstPtr ownAttachedType() const { return m_attachedType; }
305
306 QString attachedTypeName() const;
307 QQmlJSScope::ConstPtr attachedType() const;
308
309 QString extensionTypeName() const { return m_extensionTypeName; }
310 void setExtensionTypeName(const QString &name) { m_extensionTypeName = name; }
311 enum ExtensionKind {
312 NotExtension,
313 ExtensionType,
314 ExtensionJavaScript,
315 ExtensionNamespace,
316 };
317 struct AnnotatedScope
318 {
319 QQmlJSScope::ConstPtr scope;
320 ExtensionKind extensionSpecifier = NotExtension;
321 };
322 AnnotatedScope extensionType() const;
323
324 QString elementTypeName() const { return m_elementTypeName; }
325 void setElementTypeName(const QString &name) { m_elementTypeName = name; }
326 QQmlJSScope::ConstPtr elementType() const { return m_elementType; }
327 QQmlJSScope::ConstPtr listType() const { return m_listType; }
328 QQmlJSScope::Ptr listType() { return m_listType; }
329
330 void addOwnRuntimeFunctionIndex(QQmlJSMetaMethod::AbsoluteFunctionIndex index);
331 QQmlJSMetaMethod::AbsoluteFunctionIndex
332 ownRuntimeFunctionIndex(QQmlJSMetaMethod::RelativeFunctionIndex index) const;
333
334
335 /*!
336 * \internal
337 *
338 * Returns true for objects defined from Qml, and false for objects declared from C++.
339 */
340 bool isComposite() const { return m_flags.testFlag(Composite); }
341 void setIsComposite(bool v) { m_flags.setFlag(Composite, v); }
342
343 /*!
344 * \internal
345 *
346 * Returns true for JavaScript types, false for QML and C++ types.
347 */
348 bool isJavaScriptBuiltin() const { return m_flags.testFlag(JavaScriptBuiltin); }
349 void setIsJavaScriptBuiltin(bool v) { m_flags.setFlag(JavaScriptBuiltin, v); }
350
351 QString metaObjectHash() const { return m_metaObjectHash; }
352 void setMetaObjectHash(const QString &hash) { m_metaObjectHash = hash; }
353
354 bool isScript() const { return m_flags.testFlag(Script); }
355 void setIsScript(bool v) { m_flags.setFlag(Script, v); }
356
357 bool hasCustomParser() const { return m_flags.testFlag(CustomParser); }
358 void setHasCustomParser(bool v) { m_flags.setFlag(CustomParser, v); }
359
360 bool isArrayScope() const { return m_flags.testFlag(Array); }
361 void setIsArrayScope(bool v) { m_flags.setFlag(Array, v); }
362
363 bool isInlineComponent() const { return m_flags.testFlag(InlineComponent); }
364 void setIsInlineComponent(bool v) { m_flags.setFlag(InlineComponent, v); }
365
366 bool isWrappedInImplicitComponent() const { return m_flags.testFlag(WrappedInImplicitComponent); }
367 void setIsWrappedInImplicitComponent(bool v) { m_flags.setFlag(WrappedInImplicitComponent, v); }
368
369 bool isAssignedToUnknownProperty() const { return m_flags.testFlag(AssignedToUnknownProperty); }
370 void setAssignedToUnknownProperty(bool v) { m_flags.setFlag(AssignedToUnknownProperty, v); }
371
372 bool extensionIsJavaScript() const { return m_flags.testFlag(ExtensionIsJavaScript); }
373 void setExtensionIsJavaScript(bool v) { m_flags.setFlag(ExtensionIsJavaScript, v); }
374
375 bool extensionIsNamespace() const { return m_flags.testFlag(ExtensionIsNamespace); }
376 void setExtensionIsNamespace(bool v) { m_flags.setFlag(ExtensionIsNamespace, v); }
377
378 bool isListProperty() const { return m_flags.testFlag(IsListProperty); }
379 void setIsListProperty(bool v) { m_flags.setFlag(IsListProperty, v); }
380
381 bool isSingleton() const { return m_flags.testFlag(Singleton); }
382 void setIsSingleton(bool v) { m_flags.setFlag(Singleton, v); }
383
384 bool enforcesScopedEnums() const;
385 void setEnforcesScopedEnumsFlag(bool v) { m_flags.setFlag(EnforcesScopedEnums, v); }
386
387 bool isCreatable() const;
388 void setCreatableFlag(bool v) { m_flags.setFlag(Creatable, v); }
389
390 bool isStructured() const;
391 void setStructuredFlag(bool v) { m_flags.setFlag(Structured, v); }
392
393 bool isFileRootComponent() const { return m_flags.testFlag(FileRootComponent); }
394 void setIsRootFileComponentFlag(bool v) { m_flags.setFlag(FileRootComponent, v); }
395
396 void setAccessSemantics(AccessSemantics semantics) { m_semantics = semantics; }
397 AccessSemantics accessSemantics() const { return m_semantics; }
398 bool isReferenceType() const { return m_semantics == QQmlJSScope::AccessSemantics::Reference; }
399 bool isValueType() const { return m_semantics == QQmlJSScope::AccessSemantics::Value; }
400
401 std::optional<JavaScriptIdentifier> jsIdentifier(const QString &id) const;
402 std::optional<JavaScriptIdentifier> ownJSIdentifier(const QString &id) const;
403
404 QQmlJS::ChildScopesIterator childScopesBegin() const { return m_childScopes.constBegin(); }
405 QQmlJS::ChildScopesIterator childScopesEnd() const { return m_childScopes.constEnd(); }
406
407 void setInlineComponentName(const QString &inlineComponentName);
408 std::optional<QString> inlineComponentName() const;
409 InlineComponentOrDocumentRootName enclosingInlineComponentName() const;
410
411 QList<QQmlJSScope::Ptr> childScopes();
412
413 QList<QQmlJSScope::ConstPtr> childScopes() const;
414
415 static QTypeRevision resolveTypes(
416 const Ptr &self, const QQmlJS::ContextualTypes &contextualTypes,
417 QSet<QString> *usedTypes = nullptr);
418 static void resolveNonEnumTypes(
419 const QQmlJSScope::Ptr &self, const QQmlJS::ContextualTypes &contextualTypes,
420 QSet<QString> *usedTypes = nullptr);
421 static void resolveEnums(
422 const QQmlJSScope::Ptr &self, const QQmlJS::ContextualTypes &contextualTypes,
423 QSet<QString> *usedTypes = nullptr);
424 static void resolveList(
425 const QQmlJSScope::Ptr &self, const QQmlJSScope::ConstPtr &arrayType);
426 static void resolveGroup(
427 const QQmlJSScope::Ptr &self, const QQmlJSScope::ConstPtr &baseType,
428 const QQmlJS::ContextualTypes &contextualTypes,
429 QSet<QString> *usedTypes = nullptr);
430
431 void setSourceLocation(const QQmlJS::SourceLocation &sourceLocation);
432 QQmlJS::SourceLocation sourceLocation() const;
433
434 void setIdSourceLocation(const QQmlJS::SourceLocation &sourceLocation);
435 QQmlJS::SourceLocation idSourceLocation() const;
436
437 static QQmlJSScope::ConstPtr nonCompositeBaseType(const QQmlJSScope::ConstPtr &type);
438
439 static QTypeRevision
440 nonCompositeBaseRevision(const ImportedScope<QQmlJSScope::ConstPtr> &scope);
441
442 bool isSameType(const QQmlJSScope::ConstPtr &otherScope) const;
443 bool inherits(const QQmlJSScope::ConstPtr &base) const;
444 bool canAssign(const QQmlJSScope::ConstPtr &derived) const;
445
446 bool isInCustomParserParent() const;
447
448
449 static ImportedScope<QQmlJSScope::ConstPtr> findType(const QString &name,
450 const QQmlJS::ContextualTypes &contextualTypes,
451 QSet<QString> *usedTypes = nullptr);
452
453 static QQmlSA::Element createQQmlSAElement(const ConstPtr &);
454 static QQmlSA::Element createQQmlSAElement(ConstPtr &&);
455 static const QQmlJSScope::ConstPtr &scope(const QQmlSA::Element &);
456 static constexpr qsizetype sizeofQQmlSAElement() { return QQmlSA::Element::sizeofElement; }
457
458private:
459 // the way to construct a QQmlJSScope is via create
460 explicit QQmlJSScope(const QString &internalName);
461 QQmlJSScope(QQmlJSScope &&) = default;
462 QQmlJSScope &operator=(QQmlJSScope &&) = default;
463 /*! \internal
464
465 Minimal information about a QQmlJSMetaPropertyBinding that allows it to
466 be manipulated similarly to QmlIR::Binding.
467 */
468 template <typename T>
469 friend class QTypeInfo; // so that we can Q_DECLARE_TYPEINFO QmlIRCompatibilityBindingData
470 struct QmlIRCompatibilityBindingData
471 {
472 QmlIRCompatibilityBindingData() = default;
473 QmlIRCompatibilityBindingData(const QString &name, quint32 offset)
474 : propertyName(name), sourceLocationOffset(offset)
475 {
476 }
477 QString propertyName; // bound property name
478 quint32 sourceLocationOffset = 0; // binding's source location offset
479 };
480
481 QQmlJSScope() = default;
482 QQmlJSScope(const QQmlJSScope &) = default;
483 QQmlJSScope &operator=(const QQmlJSScope &) = default;
484 static QTypeRevision resolveType(
485 const QQmlJSScope::Ptr &self, const QQmlJS::ContextualTypes &contextualTypes,
486 QSet<QString> *usedTypes);
487 static void updateChildScope(
488 const QQmlJSScope::Ptr &childScope, const QQmlJSScope::Ptr &self,
489 const QQmlJS::ContextualTypes &contextualTypes, QSet<QString> *usedTypes);
490
491 void addOwnPropertyBindingInQmlIROrder(const QQmlJSMetaPropertyBinding &binding,
492 BindingTargetSpecifier specifier);
493 bool hasEnforcesScopedEnumsFlag() const { return m_flags & EnforcesScopedEnums; }
494 bool hasCreatableFlag() const { return m_flags & Creatable; }
495 bool hasStructuredFlag() const { return m_flags & Structured; }
496
497 QHash<QString, JavaScriptIdentifier> m_jsIdentifiers;
498
499 QMultiHash<QString, QQmlJSMetaMethod> m_methods;
500 QHash<QString, QQmlJSMetaProperty> m_properties;
501 QMultiHash<QString, QQmlJSMetaPropertyBinding> m_propertyBindings;
502
503 // a special QmlIR compatibility bindings array, ordered the same way as
504 // bindings in QmlIR::Object
505 QList<QmlIRCompatibilityBindingData> m_propertyBindingsArray;
506
507 // same as QmlIR::Object::runtimeFunctionIndices
508 QList<QQmlJSMetaMethod::AbsoluteFunctionIndex> m_runtimeFunctionIndices;
509
510 QHash<QString, QQmlJSMetaEnum> m_enumerations;
511
512 QList<QQmlJSAnnotation> m_annotations;
513 QList<QQmlJSScope::Ptr> m_childScopes;
514 QQmlJSScope::WeakPtr m_parentScope;
515
516 QString m_filePath;
517 QString m_internalName;
518 QString m_baseTypeNameOrError;
519 QString m_metaObjectHash;
520
521 // We only need the revision for the base type as inheritance is
522 // the only relation between two types where the revisions matter.
523 ImportedScope<QQmlJSScope::WeakConstPtr> m_baseType;
524
525 ScopeType m_scopeType = ScopeType::QMLScope;
526 QStringList m_aliases;
527 QStringList m_interfaceNames;
528 QStringList m_ownDeferredNames;
529 QStringList m_ownImmediateNames;
530
531 QString m_defaultPropertyName;
532 QString m_parentPropertyName;
533 /*! \internal
534 * The attached type name.
535 * This is an internal name, from a c++ type or a synthetic jsrootgen.
536 */
537 QString m_attachedTypeName;
538 QStringList m_requiredPropertyNames;
539 QQmlJSScope::WeakConstPtr m_attachedType;
540
541 /*! \internal
542 * The type name of the list element in case this is a sequence type.
543 * This is an internal name, from a c++ type or a synthetic jsrootgen.
544 */
545 QString m_elementTypeName;
546 QQmlJSScope::WeakConstPtr m_elementType;
547 QQmlJSScope::Ptr m_listType;
548
549 /*!
550 The extension is provided as either a type (QML_{NAMESPACE_}EXTENDED) or as a
551 namespace (QML_EXTENDED_NAMESPACE).
552 The bool HasExtensionNamespace helps differentiating both cases, as namespaces
553 have a more limited lookup capaility.
554 This is an internal name, from a c++ type or a synthetic jsrootgen.
555 */
556 QString m_extensionTypeName;
557 QQmlJSScope::WeakConstPtr m_extensionType;
558
559 Flags m_flags = Creatable; // all types are marked as creatable by default.
560 AccessSemantics m_semantics = AccessSemantics::Reference;
561
562 QQmlJS::SourceLocation m_sourceLocation;
563 QQmlJS::SourceLocation m_idSourceLocation;
564
565 QString m_moduleName;
566
567 std::optional<QString> m_inlineComponentName;
568};
569
570inline QQmlJSScope::Ptr QQmlJSScope::parentScope()
571{
572 return m_parentScope.toStrongRef();
573}
574
575inline QQmlJSScope::ConstPtr QQmlJSScope::parentScope() const
576{
577 QT_WARNING_PUSH
578#if defined(Q_CC_GNU_ONLY) && Q_CC_GNU < 1400 && Q_CC_GNU >= 1200
579 QT_WARNING_DISABLE_GCC("-Wuse-after-free")
580#endif
581 return QQmlJSScope::WeakConstPtr(m_parentScope).toStrongRef();
582 QT_WARNING_POP
583}
584
585inline QMultiHash<QString, QQmlJSMetaPropertyBinding> QQmlJSScope::ownPropertyBindings() const
586{
587 return m_propertyBindings;
588}
589
590inline std::pair<QMultiHash<QString, QQmlJSMetaPropertyBinding>::const_iterator, QMultiHash<QString, QQmlJSMetaPropertyBinding>::const_iterator> QQmlJSScope::ownPropertyBindings(const QString &name) const
591{
592 return m_propertyBindings.equal_range(name);
593}
594
595inline bool QQmlJSScope::hasOwnPropertyBindings(const QString &name) const
596{
597 return m_propertyBindings.contains(name);
598}
599
600inline QQmlJSMetaMethod::AbsoluteFunctionIndex QQmlJSScope::ownRuntimeFunctionIndex(QQmlJSMetaMethod::RelativeFunctionIndex index) const
601{
602 const int i = static_cast<int>(index);
603 Q_ASSERT(i >= 0);
604 Q_ASSERT(i < int(m_runtimeFunctionIndices.size()));
605 return m_runtimeFunctionIndices[i];
606}
607
608inline void QQmlJSScope::setInlineComponentName(const QString &inlineComponentName)
609{
610 Q_ASSERT(isInlineComponent());
611 m_inlineComponentName = inlineComponentName;
612}
613
614inline QList<QQmlJSScope::Ptr> QQmlJSScope::childScopes()
615{
616 return m_childScopes;
617}
618
619inline void QQmlJSScope::setSourceLocation(const QQmlJS::SourceLocation &sourceLocation)
620{
621 m_sourceLocation = sourceLocation;
622}
623
624inline QQmlJS::SourceLocation QQmlJSScope::sourceLocation() const
625{
626 return m_sourceLocation;
627}
628
629inline void QQmlJSScope::setIdSourceLocation(const QQmlJS::SourceLocation &sourceLocation)
630{
631 Q_ASSERT(m_scopeType == QQmlSA::ScopeType::QMLScope);
632 m_idSourceLocation = sourceLocation;
633}
634
635inline QQmlJS::SourceLocation QQmlJSScope::idSourceLocation() const
636{
637 Q_ASSERT(m_scopeType == QQmlSA::ScopeType::QMLScope);
638 return m_idSourceLocation;
639}
640
641inline QQmlJSScope::ConstPtr QQmlJSScope::nonCompositeBaseType(const ConstPtr &type)
642{
643 for (QQmlJSScope::ConstPtr base = type; base; base = base->baseType()) {
644 if (!base->isComposite())
645 return base;
646 }
647 return {};
648}
649
651
652template<>
653class Q_QMLCOMPILER_EXPORT QDeferredFactory<QQmlJSScope>
654{
655public:
656 using TypeReader = std::function<QList<QQmlJS::DiagnosticMessage>(
657 QQmlJSImporter *importer, const QString &filePath,
658 const QSharedPointer<QQmlJSScope> &scopeToPopulate)>;
659 QDeferredFactory() = default;
660
661 QDeferredFactory(QQmlJSImporter *importer, const QString &filePath,
662 const TypeReader &typeReader = {});
663
664 bool isValid() const
665 {
666 return !m_filePath.isEmpty() && m_importer != nullptr;
667 }
668
669 QString internalName() const
670 {
671 return QFileInfo(m_filePath).baseName();
672 }
673
674 QString filePath() const { return m_filePath; }
675
676 QQmlJSImporter* importer() const { return m_importer; }
677
678 void setIsSingleton(bool isSingleton)
679 {
680 m_isSingleton = isSingleton;
681 }
682
683 void setModuleName(const QString &moduleName) { m_moduleName = moduleName; }
684
685private:
686 friend class QDeferredSharedPointer<QQmlJSScope>;
687 friend class QDeferredSharedPointer<const QQmlJSScope>;
688 friend class QDeferredWeakPointer<QQmlJSScope>;
689 friend class QDeferredWeakPointer<const QQmlJSScope>;
690
691 // Should only be called when lazy-loading the type in a deferred pointer.
692 void populate(const QSharedPointer<QQmlJSScope> &scope) const;
693
694 QString m_filePath;
695 QQmlJSImporter *m_importer = nullptr;
696 bool m_isSingleton = false;
697 QString m_moduleName;
698 TypeReader m_typeReader;
699};
700
701using QQmlJSExportedScope = QQmlJSScope::ExportedScope<QQmlJSScope::Ptr>;
702using QQmlJSImportedScope = QQmlJSScope::ImportedScope<QQmlJSScope::ConstPtr>;
703
704namespace QQmlSA {
705constexpr inline bool isFunctionScope(ScopeType type)
706{
707 switch (type) {
711 return true;
712 default:
713 return false;
714 }
715}
716
717}
718
719QT_END_NAMESPACE
720
721#endif // QQMLJSSCOPE_P_H
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:807
friend bool operator!=(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are different, otherwise returns false.
Definition qbytearray.h:818
QString fileName() const
If the currently assigned device is a QFile, or if setFileName() has been called, this function retur...
void setFileName(const QString &fileName)
Sets the file name of QImageReader to fileName.
virtual Type type() const =0
Reimplement this function to return the paint engine \l{Type}.
virtual bool isValid() const
virtual QString location() const
QString typeName() const
void setIsFlag(bool isFlag)
void setLineNumber(int lineNumber)
friend bool operator!=(const QQmlJSMetaEnum &a, const QQmlJSMetaEnum &b)
void setName(const QString &name)
QQmlJSMetaEnum()=default
int lineNumber() const
friend size_t qHash(const QQmlJSMetaEnum &e, size_t seed=0)
friend bool operator==(const QQmlJSMetaEnum &a, const QQmlJSMetaEnum &b)
void setTypeName(const QString &typeName)
bool hasKey(const QString &key) const
void setIsScoped(bool v)
void setType(const QSharedPointer< const QQmlJSScope > &type)
QSharedPointer< const QQmlJSScope > type() const
int value(const QString &key) const
bool isQml() const
void setIsQml(bool v)
void addValue(int value)
bool isScoped() const
QList< int > values() const
bool isFlag() const
QString alias() const
QStringList keys() const
bool isValid() const
void addKey(const QString &key)
void setAlias(const QString &alias)
QString name() const
QQmlJSMetaEnum(QString name)
bool hasValues() const
QQmlJSMetaReturnType returnValue() const
QQmlJSMetaMethod()=default
void setReturnType(QWeakPointer< const QQmlJSScope > type)
void setSourceLocation(QQmlJS::SourceLocation location)
QQmlJS::SourceLocation sourceLocation() const
QString methodName() const
QQmlJSMetaMethodType MethodType
void setMethodName(const QString &name)
void setReturnTypeName(const QString &typeName)
QString returnTypeName() const
QQmlJSMetaMethod(QString name, QString returnType=QString())
QList< QQmlJSMetaParameter > parameters() const
void setReturnValue(const QQmlJSMetaReturnType returnValue)
QSharedPointer< const QQmlJSScope > returnType() const
void setIsPointer(bool isPointer)
void setType(QWeakPointer< const QQmlJSScope > type)
void setIsList(bool isList)
friend bool operator!=(const QQmlJSMetaParameter &a, const QQmlJSMetaParameter &b)
friend size_t qHash(const QQmlJSMetaParameter &e, size_t seed=0)
void setName(const QString &name)
void setTypeName(const QString &typeName)
void setTypeQualifier(Constness typeQualifier)
Constness typeQualifier() const
QString typeName() const
QQmlJSMetaParameter(QString name=QString(), QString typeName=QString(), Constness typeQualifier=NonConst, QWeakPointer< const QQmlJSScope > type={})
friend bool operator==(const QQmlJSMetaParameter &a, const QQmlJSMetaParameter &b)
QSharedPointer< const QQmlJSScope > type() const
\inmodule QtQmlCompiler
friend bool operator!=(const QQmlJSMetaProperty &a, const QQmlJSMetaProperty &b)
QString notify() const
QSharedPointer< const QQmlJSScope > aliasTargetScope() const
QString aliasExpression() const
friend bool operator==(const QQmlJSMetaProperty &a, const QQmlJSMetaProperty &b)
void setIsOverride(bool isOverride)
void setPropertyName(const QString &propertyName)
QString aliasTargetName() const
void setAnnotations(const QList< QQmlJSAnnotation > &annotation)
void setIsList(bool isList)
QString bindable() const
QSharedPointer< const QQmlJSScope > type() const
void setPrivateClass(const QString &privateClass)
void setRead(const QString &read)
friend size_t qHash(const QQmlJSMetaProperty &prop, size_t seed=0)
void setBindable(const QString &bindable)
void setWrite(const QString &write)
void setIsPropertyConstant(bool isPropertyConstant)
QString reset() const
void setRevision(int revision)
void setIsFinal(bool isFinal)
QString typeName() const
void setAliasTargetScope(const QSharedPointer< const QQmlJSScope > &scope)
void setSourceLocation(const QQmlJS::SourceLocation &newSourceLocation)
QString write() const
void setIsTypeConstant(bool isTypeConstant)
const QList< QQmlJSAnnotation > & annotations() const
QQmlJS::SourceLocation sourceLocation() const
void setTypeName(const QString &typeName)
QQmlJSMetaProperty()=default
void setReset(const QString &reset)
QString privateClass() const
void setIsVirtual(bool isVirtual)
void setIsWritable(bool isWritable)
void setAliasExpression(const QString &aliasString)
void setAliasTargetName(const QString &name)
void setIndex(int index)
void setNotify(const QString &notify)
bool isPropertyConstant() const
bool isTypeConstant() const
void setType(const QSharedPointer< const QQmlJSScope > &type)
QString propertyName() const
void setIsPointer(bool isPointer)
QQmlJSRegisterContent createMethod(const QList< QQmlJSMetaMethod > &methods, const QQmlJSScope::ConstPtr &methodType, ContentVariant variant, QQmlJSRegisterContent scope)
QQmlJSRegisterContent createImportNamespace(uint importNamespaceStringId, const QQmlJSScope::ConstPtr &importNamespaceType, ContentVariant variant, QQmlJSRegisterContent scope)
QQmlJSRegisterContent createProperty(const QQmlJSMetaProperty &property, int baseLookupIndex, int resultLookupIndex, ContentVariant variant, QQmlJSRegisterContent scope)
void adjustType(QQmlJSRegisterContent content, const QQmlJSScope::ConstPtr &adjusted)
void generalizeType(QQmlJSRegisterContent content, const QQmlJSScope::ConstPtr &generalized)
void storeType(QQmlJSRegisterContent content, const QQmlJSScope::ConstPtr &stored)
QQmlJSRegisterContent castTo(QQmlJSRegisterContent content, const QQmlJSScope::ConstPtr &newContainedType)
QQmlJSRegisterContent createEnumeration(const QQmlJSMetaEnum &enumeration, const QString &enumMember, ContentVariant variant, QQmlJSRegisterContent scope)
void setAllocationMode(AllocationMode mode)
QQmlJSRegisterContent storedIn(QQmlJSRegisterContent content, const QQmlJSScope::ConstPtr &newStoredType)
QQmlJSRegisterContent clone(QQmlJSRegisterContent from)
QQmlJSRegisterContent createType(const QQmlJSScope::ConstPtr &type, int resultLookupIndex, ContentVariant variant, QQmlJSRegisterContent scope={})
QQmlJSRegisterContent createConversion(const QList< QQmlJSRegisterContent > &origins, const QQmlJSScope::ConstPtr &conversion, QQmlJSRegisterContent conversionScope, ContentVariant variant, QQmlJSRegisterContent scope)
QQmlJSRegisterContent createMethodCall(const QQmlJSMetaMethod &method, const QQmlJSScope::ConstPtr &returnType, QQmlJSRegisterContent scope)
Tracks the types for the QmlCompiler.
QTypeRevision revision() const
QString type() const
QTypeRevision version() const
bool isValid() const
QString package() const
Export()=default
Export(QString package, QString type, QTypeRevision version, QTypeRevision revision)
virtual QQmlSourceLocation sourceLocation() const
\inmodule QtQmlCompiler
Definition qqmlsa.h:52
Element attachedType() const
Returns the attached type if the content type of this binding is AttachedProperty,...
Definition qqmlsa.cpp:384
BindingType bindingType() const
Returns the type of this binding.
Definition qqmlsa.cpp:350
Element bindingScope() const
Returns the Element scope in which the binding is defined.
Definition qqmlsa.cpp:342
friend bool operator!=(const Binding &lhs, const Binding &rhs)
Returns true if lhs and rhs are not equal, and false otherwise.
Definition qqmlsa.h:106
bool hasUndefinedScriptValue() const
Returns whether this binding has script value type undefined like when it is assigned undefined.
Definition qqmlsa.cpp:451
Binding(const Binding &)
Creates a copy of other.
Definition qqmlsa.cpp:254
Binding & operator=(const Binding &)
Assigns other to this Binding instance.
Definition qqmlsa.cpp:266
bool isAttached() const
Returns true if this type is attached to another one, false otherwise.
Definition qqmlsa.cpp:375
bool hasObject() const
Returns true if this binding has an objects, otherwise returns false.
Definition qqmlsa.cpp:432
friend bool operator==(const Binding &lhs, const Binding &rhs)
Returns true if lhs and rhs are equal, and false otherwise.
Definition qqmlsa.h:102
static bool isLiteralBinding(BindingType)
Returns true if bindingType is a literal type, and false otherwise.
Definition qqmlsa.cpp:475
QString propertyName() const
Returns the name of the property bound with this binding.
Definition qqmlsa.cpp:367
Binding()
Constructs a new Binding object.
Definition qqmlsa.cpp:247
QString stringValue() const
Returns the associated string literal if the content type of this binding is StringLiteral,...
Definition qqmlsa.cpp:359
Element objectType() const
Returns the type of the associated object if the content type of this binding is Object,...
Definition qqmlsa.cpp:441
Binding & operator=(Binding &&) noexcept
Move-assigns other to this Binding instance.
Definition qqmlsa.cpp:282
QQmlSA::SourceLocation sourceLocation() const
Returns the location in the QML code where this binding is defined.
Definition qqmlsa.cpp:405
ScriptBindingKind scriptKind() const
Returns the kind of the associated script if the content type of this binding is Script,...
Definition qqmlsa.cpp:424
double numberValue() const
Returns the associated number if the content type of this binding is NumberLiteral,...
Definition qqmlsa.cpp:415
Element groupType() const
Returns the type of the property of this binding if it is a group property, otherwise returns an inva...
Definition qqmlsa.cpp:334
bool hasFunctionScriptValue() const
Returns whether this binding has script value type function like when it is assigned a (lambda) metho...
Definition qqmlsa.cpp:464
\inmodule QtQmlCompiler
Definition qqmlsa.h:368
\inmodule QtQmlCompiler
Definition qqmlsa.h:202
\inmodule QtQmlCompiler
Definition qqmlsa.h:377
FixSuggestion(const FixSuggestion &)
Creates a copy of other.
Definition qqmlsa.cpp:2039
QString description() const
Returns the description of the fix.
Definition qqmlsa.cpp:2084
QString replacement() const
Returns the fix that will replace the problematic source code.
Definition qqmlsa.cpp:2102
bool isAutoApplicable() const
Returns whether this suggested fix can be applied automatically.
Definition qqmlsa.cpp:2137
void setAutoApplicable(bool autoApplicable=true)
Sets autoApplicable to determine whether this suggested fix can be applied automatically.
Definition qqmlsa.cpp:2128
\inmodule QtQmlCompiler
Definition qqmlsa.h:283
void emitWarning(QAnyStringView diagnostic, LoggerWarningId id)
Emits a warning message diagnostic about an issue of type id.
Definition qqmlsa.cpp:1269
Element resolveLiteralType(const Binding &binding)
Returns the element representing the type of literal in binding.
Definition qqmlsa.cpp:1381
void emitWarning(QAnyStringView diagnostic, LoggerWarningId id, QQmlSA::SourceLocation srcLocation, const QQmlSA::FixSuggestion &fix)
Emits a warning message diagnostic about an issue of type id located at srcLocation and with suggeste...
Definition qqmlsa.cpp:1292
void emitWarning(QAnyStringView diagnostic, LoggerWarningId id, QQmlSA::SourceLocation srcLocation)
Emits warning message diagnostic about an issue of type id located at srcLocation.
Definition qqmlsa.cpp:1278
Element resolveBuiltinType(QAnyStringView typeName) const
Returns the type of the built-in type identified by typeName.
Definition qqmlsa.cpp:1353
Element resolveAttached(QAnyStringView moduleName, QAnyStringView typeName)
Returns the attached type of typeName defined in module moduleName.
Definition qqmlsa.cpp:1371
QString resolveElementToId(const Element &element, const Element &context)
Returns the id of element in a given context.
Definition qqmlsa.cpp:1404
Element resolveTypeInFileScope(QAnyStringView typeName)
Returns the type corresponding to typeName inside the currently analysed file.
Definition qqmlsa.cpp:1307
QString sourceCode(QQmlSA::SourceLocation location)
Returns the source code located within location.
Definition qqmlsa.cpp:1416
Element resolveAttachedInFileScope(QAnyStringView typeName)
Returns the attached type corresponding to typeName used inside the currently analysed file.
Definition qqmlsa.cpp:1319
Element resolveIdToElement(QAnyStringView id, const Element &context)
Returns the element in context that has id id.
Definition qqmlsa.cpp:1392
Element resolveType(QAnyStringView moduleName, QAnyStringView typeName)
Returns the type of typeName defined in module moduleName.
Definition qqmlsa.cpp:1337
\inmodule QtQmlCompiler
Definition qqmlsa.h:340
\inmodule QtQmlCompiler
Definition qqmlsa.h:120
Method & operator=(Method &&) noexcept
Move-assigns other to this Method instance.
Definition qqmlsa.cpp:624
MethodType methodType() const
Returns the type of this method.
Definition qqmlsa.cpp:651
Method(const Method &)
Creates a copy of other.
Definition qqmlsa.cpp:598
Method()
Constructs a new Method object.
Definition qqmlsa.cpp:593
Method & operator=(const Method &)
Assigns other to this Method instance.
Definition qqmlsa.cpp:611
QString methodName() const
Returns the name of the this method.
Definition qqmlsa.cpp:642
\inmodule QtQmlCompiler
Definition qqmlsa.h:314
void analyze(const Element &root)
Runs the element passes over root and all its children.
Definition qqmlsa.cpp:1605
bool isCategoryEnabled(LoggerWarningId category) const
Returns true if warnings of category are enabled, false otherwise.
Definition qqmlsa.cpp:1716
bool registerPropertyPass(std::shared_ptr< PropertyPass > pass, QAnyStringView moduleName, QAnyStringView typeName, QAnyStringView propertyName=QAnyStringView(), bool allowInheritance=true)
Registers a static analysis pass for properties.
Definition qqmlsa.cpp:1508
std::unordered_map< quint32, Binding > bindingsByLocation() const
Returns bindings by their source location.
Definition qqmlsa.cpp:1946
bool hasImportedModule(QAnyStringView name) const
Returns true if the module named module has been imported by the QML to be analyzed,...
Definition qqmlsa.cpp:1708
\inmodule QtQmlCompiler
Definition qqmlsa.h:351
\inmodule QtQmlCompiler
Definition qqmlsa_p.h:156
\inmodule QtQmlCompiler
Definition qqmlsa.h:169
Property(const Property &)
Creates a copy of other.
Definition qqmlsa.cpp:779
QString typeName() const
Returns the name of the type of this property.
Definition qqmlsa.cpp:824
bool isReadonly() const
Returns true if this property is read-only, false otherwise.
Definition qqmlsa.cpp:842
\inmodule QtQmlCompiler
MethodType
Definition qqmlsa.h:48
constexpr bool isFunctionScope(ScopeType type)
AccessSemantics
Definition qqmlsa.h:49
Combined button and popup list for selecting options.
Q_DECLARE_INTERFACE(QNetworkAccessBackendFactory, QNetworkAccessBackendFactory_iid)
Q_DECLARE_TYPEINFO(QObjectPrivate::ConnectionList, Q_RELOCATABLE_TYPE)
static QString toNumericString(double value)
static QString messageTypeForMethod(const QString &method)
static QString derefContentPointer(const QString &contentPointer)
static bool canTypeBeAffectedBySideEffects(const QQmlJSTypeResolver *typeResolver, const QQmlJSRegisterContent &baseType)
#define BYTECODE_UNIMPLEMENTED()
#define INJECT_TRACE_INFO(function)
#define REJECT
static QString registerName(int registerIndex, int offset)
static QString minExpression(int argc)
static QString maxExpression(int argc)
static bool isTypeStorable(const QQmlJSTypeResolver *resolver, const QQmlJSScope::ConstPtr &type)
QQmlSA::MethodType QQmlJSMetaMethodType
QQmlJSMetaParameter QQmlJSMetaReturnType
ScriptBindingValueType
@ ScriptValue_Function
@ ScriptValue_Unknown
@ ScriptValue_Undefined
#define QmlLintPluginInterface_iid
Definition qqmlsa.h:417
QList< Export > exports
QTypeRevision revision