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
qqmldomitem_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QMLDOMITEM_H
5#define QMLDOMITEM_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qqmldom_global.h"
19#include "qqmldom_fwd_p.h"
23#include "qqmldompath_p.h"
29
30#include <QtCore/QMap>
31#include <QtCore/QMultiMap>
32#include <QtCore/QSet>
33#include <QtCore/QString>
34#include <QtCore/QStringView>
35#include <QtCore/QDebug>
36#include <QtCore/QDateTime>
37#include <QtCore/QMutex>
38#include <QtCore/QCborValue>
39#include <QtCore/QTimeZone>
40#include <QtQml/private/qqmljssourcelocation_p.h>
41#include <QtQmlCompiler/private/qqmljsscope_p.h>
42
43#include <memory>
44#include <typeinfo>
45#include <utility>
46#include <type_traits>
47#include <variant>
48#include <optional>
49#include <cstddef>
50
51QT_BEGIN_NAMESPACE
52
53struct QQmlJSResourceFileMapper;
54
55QT_ENABLE_P0846_SEMANTICS_FOR(get_if)
56
57QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(writeOutLog, QMLDOM_EXPORT);
58
59namespace QQmlJS {
60// we didn't have enough 'O's to properly name everything...
61namespace Dom {
62
63class Path;
64
65constexpr bool domTypeIsObjWrap(DomType k);
66constexpr bool domTypeIsValueWrap(DomType k);
67constexpr bool domTypeIsDomElement(DomType);
68constexpr bool domTypeIsOwningItem(DomType);
70constexpr bool domTypeIsScriptElement(DomType);
74constexpr bool domTypeCanBeInline(DomType k)
75{
76 switch (k) {
77 case DomType::Empty:
78 case DomType::Map:
79 case DomType::List:
80 case DomType::ListP:
85 return true;
86 default:
87 return false;
88 }
89}
91
96
97inline bool noFilter(const DomItem &, const PathEls::PathComponent &, const DomItem &)
98{
99 return true;
100}
101
103// using DirectVisitor = function_ref<bool(Path, const DomItem &)>;
104
105namespace {
106template<typename T>
107struct IsMultiMap : std::false_type
108{
109};
110
111template<typename Key, typename T>
112struct IsMultiMap<QMultiMap<Key, T>> : std::true_type
113{
114};
115
116template<typename T>
117struct IsMap : std::false_type
118{
119};
120
121template<typename Key, typename T>
122struct IsMap<QMap<Key, T>> : std::true_type
123{
124};
125
126template<typename... Ts>
127using void_t = void;
128
129template<typename T, typename = void>
130struct IsDomObject : std::false_type
131{
132};
133
134template<typename T>
135struct IsDomObject<T, void_t<decltype(T::kindValue)>> : std::true_type
136{
137};
138
139template<typename T, typename = void>
140struct IsInlineDom : std::false_type
141{
142};
143
144template<typename T>
145struct IsInlineDom<T, void_t<decltype(T::kindValue)>>
146 : std::integral_constant<bool, domTypeCanBeInline(T::kindValue)>
147{
148};
149
150template<typename T>
151struct IsInlineDom<T *, void_t<decltype(T::kindValue)>> : std::true_type
152{
153};
154
155template<typename T>
156struct IsInlineDom<std::shared_ptr<T>, void_t<decltype(T::kindValue)>> : std::true_type
157{
158};
159
160template<typename T>
161struct IsSharedPointerToDomObject : std::false_type
162{
163};
164
165template<typename T>
166struct IsSharedPointerToDomObject<std::shared_ptr<T>> : IsDomObject<T>
167{
168};
169
170template<typename T, typename = void>
171struct IsList : std::false_type
172{
173};
174
175template<typename T>
176struct IsList<T, void_t<typename T::value_type>> : std::true_type
177{
178};
179
180}
181
182template<typename T>
184 int i;
185 T lp;
186
187 // TODO: these are extremely nasty. What is this int doing in here?
188 T *data() { return reinterpret_cast<T *>(this); }
189 const T *data() const { return reinterpret_cast<const T *>(this); }
190
192 SubclassStorage(T &&el) { el.moveTo(data()); }
193 SubclassStorage(const T *el) { el->copyTo(data()); }
197 {
198 data()->~T();
199 o.data()->copyTo(data());
200 return *this;
201 }
202 ~SubclassStorage() { data()->~T(); }
203};
204
206{
207public:
208 using FilterT = function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)>;
209
210 virtual ~DomBase() = default;
211
212 DomBase *domBase() { return this; }
213 const DomBase *domBase() const { return this; }
214
215 // minimal overload set:
216 virtual DomType kind() const = 0;
217 virtual DomKind domKind() const;
218 virtual Path pathFromOwner() const = 0;
219 virtual Path canonicalPath(const DomItem &self) const = 0;
220 virtual bool
222 DirectVisitor visitor) const = 0; // iterates the *direct* subpaths, returns
223 // false if a quick end was requested
224
226 const DomItem &self) const; // the DomItem corresponding to the canonicalSource source
227 virtual void dump(const DomItem &, const Sink &sink, int indent, FilterT filter) const;
228 virtual quintptr id() const;
229 QString typeName() const;
230
231 virtual QList<QString> fields(const DomItem &self) const;
232 virtual DomItem field(const DomItem &self, FieldName name) const;
233
234 virtual index_type indexes(const DomItem &self) const;
235 virtual DomItem index(const DomItem &self, index_type index) const;
236
237 virtual QSet<QString> const keys(const DomItem &self) const;
238 virtual DomItem key(const DomItem &self, const QString &name) const;
239
240 virtual QString canonicalFilePath(const DomItem &self) const;
241
242 virtual void writeOut(const DomItem &self, OutWriter &lw) const;
243
244 virtual QCborValue value() const {
245 return QCborValue();
246 }
247};
248
250{
251 switch (k) {
252 case DomType::Empty:
253 return DomKind::Empty;
254 case DomType::List:
255 case DomType::ListP:
256 return DomKind::List;
257 case DomType::Map:
258 return DomKind::Map;
259 case DomType::ConstantData:
260 return DomKind::Value;
261 default:
262 return DomKind::Object;
263 }
264}
265
266class QMLDOM_EXPORT Empty final : public DomBase
267{
268public:
269 constexpr static DomType kindValue = DomType::Empty;
270 DomType kind() const override { return kindValue; }
271
272 Empty *operator->() { return this; }
273 const Empty *operator->() const { return this; }
274 Empty &operator*() { return *this; }
275 const Empty &operator*() const { return *this; }
276
277 Empty();
278 quintptr id() const override { return ~quintptr(0); }
279 Path pathFromOwner() const override;
280 Path canonicalPath(const DomItem &self) const override;
281 DomItem containingObject(const DomItem &self) const override;
282 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override;
283 void dump(const DomItem &, const Sink &s, int indent,
284 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter)
285 const override;
286};
287
289protected:
290 DomElement& operator=(const DomElement&) = default;
291public:
292 DomElement(const Path &pathFromOwner = Path());
293 DomElement(const DomElement &o) = default;
294 Path pathFromOwner() const override { return m_pathFromOwner; }
295 Path canonicalPath(const DomItem &self) const override;
296 DomItem containingObject(const DomItem &self) const override;
297 virtual void updatePathFromOwner(const Path &newPath);
298
299private:
300 Path m_pathFromOwner;
301};
302
303class QMLDOM_EXPORT Map final : public DomElement
304{
305public:
306 constexpr static DomType kindValue = DomType::Map;
307 DomType kind() const override { return kindValue; }
308
309 Map *operator->() { return this; }
310 const Map *operator->() const { return this; }
311 Map &operator*() { return *this; }
312 const Map &operator*() const { return *this; }
313
314 using LookupFunction = std::function<DomItem(const DomItem &, QString)>;
315 using Keys = std::function<QSet<QString>(const DomItem &)>;
316 Map(const Path &pathFromOwner, const LookupFunction &lookup,
317 const Keys &keys, const QString &targetType);
318 quintptr id() const override;
319 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override;
320 QSet<QString> const keys(const DomItem &self) const override;
321 DomItem key(const DomItem &self, const QString &name) const override;
322
323 template<typename T>
324 static Map fromMultiMapRef(const Path &pathFromOwner, const QMultiMap<QString, T> &mmap);
325 template<typename T>
326 static Map fromMultiMap(const Path &pathFromOwner, const QMultiMap<QString, T> &mmap);
327 template<typename T>
328 static Map
330 const Path &pathFromOwner, const QMap<QString, T> &mmap,
331 const std::function<DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper);
332
333 template<typename T>
334 static Map fromFileRegionMap(
335 const Path &pathFromOwner, const QMap<FileLocationRegion, T> &map);
336
337private:
338 template<typename MapT>
339 static QSet<QString> fileRegionKeysFromMap(const MapT &map);
340 LookupFunction m_lookup;
341 Keys m_keys;
342 QString m_targetType;
343};
344
345class QMLDOM_EXPORT List final : public DomElement
346{
347public:
348 constexpr static DomType kindValue = DomType::List;
349 DomType kind() const override { return kindValue; }
350
351 List *operator->() { return this; }
352 const List *operator->() const { return this; }
353 List &operator*() { return *this; }
354 const List &operator*() const { return *this; }
355
356 using LookupFunction = std::function<DomItem(const DomItem &, index_type)>;
359 std::function<bool(const DomItem &, function_ref<bool(index_type, function_ref<DomItem()>)>)>;
360
361 List(const Path &pathFromOwner, const LookupFunction &lookup, const Length &length,
362 const IteratorFunction &iterator, const QString &elType);
363 quintptr id() const override;
364 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override;
365 void
366 dump(const DomItem &, const Sink &s, int indent,
367 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)>) const override;
368 index_type indexes(const DomItem &self) const override;
369 DomItem index(const DomItem &self, index_type index) const override;
370
371 template<typename T>
372 static List
373 fromQList(const Path &pathFromOwner, const QList<T> &list,
374 const std::function<DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper,
376 template<typename T>
377 static List
378 fromQListRef(const Path &pathFromOwner, const QList<T> &list,
379 const std::function<DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper,
381 void writeOut(const DomItem &self, OutWriter &ow, bool compact) const;
382 void writeOut(const DomItem &self, OutWriter &ow) const override { writeOut(self, ow, true); }
383
384private:
385 LookupFunction m_lookup;
386 Length m_length;
387 IteratorFunction m_iterator;
388 QString m_elType;
389};
390
392{
393public:
394 constexpr static DomType kindValue = DomType::ListP;
395 DomType kind() const override { return kindValue; }
396
397 ListPBase(const Path &pathFromOwner, const QList<const void *> &pList, const QString &elType)
398 : DomElement(pathFromOwner), m_pList(pList), m_elType(elType)
399 {
400 }
401 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor v) const override;
402 virtual void copyTo(ListPBase *) const { Q_ASSERT(false); };
403 virtual void moveTo(ListPBase *) const { Q_ASSERT(false); };
404 quintptr id() const override { return quintptr(0); }
405 index_type indexes(const DomItem &) const override { return index_type(m_pList.size()); }
406 void writeOut(const DomItem &self, OutWriter &ow, bool compact) const;
407 void writeOut(const DomItem &self, OutWriter &ow) const override { writeOut(self, ow, true); }
408
409protected:
410 QList<const void *> m_pList;
412};
413
414template<typename T>
415class ListPT final : public ListPBase
416{
417public:
418 constexpr static DomType kindValue = DomType::ListP;
419
420 ListPT(const Path &pathFromOwner, const QList<T *> &pList, const QString &elType = QString(),
422 : ListPBase(pathFromOwner, {},
423 (elType.isEmpty() ? QLatin1String(typeid(T).name()) : elType))
424 {
425 static_assert(sizeof(ListPBase) == sizeof(ListPT),
426 "ListPT does not have the same size as ListPBase");
427 static_assert(alignof(ListPBase) == alignof(ListPT),
428 "ListPT does not have the same size as ListPBase");
429 m_pList.reserve(pList.size());
430 if (options == ListOptions::Normal) {
431 for (const void *p : pList)
432 m_pList.append(p);
433 } else if (options == ListOptions::Reverse) {
434 for (qsizetype i = pList.size(); i-- != 0;)
435 // probably writing in reverse and reading sequentially would be better
436 m_pList.append(pList.at(i));
437 } else {
438 Q_ASSERT(false);
439 }
440 }
441 void copyTo(ListPBase *t) const override { new (t) ListPT(*this); }
442 void moveTo(ListPBase *t) const override { new (t) ListPT(std::move(*this)); }
443 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor v) const override;
444
445 DomItem index(const DomItem &self, index_type index) const override;
446};
447
449{
450public:
451 constexpr static DomType kindValue = DomType::ListP;
452 template<typename T>
453 ListP(const Path &pathFromOwner, const QList<T *> &pList, const QString &elType = QString(),
456 {
457 }
458 ListP() = delete;
459
460 ListPBase *operator->() { return list.data(); }
461 const ListPBase *operator->() const { return list.data(); }
462 ListPBase &operator*() { return *list.data(); }
463 const ListPBase &operator*() const { return *list.data(); }
464
465private:
467};
468
469class QMLDOM_EXPORT ConstantData final : public DomElement
470{
471public:
473 DomType kind() const override { return kindValue; }
474
475 ConstantData *operator->() { return this; }
476 const ConstantData *operator->() const { return this; }
477 ConstantData &operator*() { return *this; }
478 const ConstantData &operator*() const { return *this; }
479
480 ConstantData(const Path &pathFromOwner, const QCborValue &value);
481 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override;
482 quintptr id() const override;
483 DomKind domKind() const override;
484 QCborValue value() const override { return m_value; }
485private:
486 QCborValue m_value;
487};
488
490{
491public:
493 DomType kind() const final override { return m_kind; }
494
495 quintptr id() const final override { return m_id; }
496 DomKind domKind() const final override { return m_domKind; }
497
498 template <typename T>
499 T const *as() const
500 {
501 if (m_options & SimpleWrapOption::ValueType) {
502 if (m_value.metaType() == QMetaType::fromType<T>())
503 return static_cast<const T *>(m_value.constData());
504 return nullptr;
505 } else {
506 return m_value.value<const T *>();
507 }
508 }
509
511 virtual void copyTo(SimpleObjectWrapBase *) const { Q_ASSERT(false); }
512 virtual void moveTo(SimpleObjectWrapBase *) const { Q_ASSERT(false); }
513 bool iterateDirectSubpaths(const DomItem &, DirectVisitor) const override
514 {
515 Q_ASSERT(false);
516 return true;
517 }
518
519protected:
520 friend class TestDomItem;
521 SimpleObjectWrapBase(const Path &pathFromOwner, const QVariant &value, quintptr idValue,
522 DomType kind = kindValue,
523 SimpleWrapOptions options = SimpleWrapOption::None)
524 : DomElement(pathFromOwner),
525 m_kind(kind),
527 m_value(value),
528 m_id(idValue),
530 {
531 }
532
538};
539
540template<typename T>
541class SimpleObjectWrapT final : public SimpleObjectWrapBase
542{
543public:
545
546 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const override
547 {
548 return asT()->iterateDirectSubpaths(self, visitor);
549 }
550
551 void writeOut(const DomItem &self, OutWriter &lw) const override;
552
553 const T *asT() const
554 {
555 if constexpr (domTypeIsValueWrap(T::kindValue)) {
556 if (m_value.metaType() == QMetaType::fromType<T>())
557 return static_cast<const T *>(m_value.constData());
558 return nullptr;
559 } else if constexpr (domTypeIsObjWrap(T::kindValue)) {
560 return m_value.value<const T *>();
561 } else {
562 // need dependent static assert to not unconditially trigger
563 static_assert(!std::is_same_v<T, T>, "wrapping of unexpected type");
564 return nullptr; // necessary to avoid warnings on INTEGRITY
565 }
566 }
567
568 void copyTo(SimpleObjectWrapBase *target) const override
569 {
570 static_assert(sizeof(SimpleObjectWrapBase) == sizeof(SimpleObjectWrapT),
571 "Size mismatch in SimpleObjectWrapT");
572 static_assert(alignof(SimpleObjectWrapBase) == alignof(SimpleObjectWrapT),
573 "Size mismatch in SimpleObjectWrapT");
574 new (target) SimpleObjectWrapT(*this);
575 }
576
577 void moveTo(SimpleObjectWrapBase *target) const override
578 {
579 static_assert(sizeof(SimpleObjectWrapBase) == sizeof(SimpleObjectWrapT),
580 "Size mismatch in SimpleObjectWrapT");
581 static_assert(alignof(SimpleObjectWrapBase) == alignof(SimpleObjectWrapT),
582 "Size mismatch in SimpleObjectWrapT");
583 new (target) SimpleObjectWrapT(std::move(*this));
584 }
585
586 SimpleObjectWrapT(const Path &pathFromOwner, const QVariant &v,
587 quintptr idValue, SimpleWrapOptions o)
588 : SimpleObjectWrapBase(pathFromOwner, v, idValue, T::kindValue, o)
589 {
590 Q_ASSERT(domTypeIsValueWrap(T::kindValue) == bool(o & SimpleWrapOption::ValueType));
591 }
592};
593
595{
596public:
598
599 SimpleObjectWrapBase *operator->() { return wrap.data(); }
600 const SimpleObjectWrapBase *operator->() const { return wrap.data(); }
601 SimpleObjectWrapBase &operator*() { return *wrap.data(); }
602 const SimpleObjectWrapBase &operator*() const { return *wrap.data(); }
603
604 template<typename T>
605 static SimpleObjectWrap fromObjectRef(const Path &pathFromOwner, T &value)
606 {
607 return SimpleObjectWrap(pathFromOwner, value);
608 }
610
611private:
612 template<typename T>
613 SimpleObjectWrap(const Path &pathFromOwner, T &value)
614 {
615 using BaseT = std::decay_t<T>;
616 if constexpr (domTypeIsObjWrap(BaseT::kindValue)) {
617 new (wrap.data()) SimpleObjectWrapT<BaseT>(pathFromOwner, QVariant::fromValue(&value),
618 quintptr(&value), SimpleWrapOption::None);
619 } else if constexpr (domTypeIsValueWrap(BaseT::kindValue)) {
620 new (wrap.data()) SimpleObjectWrapT<BaseT>(pathFromOwner, QVariant::fromValue(value),
621 quintptr(0), SimpleWrapOption::ValueType);
622 } else {
623 qCWarning(domLog) << "Unexpected object to wrap in SimpleObjectWrap: "
624 << domTypeToString(BaseT::kindValue);
625 Q_ASSERT_X(false, "SimpleObjectWrap",
626 "simple wrap of unexpected object"); // allow? (mocks for testing,...)
627 new (wrap.data())
628 SimpleObjectWrapT<BaseT>(pathFromOwner, nullptr, 0, SimpleWrapOption::None);
629 }
630 }
632};
633
635{
637public:
638 constexpr static DomType kindValue = DomType::Reference;
639 DomType kind() const override { return kindValue; }
640
641 Reference *operator->() { return this; }
642 const Reference *operator->() const { return this; }
643 Reference &operator*() { return *this; }
644 const Reference &operator*() const { return *this; }
645
646 bool shouldCache() const;
647 Reference(const Path &referredObject = Path(), const Path &pathFromOwner = Path(),
648 const SourceLocation &loc = SourceLocation());
649 quintptr id() const override;
650 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override;
651 DomItem field(const DomItem &self, FieldName name) const override;
652 QList<QString> fields(const DomItem &self) const override;
653 index_type indexes(const DomItem &) const override { return 0; }
654 DomItem index(const DomItem &, index_type) const override;
655 QSet<QString> const keys(const DomItem &) const override { return {}; }
656 DomItem key(const DomItem &, const QString &) const override;
657
658 DomItem get(const DomItem &self, const ErrorHandler &h = nullptr,
659 QList<Path> *visitedRefs = nullptr) const;
660 QList<DomItem> getAll(const DomItem &self, const ErrorHandler &h = nullptr,
661 QList<Path> *visitedRefs = nullptr) const;
662
664};
665
666namespace FileLocations {
667struct Info;
668}
669
670/*!
671 \internal
672 \brief A common base class for all the script elements.
673
674 This marker class allows to use all the script elements as a ScriptElement*, using virtual
675 dispatch. For now, it does not add any extra functionality, compared to a DomElement, but allows
676 to forbid DomElement* at the places where only script elements are required.
677 */
678// TODO: do we need another marker struct like this one to differentiate expressions from
679// statements? This would allow to avoid mismatchs between script expressions and script statements,
680// using type-safety.
682{
683 template<typename T>
684 using PointerType = std::shared_ptr<T>;
685
686 using DomElement::DomElement;
687 virtual void
688 createFileLocations(const std::shared_ptr<FileLocations::Node> &fileLocationOfOwner) = 0;
689
691 void setSemanticScope(const QQmlJSScope::ConstPtr &scope);
692
693private:
694 QQmlJSScope::ConstPtr m_scope;
695};
696
697/*!
698 \internal
699 \brief Use this to contain any script element.
700 */
702{
703private:
704 template<typename... T>
706
707 template<typename T, typename Variant>
708 struct TypeIsInVariant;
709
710 template<typename T, typename... Ts>
711 struct TypeIsInVariant<T, std::variant<Ts...>> : public std::disjunction<std::is_same<T, Ts>...>
712 {
713 };
714
715public:
722
723 template<typename T>
724 static ScriptElementVariant fromElement(const T &element)
725 {
726 static_assert(TypeIsInVariant<T, ScriptElementT>::value,
727 "Cannot construct ScriptElementVariant from T, as it is missing from the "
728 "ScriptElementT.");
730 p.m_data = element;
731 return p;
732 }
733
735
736 operator bool() const { return m_data.has_value(); }
737
738 template<typename F>
739 void visitConst(F &&visitor) const
740 {
741 if (m_data)
742 std::visit(std::forward<F>(visitor), *m_data);
743 }
744
745 template<typename F>
746 void visit(F &&visitor)
747 {
748 if (m_data)
749 std::visit(std::forward<F>(visitor), *m_data);
750 }
751 std::optional<ScriptElementT> data() { return m_data; }
752 void setData(const ScriptElementT &data) { m_data = data; }
753
754private:
755 std::optional<ScriptElementT> m_data;
756};
757
758/*!
759 \internal
760
761 To avoid cluttering the already unwieldy \l ElementT type below with all the types that the
762 different script elements can have, wrap them in an extra class. It will behave like an internal
763 Dom structure (e.g. like a List or a Map) and contain a pointer the the script element.
764 */
766{
767public:
769
771
772 DomBase *operator->() { return m_element.base().get(); }
773 const DomBase *operator->() const { return m_element.base().get(); }
774 DomBase &operator*() { return *m_element.base(); }
775 const DomBase &operator*() const { return *m_element.base(); }
776
777 ScriptElementVariant element() const { return m_element; }
778
779private:
780 ScriptElementVariant m_element;
781};
782
783// TODO: create more "groups" to simplify this variant? Maybe into Internal, ScriptExpression, ???
784using ElementT =
785 std::variant<ConstantData, Empty, List, ListP, Map, Reference, ScriptElementDomWrapper,
787 const DomEnvironment *, const DomUniverse *, const EnumDecl *,
789 const GlobalComponent *, const JsFile *, const JsResource *, const LoadInfo *,
790 const QmlComponent *, const QmlFile *, const QmlObject *,
791 const ScriptExpression *>;
792
793using TopT = std::variant<
794 std::monostate,
795 std::shared_ptr<DomEnvironment>,
796 std::shared_ptr<DomUniverse>>;
797
798using OwnerT =
799 std::variant<std::monostate,
800 std::shared_ptr<ExternalItemInfoBase>, std::shared_ptr<ExternalItemPairBase>,
801 std::shared_ptr<JsFile>, std::shared_ptr<QmlFile>,
802 std::shared_ptr<ScriptExpression>, std::shared_ptr<AstComments>,
803 std::shared_ptr<LoadInfo>, std::shared_ptr<FileLocations::Node>,
804 std::shared_ptr<DomEnvironment>, std::shared_ptr<DomUniverse>>;
805
806inline bool emptyChildrenVisitor(Path, const DomItem &, bool)
807{
808 return true;
809}
810
811class MutableDomItem;
812
814{
815public:
821
822 FileToLoad(const std::weak_ptr<DomEnvironment> &environment, const QString &canonicalPath,
823 const QString &logicalPath, const std::optional<InMemoryContents> &content);
824 FileToLoad() = default;
825
826 static FileToLoad fromMemory(const std::weak_ptr<DomEnvironment> &environment,
827 const QString &path, const QString &data);
828 static FileToLoad fromFileSystem(const std::weak_ptr<DomEnvironment> &environment,
829 const QString &canonicalPath);
830
831 std::shared_ptr<DomEnvironment> environment() const { return m_environment.lock(); }
832 QString canonicalPath() const { return m_canonicalPath; }
833 QString logicalPath() const { return m_logicalPath; }
834 void setCanonicalPath(const QString &canonicalPath) { m_canonicalPath = canonicalPath; }
835 void setLogicalPath(const QString &logicalPath) { m_logicalPath = logicalPath; }
836 std::optional<InMemoryContents> content() const { return m_content; }
837
838private:
839 std::weak_ptr<DomEnvironment> m_environment;
840 QString m_canonicalPath;
841 QString m_logicalPath;
842 std::optional<InMemoryContents> m_content;
843};
844
847public:
848 using Callback = std::function<void(const Path &, const DomItem &, const DomItem &)>;
849
851 using Visitor = function_ref<bool(const Path &, const DomItem &)>;
852 using ChildrenVisitor = function_ref<bool(const Path &, const DomItem &, bool)>;
853
855 static ErrorGroups myErrors();
858
860
861 template<typename F>
862 auto visitEl(F f) const
863 {
864 return std::visit(f, this->m_element);
865 }
866
867 explicit operator bool() const { return m_kind != DomType::Empty; }
869 return m_kind;
870 }
871 QString internalKindStr() const { return domTypeToString(internalKind()); }
873 {
874 if (m_kind == DomType::ConstantData)
875 return std::get<ConstantData>(m_element).domKind();
876 else
877 return kind2domKind(m_kind);
878 }
879
880 Path canonicalPath() const;
881
882 DomItem filterUp(function_ref<bool(DomType k, const DomItem &)> filter, FilterUpOptions options) const;
884 DomItem container() const;
885 DomItem owner() const;
886 DomItem top() const;
887 DomItem environment() const;
888 DomItem universe() const;
889 std::shared_ptr<QQmlJSResourceFileMapper> resourceFileMapper() const;
890 DomItem containingFile() const;
892 DomItem goToFile(const QString &filePath) const;
893 DomItem goUp(int) const;
894 DomItem directParent() const;
895
896 DomItem qmlObject(GoTo option = GoTo::Strict,
898 DomItem fileObject(GoTo option = GoTo::Strict) const;
899 DomItem rootQmlObject(GoTo option = GoTo::Strict) const;
900 DomItem component(GoTo option = GoTo::Strict) const;
904
905 // convenience getters
906 DomItem get(const ErrorHandler &h = nullptr, QList<Path> *visitedRefs = nullptr) const;
907 QList<DomItem> getAll(const ErrorHandler &h = nullptr, QList<Path> *visitedRefs = nullptr) const;
913 bool isCanonicalChild(const DomItem &child) const;
914 bool hasAnnotations() const;
915 QString name() const { return field(Fields::name).value().toString(); }
916 DomItem pragmas() const { return field(Fields::pragmas); }
917 DomItem ids() const { return field(Fields::ids); }
918 QString idStr() const { return field(Fields::idStr).value().toString(); }
919 DomItem propertyInfos() const { return field(Fields::propertyInfos); }
920 PropertyInfo propertyInfoWithName(const QString &name) const;
922 DomItem propertyDefs() const { return field(Fields::propertyDefs); }
923 DomItem bindings() const { return field(Fields::bindings); }
924 DomItem methods() const { return field(Fields::methods); }
925 DomItem enumerations() const { return field(Fields::enumerations); }
926 DomItem children() const { return field(Fields::children); }
927 DomItem child(index_type i) const { return field(Fields::children).index(i); }
929 {
931 return field(Fields::annotations);
932 else
933 return DomItem();
934 }
935
936 bool resolve(const Path &path, Visitor visitor, const ErrorHandler &errorHandler,
937 const Path &fullPath = Path(), QList<Path> *visitedRefs = nullptr) const;
938
939 DomItem operator[](const Path &path) const;
940 DomItem operator[](QStringView component) const;
941 DomItem operator[](const QString &component) const;
942 DomItem operator[](FieldName name) const { return field(name); }
943 DomItem operator[](const char16_t *component) const
944 {
945 return (*this)[QStringView(component)];
946 } // to avoid clash with stupid builtin ptrdiff_t[DomItem&], coming from C
947 DomItem operator[](index_type i) const { return index(i); }
948 DomItem operator[](int i) const { return index(i); }
949 index_type size() const { return indexes() + keys().size(); }
950 index_type length() const { return size(); }
951
952 DomItem path(const Path &p, const ErrorHandler &h = &defaultErrorHandler) const;
953
954 QList<QString> fields() const;
955 DomItem field(FieldName name) const;
956
957 index_type indexes() const;
958 DomItem index(index_type) const;
959 bool visitIndexes(function_ref<bool(const DomItem &)> visitor) const;
960
961 QSet<QString> keys() const;
962 QStringList sortedKeys() const;
963 DomItem key(const QString &name) const;
964 DomItem key(QStringView name) const { return key(name.toString()); }
965 bool visitKeys(function_ref<bool(const QString &, const DomItem &)> visitor) const;
966
967 QList<DomItem> values() const;
968 void writeOutPre(OutWriter &lw) const;
969 void writeOut(OutWriter &lw) const;
970 void writeOutPost(OutWriter &lw) const;
971 bool writeOutForFile(OutWriter &ow, WriteOutChecks extraChecks) const;
972 bool writeOut(const QString &path, const LineWriterOptions &opt = LineWriterOptions(),
973 FileWriter *fw = nullptr,
974 WriteOutChecks extraChecks = WriteOutCheck::Default) const;
975
976 bool visitTree(const Path &basePath, ChildrenVisitor visitor,
977 VisitOptions options = VisitOption::Default,
978 ChildrenVisitor openingVisitor = emptyChildrenVisitor,
979 ChildrenVisitor closingVisitor = emptyChildrenVisitor,
980 const FieldFilter &filter = FieldFilter::noFilter()) const;
981 bool visitPrototypeChain(function_ref<bool(const DomItem &)> visitor,
982 VisitPrototypesOptions options = VisitPrototypesOption::Normal,
983 const ErrorHandler &h = nullptr, QSet<quintptr> *visited = nullptr,
984 QList<Path> *visitedRefs = nullptr) const;
985 bool visitDirectAccessibleScopes(function_ref<bool(const DomItem &)> visitor,
986 VisitPrototypesOptions options = VisitPrototypesOption::Normal,
987 const ErrorHandler &h = nullptr, QSet<quintptr> *visited = nullptr,
988 QList<Path> *visitedRefs = nullptr) const;
989 bool
990 visitStaticTypePrototypeChains(function_ref<bool(const DomItem &)> visitor,
991 VisitPrototypesOptions options = VisitPrototypesOption::Normal,
992 const ErrorHandler &h = nullptr, QSet<quintptr> *visited = nullptr,
993 QList<Path> *visitedRefs = nullptr) const;
994
995 bool visitUp(function_ref<bool(const DomItem &)> visitor) const;
996 bool visitScopeChain(
997 function_ref<bool(const DomItem &)> visitor, LookupOptions = LookupOption::Normal,
998 const ErrorHandler &h = nullptr, QSet<quintptr> *visited = nullptr,
999 QList<Path> *visitedRefs = nullptr) const;
1001 const QString &name, function_ref<bool(const DomItem &)> visitor) const;
1002 bool visitLookup1(
1003 const QString &symbolName, function_ref<bool(const DomItem &)> visitor,
1004 LookupOptions = LookupOption::Normal, const ErrorHandler &h = nullptr,
1005 QSet<quintptr> *visited = nullptr, QList<Path> *visitedRefs = nullptr) const;
1006 bool visitLookup(
1007 const QString &symbolName, function_ref<bool(const DomItem &)> visitor,
1008 LookupType type = LookupType::Symbol, LookupOptions = LookupOption::Normal,
1009 const ErrorHandler &errorHandler = nullptr, QSet<quintptr> *visited = nullptr,
1010 QList<Path> *visitedRefs = nullptr) const;
1012 const QString &name, function_ref<bool(const DomItem &)> visitor) const;
1014 const ErrorHandler &h = nullptr, QList<Path> *visitedRefs = nullptr) const;
1016 const QString &symbolName, LookupType type = LookupType::Symbol,
1017 LookupOptions = LookupOption::Normal, const ErrorHandler &errorHandler = nullptr) const;
1019 const QString &symbolName, LookupType type = LookupType::Symbol,
1020 LookupOptions = LookupOption::Normal, const ErrorHandler &errorHandler = nullptr) const;
1021
1022 quintptr id() const;
1023 Path pathFromOwner() const;
1024 QString canonicalFilePath() const;
1026 bool commitToBase(const std::shared_ptr<DomEnvironment> &validPtr = nullptr) const;
1028 QCborValue value() const;
1029
1030 void dumpPtr(const Sink &sink) const;
1031 void dump(const Sink &, int indent = 0,
1032 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter =
1033 noFilter) const;
1035 dump(const QString &path,
1036 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)>
1037 filter = noFilter,
1038 int indent = 0, FileWriter *fw = nullptr) const;
1039 QString toString() const;
1040
1041 // OwnigItem elements
1042 int derivedFrom() const;
1043 int revision() const;
1044 QDateTime createdAt() const;
1045 QDateTime frozenAt() const;
1047
1048 void addError(ErrorMessage &&msg) const;
1049 ErrorHandler errorHandler() const;
1050 void clearErrors(const ErrorGroups &groups = ErrorGroups({}), bool iterate = true) const;
1051 // return false if a quick exit was requested
1052 bool iterateErrors(
1053 function_ref<bool (const DomItem &, const ErrorMessage &)> visitor, bool iterate,
1054 Path inPath = Path()) const;
1055
1056 bool iterateSubOwners(function_ref<bool(const DomItem &owner)> visitor) const;
1057 bool iterateDirectSubpaths(DirectVisitor v) const;
1058
1059 template<typename T>
1060 DomItem subDataItem(const PathEls::PathComponent &c, const T &value) const;
1061 template<typename T>
1062 DomItem subValueItem(const PathEls::PathComponent &c, const T &value) const;
1063 template <typename T>
1064 bool invokeVisitorOnValue(DirectVisitor visitor, const PathEls::PathComponent &c,
1065 const T &value) const;
1066 template <typename F>
1067 bool invokeVisitorOnLazyField(DirectVisitor visitor, FieldName f, F valueF) const
1068 {
1069 PathEls::PathComponent c = f;
1070 auto lazyWrap = [this, &c, &valueF]() {
1071 return this->subValueItem<decltype(valueF())>(c, valueF());
1072 };
1073 return visitor(c, lazyWrap);
1074 }
1075 DomItem subReferencesItem(const PathEls::PathComponent &c, const QList<Path> &paths) const;
1076 DomItem subReferenceItem(const PathEls::PathComponent &c, const Path &referencedObject) const;
1077 bool invokeVisitorOnReference(DirectVisitor visitor, FieldName f,
1078 const Path &referencedObject) const
1079 {
1080 PathEls::PathComponent c = f;
1081 return visitor(c, [c, this, referencedObject]() {
1082 return this->subReferenceItem(c, referencedObject);
1083 });
1084 }
1085 bool invokeVisitorOnReferences(DirectVisitor visitor, FieldName f,
1086 const QList<Path> &paths) const
1087 {
1088 PathEls::PathComponent c = f;
1089 return visitor(c, [c, this, paths]() { return this->subReferencesItem(c, paths); });
1090 }
1091 DomItem subListItem(const List &list) const;
1092 DomItem subMapItem(const Map &map) const;
1093
1095 {
1096 Q_ASSERT(obj);
1097 return DomItem(m_top, m_owner, m_ownerPath, ScriptElementDomWrapper(obj));
1098 }
1099
1100 template<typename Owner>
1101 DomItem subOwnerItem(const PathEls::PathComponent &c, Owner o) const
1102 {
1103 if constexpr (domTypeIsUnattachedOwningItem(Owner::element_type::kindValue))
1104 return DomItem(m_top, o, canonicalPath().withComponent(c), o.get());
1105 else
1106 return DomItem(m_top, o, Path(), o.get());
1107 }
1108 template <typename T>
1109 DomItem wrap(const PathEls::PathComponent &c, const T &obj) const;
1110 template <typename T>
1111 bool invokeVisitorOnField(DirectVisitor visitor, FieldName f, T &obj) const
1112 {
1113 PathEls::PathComponent c = f;
1114 auto lazyWrap = [this, &c, &obj]() { return this->wrap<T>(c, obj); };
1115 return visitor(c, lazyWrap);
1116 }
1117
1118 DomItem() = default;
1119 DomItem(const std::shared_ptr<DomEnvironment> &);
1120 DomItem(const std::shared_ptr<DomUniverse> &);
1121
1122 // TODO move to DomEnvironment?
1123 static DomItem fromCode(const QString &code, DomType fileType = DomType::QmlFile);
1124
1125 // --- start of potentially dangerous stuff, make private? ---
1126
1127 std::shared_ptr<DomTop> topPtr() const;
1128 std::shared_ptr<OwningItem> owningItemPtr() const;
1129
1130 // keep the DomItem around to ensure that it doesn't get deleted
1131 template<typename T, typename std::enable_if<std::is_base_of_v<DomBase, T>, bool>::type = true>
1132 T const *as() const
1133 {
1134 if (m_kind == T::kindValue) {
1135 if constexpr (domTypeIsObjWrap(T::kindValue) || domTypeIsValueWrap(T::kindValue))
1136 return std::get<SimpleObjectWrap>(m_element)->as<T>();
1137 else
1138 return static_cast<T const *>(base());
1139 }
1140 return nullptr;
1141 }
1142
1143 template<typename T, typename std::enable_if<!std::is_base_of_v<DomBase, T>, bool>::type = true>
1144 T const *as() const
1145 {
1146 if (m_kind == T::kindValue) {
1147 Q_ASSERT(domTypeIsObjWrap(m_kind) || domTypeIsValueWrap(m_kind));
1148 return std::get<SimpleObjectWrap>(m_element)->as<T>();
1149 }
1150 return nullptr;
1151 }
1152
1153 template<typename T>
1154 std::shared_ptr<T> ownerAs() const;
1155
1156 template<typename Owner, typename T>
1157 DomItem copy(const Owner &owner, const Path &ownerPath, const T &base) const
1158 {
1159 Q_ASSERT(!std::holds_alternative<std::monostate>(m_top));
1160 static_assert(IsInlineDom<std::decay_t<T>>::value, "Expected an inline item or pointer");
1161 return DomItem(m_top, owner, ownerPath, base);
1162 }
1163
1164 template<typename Owner>
1165 DomItem copy(const Owner &owner, const Path &ownerPath) const
1166 {
1167 Q_ASSERT(!std::holds_alternative<std::monostate>(m_top));
1168 return DomItem(m_top, owner, ownerPath, owner.get());
1169 }
1170
1171 template<typename T>
1172 DomItem copy(const T &base) const
1173 {
1174 Q_ASSERT(!std::holds_alternative<std::monostate>(m_top));
1175 using BaseT = std::decay_t<T>;
1176 static_assert(!std::is_same_v<BaseT, ElementT>,
1177 "variant not supported, pass in the stored types");
1178 static_assert(IsInlineDom<BaseT>::value || std::is_same_v<BaseT, std::monostate>,
1179 "expected either a pointer or an inline item");
1180
1181 if constexpr (IsSharedPointerToDomObject<BaseT>::value)
1182 return DomItem(m_top, base, Path(), base.get());
1183 else if constexpr (IsInlineDom<BaseT>::value)
1184 return DomItem(m_top, m_owner, m_ownerPath, base);
1185
1186 Q_UNREACHABLE_RETURN(DomItem(m_top, m_owner, m_ownerPath, nullptr));
1187 }
1188
1189private:
1190 template <typename T>
1191 std::shared_ptr<T> ownerAs_impl() const
1192 {
1193 if (auto p = get_if<std::shared_ptr<T>>(&m_owner))
1194 return *p;
1195 return nullptr;
1196 }
1197
1198 enum class WriteOutCheckResult { Success, Failed };
1199 WriteOutCheckResult performWriteOutChecks(const DomItem &, OutWriter &, WriteOutChecks) const;
1200 const DomBase *base() const;
1201
1202 template<typename Env, typename Owner>
1203 DomItem(Env, Owner, Path, std::nullptr_t) : DomItem()
1204 {
1205 }
1206
1207 template<typename Env, typename Owner, typename T,
1208 typename = std::enable_if_t<IsInlineDom<std::decay_t<T>>::value>>
1209 DomItem(Env env, Owner owner, const Path &ownerPath, const T &el)
1210 : m_top(env), m_owner(owner), m_ownerPath(ownerPath), m_element(el)
1211 {
1212 using BaseT = std::decay_t<T>;
1213 if constexpr (std::is_pointer_v<BaseT>) {
1214 if (!el || el->kind() == DomType::Empty) { // avoid null ptr, and allow only a
1215 // single kind of Empty
1216 m_kind = DomType::Empty;
1217 m_top = std::monostate();
1218 m_owner = std::monostate();
1219 m_ownerPath = Path();
1220 m_element = Empty();
1221 } else {
1222 using DomT = std::remove_pointer_t<BaseT>;
1223 m_element = el;
1224 m_kind = DomT::kindValue;
1225 }
1226 } else {
1227 static_assert(!std::is_same_v<BaseT, ElementT>,
1228 "variant not supported, pass in the internal type");
1229 m_kind = el->kind();
1230 }
1231 }
1232 friend class DomBase;
1233 friend class DomElement;
1234 friend class Map;
1235 friend class List;
1236 friend class QmlObject;
1237 friend class DomUniverse;
1238 friend class DomEnvironment;
1240 friend class ConstantData;
1241 friend class MutableDomItem;
1242 friend class ScriptExpression;
1243 friend class AstComments;
1244 friend class FileLocations::Node;
1245 friend class TestDomItem;
1246 friend QMLDOM_EXPORT bool operator==(const DomItem &, const DomItem &);
1247 DomType m_kind = DomType::Empty;
1248 TopT m_top;
1249 OwnerT m_owner;
1250 Path m_ownerPath;
1251 ElementT m_element = Empty();
1252};
1253
1254QMLDOM_EXPORT bool operator==(const DomItem &o1, const DomItem &o2);
1255
1256inline bool operator!=(const DomItem &o1, const DomItem &o2)
1257{
1258 return !(o1 == o2);
1259}
1260
1261template<typename T>
1262static DomItem keyMultiMapHelper(const DomItem &self, const QString &key,
1263 const QMultiMap<QString, T> &mmap)
1264{
1265 auto it = mmap.find(key);
1266 auto end = mmap.cend();
1267 if (it == end)
1268 return DomItem();
1269 else {
1270 // special case single element (++it == end || it.key() != key)?
1271 QList<const T *> values;
1272 while (it != end && it.key() == key)
1273 values.append(&(*it++));
1274 ListP ll(self.pathFromOwner().withComponent(PathEls::Key(key)), values, QString(),
1276 return self.copy(ll);
1277 }
1278}
1279
1280template<typename T>
1281Map Map::fromMultiMapRef(const Path &pathFromOwner, const QMultiMap<QString, T> &mmap)
1282{
1283 return Map(
1284 pathFromOwner,
1285 [&mmap](const DomItem &self, const QString &key) {
1286 return keyMultiMapHelper(self, key, mmap);
1287 },
1288 [&mmap](const DomItem &) { return QSet<QString>(mmap.keyBegin(), mmap.keyEnd()); },
1289 QLatin1String(typeid(T).name()));
1290}
1291
1292template<typename T>
1293Map Map::fromMapRef(
1294 const Path &pathFromOwner, const QMap<QString, T> &map,
1295 const std::function<DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper)
1296{
1297 return Map(
1298 pathFromOwner,
1299 [&map, elWrapper](const DomItem &self, const QString &key) {
1300 const auto it = map.constFind(key);
1301 if (it == map.constEnd())
1302 return DomItem();
1303 return elWrapper(self, PathEls::Key(key), it.value());
1304 },
1305 [&map](const DomItem &) { return QSet<QString>(map.keyBegin(), map.keyEnd()); },
1306 QLatin1String(typeid(T).name()));
1307}
1308
1309template<typename MapT>
1310QSet<QString> Map::fileRegionKeysFromMap(const MapT &map)
1311{
1312 QSet<QString> keys;
1313 std::transform(map.keyBegin(), map.keyEnd(), std::inserter(keys, keys.begin()), fileLocationRegionName);
1314 return keys;
1315}
1316
1317template<typename T>
1318Map Map::fromFileRegionMap(const Path &pathFromOwner, const QMap<FileLocationRegion, T> &map)
1319{
1320 auto result = Map(
1321 pathFromOwner,
1322 [&map](const DomItem &mapItem, const QString &key) -> DomItem {
1323 auto it = map.constFind(fileLocationRegionValue(key));
1324 if (it == map.constEnd())
1325 return {};
1326
1327 return mapItem.wrap(PathEls::Key(key), *it);
1328 },
1329 [&map](const DomItem &) { return fileRegionKeysFromMap(map); },
1330 QString::fromLatin1(typeid(T).name()));
1331 return result;
1332}
1333
1334template<typename T>
1335List List::fromQList(
1336 const Path &pathFromOwner, const QList<T> &list,
1337 const std::function<DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper,
1338 ListOptions options)
1339{
1340 index_type len = list.size();
1341 if (options == ListOptions::Reverse) {
1342 return List(
1343 pathFromOwner,
1344 [list, elWrapper](const DomItem &self, index_type i) mutable {
1345 if (i < 0 || i >= list.size())
1346 return DomItem();
1347 return elWrapper(self, PathEls::Index(i), list[list.size() - i - 1]);
1348 },
1349 [len](const DomItem &) { return len; }, nullptr, QLatin1String(typeid(T).name()));
1350 } else {
1351 return List(
1352 pathFromOwner,
1353 [list, elWrapper](const DomItem &self, index_type i) mutable {
1354 if (i < 0 || i >= list.size())
1355 return DomItem();
1356 return elWrapper(self, PathEls::Index(i), list[i]);
1357 },
1358 [len](const DomItem &) { return len; }, nullptr, QLatin1String(typeid(T).name()));
1359 }
1360}
1361
1362template<typename T>
1363List List::fromQListRef(
1364 const Path &pathFromOwner, const QList<T> &list,
1365 const std::function<DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper,
1366 ListOptions options)
1367{
1368 if (options == ListOptions::Reverse) {
1369 return List(
1370 pathFromOwner,
1371 [&list, elWrapper](const DomItem &self, index_type i) {
1372 if (i < 0 || i >= list.size())
1373 return DomItem();
1374 return elWrapper(self, PathEls::Index(i), list[list.size() - i - 1]);
1375 },
1376 [&list](const DomItem &) { return list.size(); }, nullptr,
1377 QLatin1String(typeid(T).name()));
1378 } else {
1379 return List(
1380 pathFromOwner,
1381 [&list, elWrapper](const DomItem &self, index_type i) {
1382 if (i < 0 || i >= list.size())
1383 return DomItem();
1384 return elWrapper(self, PathEls::Index(i), list[i]);
1385 },
1386 [&list](const DomItem &) { return list.size(); }, nullptr,
1387 QLatin1String(typeid(T).name()));
1388 }
1389}
1390
1392protected:
1393 virtual std::shared_ptr<OwningItem> doCopy(const DomItem &self) const = 0;
1394
1395public:
1396 OwningItem(const OwningItem &o);
1397 OwningItem(int derivedFrom=0);
1398 OwningItem(int derivedFrom, const QDateTime &lastDataUpdateAt);
1399 OwningItem(const OwningItem &&) = delete;
1400 OwningItem &operator=(const OwningItem &&) = delete;
1401 static int nextRevision();
1402
1403 Path canonicalPath(const DomItem &self) const override = 0;
1404
1405 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override;
1406 std::shared_ptr<OwningItem> makeCopy(const DomItem &self) const { return doCopy(self); }
1407 Path pathFromOwner() const override final { return Path(); }
1408 DomItem containingObject(const DomItem &self) const override;
1409 int derivedFrom() const;
1410 virtual int revision() const;
1411
1412 QDateTime createdAt() const;
1413 virtual QDateTime lastDataUpdateAt() const;
1414 virtual void refreshedDataAt(QDateTime tNew);
1415
1416 // explicit freeze handling needed?
1417 virtual bool frozen() const;
1418 virtual bool freeze();
1419 QDateTime frozenAt() const;
1420
1421 virtual void addError(const DomItem &self, ErrorMessage &&msg);
1422 void addErrorLocal(ErrorMessage &&msg);
1423 void clearErrors(const ErrorGroups &groups = ErrorGroups({}));
1424 // return false if a quick exit was requested
1425 bool iterateErrors(
1426 const DomItem &self,
1427 function_ref<bool(const DomItem &source, const ErrorMessage &msg)> visitor,
1428 const Path &inPath = Path());
1430 QMutexLocker l(mutex());
1431 return m_errors;
1432 }
1433
1434 virtual bool iterateSubOwners(const DomItem &self, function_ref<bool(const DomItem &owner)> visitor);
1435
1436 QBasicMutex *mutex() const { return &m_mutex; }
1437private:
1438 mutable QBasicMutex m_mutex;
1439 int m_derivedFrom;
1440 int m_revision;
1441 QDateTime m_createdAt;
1442 QDateTime m_lastDataUpdateAt;
1443 QDateTime m_frozenAt;
1444 QMultiMap<Path, ErrorMessage> m_errors;
1445 QMap<ErrorMessage, quint32> m_errorsCounts;
1446};
1447
1448template<typename T>
1449std::shared_ptr<T> DomItem::ownerAs() const
1450{
1451 static_assert(domTypeIsOwningItem(T::kindValue),
1452 "unexpected non owning value in ownerAs");
1453 if constexpr (T::kindValue == DomType::FileLocationsNode)
1454 return std::static_pointer_cast<T>(ownerAs_impl<FileLocations::Node>());
1455 else if constexpr (T::kindValue == DomType::ExternalItemInfo)
1456 return std::static_pointer_cast<T>(ownerAs_impl<ExternalItemInfoBase>());
1457 else if constexpr (T::kindValue == DomType::ExternalItemPair)
1458 return std::static_pointer_cast<T>(ownerAs_impl<ExternalItemPairBase>());
1459 else
1460 return ownerAs_impl<T>();
1461}
1462
1463template<int I>
1464struct rank : rank<I - 1>
1465{
1466 static_assert(I > 0, "");
1467};
1468template<>
1469struct rank<0>
1470{
1471};
1472
1473template<typename T>
1474auto writeOutWrap(const T &t, const DomItem &self, OutWriter &lw, rank<1>)
1475 -> decltype(t.writeOut(self, lw))
1476{
1477 t.writeOut(self, lw);
1478}
1479
1480template<typename T>
1481auto writeOutWrap(const T &, const DomItem &, OutWriter &, rank<0>) -> void
1482{
1483 qCWarning(writeOutLog) << "Ignoring writeout to wrapped object not supporting it ("
1484 << typeid(T).name();
1485}
1486template<typename T>
1487auto writeOutWrap(const T &t, const DomItem &self, OutWriter &lw) -> void
1488{
1489 writeOutWrap(t, self, lw, rank<1>());
1490}
1491
1492template<typename T>
1493void SimpleObjectWrapT<T>::writeOut(const DomItem &self, OutWriter &lw) const
1494{
1495 writeOutWrap<T>(*asT(), self, lw);
1496}
1497
1498QMLDOM_EXPORT QDebug operator<<(QDebug debug, const DomItem &c);
1499
1500// TODO QTBUG-121518 quite some methods are used only in the "examples"
1501// Even though MutableDomItem provides some API for modifying internal data,
1502// de-facto it's not very helpful / convinient / intuitive to use
1503// Moreover it just amplifies and repeats issues of DomItem interface,
1504// a.k.a. abuse or misuse of type erasure technique
1506public:
1508
1509 explicit operator bool() const
1510 {
1511 return bool(m_owner);
1512 } // this is weaker than item(), but normally correct
1514 QString internalKindStr() { return domTypeToString(internalKind()); }
1515 DomKind domKind() { return kind2domKind(internalKind()); }
1516
1517 Path canonicalPath() const { return m_owner.canonicalPath().withPath(m_pathFromOwner); }
1519 {
1520 if (m_pathFromOwner)
1521 return MutableDomItem(m_owner, m_pathFromOwner.split().pathToSource);
1522 else {
1523 DomItem cObj = m_owner.containingObject();
1525 }
1526 }
1527
1529 {
1530 if (m_pathFromOwner)
1531 return MutableDomItem(m_owner, m_pathFromOwner.dropTail());
1532 else {
1534 }
1535 }
1536
1539 {
1540 return MutableDomItem(item().qmlObject(option, fOptions));
1541 }
1543 {
1544 return MutableDomItem(item().fileObject(option));
1545 }
1547 {
1548 return MutableDomItem(item().rootQmlObject(option));
1549 }
1551
1553 {
1554 return MutableDomItem { item().component(option) };
1555 }
1556 MutableDomItem owner() { return MutableDomItem(m_owner); }
1560 Path pathFromOwner() { return m_pathFromOwner; }
1562 MutableDomItem operator[](QStringView component) { return MutableDomItem(item()[component]); }
1563 MutableDomItem operator[](const QString &component)
1564 {
1565 return MutableDomItem(item()[component]);
1566 }
1567 MutableDomItem operator[](const char16_t *component)
1568 {
1569 // to avoid clash with stupid builtin ptrdiff_t[MutableDomItem&], coming from C
1570 return MutableDomItem(item()[QStringView(component)]);
1571 }
1572 MutableDomItem operator[](index_type i) { return MutableDomItem(item().index(i)); }
1573
1575
1576 QList<QString> const fields() { return item().fields(); }
1578 index_type indexes() { return item().indexes(); }
1579 MutableDomItem index(index_type i) { return MutableDomItem(item().index(i)); }
1580
1581 QSet<QString> const keys() { return item().keys(); }
1582 MutableDomItem key(const QString &name) { return MutableDomItem(item().key(name)); }
1583 MutableDomItem key(QStringView name) { return key(name.toString()); }
1584
1585 void
1586 dump(const Sink &s, int indent = 0,
1587 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter = noFilter)
1588 {
1589 item().dump(s, indent, filter);
1590 }
1592 dump(const QString &path,
1593 function_ref<bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)>
1594 filter = noFilter,
1595 int indent = 0, FileWriter *fw = nullptr)
1596 {
1597 return item().dump(path, filter, indent, fw);
1598 }
1599 void writeOut(OutWriter &lw) { return item().writeOut(lw); }
1600 bool writeOut(const QString &path, const LineWriterOptions &opt = LineWriterOptions(),
1601 FileWriter *fw = nullptr)
1602 {
1603 return item().writeOut(path, opt, fw);
1604 }
1605
1610 bool commitToBase(const std::shared_ptr<DomEnvironment> &validEnvPtr = nullptr)
1611 {
1612 return item().commitToBase(validEnvPtr);
1613 }
1614 QString canonicalFilePath() const { return item().canonicalFilePath(); }
1615
1617
1618 QCborValue value() { return item().value(); }
1619
1620 QString toString() { return item().toString(); }
1621
1622 // convenience getters
1623 QString name() { return item().name(); }
1626 QString idStr() { return item().idStr(); }
1631 MutableDomItem child(index_type i) { return MutableDomItem(item().child(i)); }
1633
1634 // // OwnigItem elements
1635 int derivedFrom() { return m_owner.derivedFrom(); }
1636 int revision() { return m_owner.revision(); }
1637 QDateTime createdAt() { return m_owner.createdAt(); }
1638 QDateTime frozenAt() { return m_owner.frozenAt(); }
1639 QDateTime lastDataUpdateAt() { return m_owner.lastDataUpdateAt(); }
1640
1641 void addError(ErrorMessage &&msg) { item().addError(std::move(msg)); }
1643
1644 // convenience setters
1649 const MethodInfo &functionDef, AddOption option = AddOption::Overwrite);
1651
1652 MutableDomItem() = default;
1653 MutableDomItem(const DomItem &owner, const Path &pathFromOwner):
1655 {}
1659
1660 std::shared_ptr<DomTop> topPtr() { return m_owner.topPtr(); }
1661 std::shared_ptr<OwningItem> owningItemPtr() { return m_owner.owningItemPtr(); }
1662
1663 template<typename T>
1664 T const *as()
1665 {
1666 return item().as<T>();
1667 }
1668
1669 template <typename T>
1671 Q_ASSERT(!m_owner || !m_owner.owningItemPtr()->frozen());
1672
1673 DomItem self = item();
1674 if (self.m_kind != T::kindValue)
1675 return nullptr;
1676
1677 const T *t = nullptr;
1678 if constexpr (domTypeIsObjWrap(T::kindValue) || domTypeIsValueWrap(T::kindValue))
1679 t = static_cast<const SimpleObjectWrapBase *>(self.base())->as<T>();
1680 else if constexpr (std::is_base_of<DomBase, T>::value)
1681 t = static_cast<const T *>(self.base());
1682 else
1683 Q_UNREACHABLE_RETURN(nullptr);
1684
1685 // Nasty. But since ElementT has to store the const pointers, we allow it in this one place.
1686 return const_cast<T *>(t);
1687 }
1688
1689 template<typename T>
1690 std::shared_ptr<T> ownerAs() const
1691 {
1692 return m_owner.ownerAs<T>();
1693 }
1694 // it is dangerous to assume it stays valid when updates are preformed...
1695 DomItem item() const { return m_owner.path(m_pathFromOwner); }
1696
1697 friend bool operator==(const MutableDomItem &o1, const MutableDomItem &o2)
1698 {
1699 return o1.m_owner == o2.m_owner && o1.m_pathFromOwner == o2.m_pathFromOwner;
1700 }
1701 friend bool operator!=(const MutableDomItem &o1, const MutableDomItem &o2)
1702 {
1703 return !(o1 == o2);
1704 }
1705
1706private:
1707 DomItem m_owner;
1708 Path m_pathFromOwner;
1709};
1710
1711QMLDOM_EXPORT QDebug operator<<(QDebug debug, const MutableDomItem &c);
1712
1713template<typename K, typename T>
1714Path insertUpdatableElementInMultiMap(const Path &mapPathFromOwner, QMultiMap<K, T> &mmap, K key,
1715 const T &value, AddOption option = AddOption::KeepExisting,
1716 T **valuePtr = nullptr)
1717{
1718 if (option == AddOption::Overwrite) {
1719 auto it = mmap.find(key);
1720 if (it != mmap.end()) {
1721 T &v = *it;
1722 v = value;
1723 if (++it != mmap.end() && it.key() == key) {
1724 qWarning() << " requested overwrite of " << key
1725 << " that contains aleready multiple entries in" << mapPathFromOwner;
1726 }
1727 Path newPath = mapPathFromOwner.withKey(key).withIndex(0);
1728 v.updatePathFromOwner(newPath);
1729 if (valuePtr)
1730 *valuePtr = &v;
1731 return newPath;
1732 }
1733 }
1734 mmap.insert(key, value);
1735 auto it = mmap.find(key);
1736 auto it2 = it;
1737 int nVal = 0;
1738 while (it2 != mmap.end() && it2.key() == key) {
1739 ++nVal;
1740 ++it2;
1741 }
1742 Path newPath = mapPathFromOwner.withKey(key).withIndex(nVal-1);
1743 T &v = *it;
1744 v.updatePathFromOwner(newPath);
1745 if (valuePtr)
1746 *valuePtr = &v;
1747 return newPath;
1748}
1749
1750template<typename T>
1751Path appendUpdatableElementInQList(const Path &listPathFromOwner, QList<T> &list, const T &value,
1752 T **vPtr = nullptr)
1753{
1754 int idx = list.size();
1755 list.append(value);
1756 Path newPath = listPathFromOwner.withIndex(idx);
1757 T &targetV = list[idx];
1758 targetV.updatePathFromOwner(newPath);
1759 if (vPtr)
1760 *vPtr = &targetV;
1761 return newPath;
1762}
1763
1764template <typename T, typename K = QString>
1765void updatePathFromOwnerMultiMap(QMultiMap<K, T> &mmap, const Path &newPath)
1766{
1767 auto it = mmap.begin();
1768 auto end = mmap.end();
1769 index_type i = 0;
1770 K name;
1771 QList<T*> els;
1772 while (it != end) {
1773 if (i > 0 && name != it.key()) {
1774 Path pName = newPath.withKey(QString(name));
1775 for (T *el : els)
1776 el->updatePathFromOwner(pName.withIndex(--i));
1777 els.clear();
1778 els.append(&(*it));
1779 name = it.key();
1780 i = 1;
1781 } else {
1782 els.append(&(*it));
1783 name = it.key();
1784 ++i;
1785 }
1786 ++it;
1787 }
1788 Path pName = newPath.withKey(name);
1789 for (T *el : els)
1790 el->updatePathFromOwner(pName.withIndex(--i));
1791}
1792
1793template <typename T>
1794void updatePathFromOwnerQList(QList<T> &list, const Path &newPath)
1795{
1796 auto it = list.begin();
1797 auto end = list.end();
1798 index_type i = 0;
1799 while (it != end)
1800 (it++)->updatePathFromOwner(newPath.withIndex(i++));
1801}
1802
1803constexpr bool domTypeIsObjWrap(DomType k)
1804{
1805 switch (k) {
1806 case DomType::Binding:
1807 case DomType::EnumItem:
1809 case DomType::Export:
1810 case DomType::Id:
1811 case DomType::Import:
1815 case DomType::Pragma:
1817 case DomType::Version:
1818 case DomType::Comment:
1822 return true;
1823 default:
1824 return false;
1825 }
1826}
1827
1829{
1830 switch (k) {
1832 return true;
1833 default:
1834 return false;
1835 }
1836}
1837
1839{
1840 switch (k) {
1841 case DomType::QmlObject:
1844 case DomType::Reference:
1845 case DomType::Map:
1846 case DomType::List:
1847 case DomType::ListP:
1848 case DomType::EnumDecl:
1852 return true;
1853 default:
1854 return false;
1855 }
1856}
1857
1859{
1860 switch (k) {
1861
1864
1865 case DomType::JsFile:
1866 case DomType::QmlFile:
1867
1870
1871 case DomType::LoadInfo:
1873
1876 return true;
1877 default:
1878 return false;
1879 }
1880}
1881
1883{
1884 switch (k) {
1888 return true;
1889 default:
1890 return false;
1891 }
1892}
1893
1895{
1897}
1898
1899template<typename T>
1900DomItem DomItem::subValueItem(const PathEls::PathComponent &c, const T &value) const
1901{
1902 using BaseT = std::remove_cv_t<std::remove_reference_t<T>>;
1903 if constexpr (
1904 std::is_base_of_v<
1905 QCborValue,
1906 BaseT> || std::is_base_of_v<QCborArray, BaseT> || std::is_base_of_v<QCborMap, BaseT>) {
1907 return DomItem(m_top, m_owner, m_ownerPath,
1908 ConstantData(pathFromOwner().withComponent(c), value));
1909 } else if constexpr (std::is_same_v<DomItem, BaseT>) {
1910 return value;
1911 } else if constexpr (IsList<T>::value && !std::is_convertible_v<BaseT, QStringView>) {
1912 return subListItem(List::fromQList<typename BaseT::value_type>(
1913 pathFromOwner().withComponent(c), value,
1914 [](const DomItem &list, const PathEls::PathComponent &p,
1915 const typename T::value_type &v) { return list.subValueItem(p, v); }));
1916 } else if constexpr (IsSharedPointerToDomObject<BaseT>::value) {
1917 return subOwnerItem(c, value);
1918 } else {
1919 return subDataItem(c, value);
1920 }
1921}
1922
1923template<typename T>
1924DomItem DomItem::subDataItem(const PathEls::PathComponent &c, const T &value) const
1925{
1926 using BaseT = std::remove_cv_t<std::remove_reference_t<T>>;
1927 if constexpr (std::is_same_v<BaseT, ConstantData>) {
1928 return this->copy(value);
1929 } else if constexpr (std::is_base_of_v<QCborValue, BaseT>) {
1930 return DomItem(m_top, m_owner, m_ownerPath,
1931 ConstantData(pathFromOwner().withComponent(c), value));
1932 } else {
1933 return DomItem(
1934 m_top, m_owner, m_ownerPath,
1935 ConstantData(pathFromOwner().withComponent(c), QCborValue(value)));
1936 }
1937}
1938
1939template <typename T>
1940bool DomItem::invokeVisitorOnValue(DirectVisitor visitor, const PathEls::PathComponent &c,
1941 const T &value) const
1942{
1943 auto lazyWrap = [this, &c, &value]() {
1944 return this->subValueItem<T>(c, value);
1945 };
1946 return visitor(c, lazyWrap);
1947}
1948
1949template<typename T>
1950DomItem DomItem::wrap(const PathEls::PathComponent &c, const T &obj) const
1951{
1952 using BaseT = std::decay_t<T>;
1953 if constexpr (std::is_same_v<QString, BaseT> || std::is_arithmetic_v<BaseT>) {
1954 return this->subDataItem(c, QCborValue(obj));
1955 } else if constexpr (std::is_same_v<SourceLocation, BaseT>) {
1956 return this->subDataItem(c, sourceLocationToQCborValue(obj));
1957 } else if constexpr (std::is_same_v<BaseT, Reference>) {
1958 Q_ASSERT_X(false, "DomItem::wrap",
1959 "wrapping a reference object, probably an error (wrap the target path instead)");
1960 return this->copy(obj);
1961 } else if constexpr (std::is_same_v<BaseT, ConstantData>) {
1962 return this->subDataItem(c, obj);
1963 } else if constexpr (std::is_same_v<BaseT, Map>) {
1964 return this->subMapItem(obj);
1965 } else if constexpr (std::is_same_v<BaseT, List>) {
1966 return this->subListItem(obj);
1967 } else if constexpr (std::is_base_of_v<ListPBase, BaseT>) {
1968 return this->subListItem(obj);
1969 } else if constexpr (std::is_same_v<BaseT, SimpleObjectWrap>) {
1970 return DomItem(m_top, m_owner, m_ownerPath, obj);
1971 } else if constexpr (IsDomObject<BaseT>::value) {
1972 if constexpr (domTypeIsObjWrap(BaseT::kindValue) || domTypeIsValueWrap(BaseT::kindValue)) {
1973 return DomItem(
1974 m_top, m_owner, m_ownerPath,
1975 SimpleObjectWrap::fromObjectRef(this->pathFromOwner().withComponent(c), obj));
1976 } else if constexpr (domTypeIsDomElement(BaseT::kindValue)) {
1977 return this->copy(&obj);
1978 } else {
1979 qCWarning(domLog) << "Unhandled object of type " << domTypeToString(BaseT::kindValue)
1980 << " in DomItem::wrap, not using a shared_ptr for an "
1981 << "OwningItem, or unexpected wrapped object?";
1982 return DomItem();
1983 }
1984 } else if constexpr (IsSharedPointerToDomObject<BaseT>::value) {
1985 if constexpr (domTypeIsOwningItem(BaseT::element_type::kindValue)) {
1986 return this->subOwnerItem(c, obj);
1987 } else {
1988 Q_ASSERT_X(false, "DomItem::wrap", "shared_ptr with non owning item");
1989 return DomItem();
1990 }
1991 } else if constexpr (IsMultiMap<BaseT>::value) {
1992 if constexpr (std::is_same_v<typename BaseT::key_type, QString>) {
1993 return subMapItem(Map::fromMultiMapRef<typename BaseT::mapped_type>(
1994 pathFromOwner().withComponent(c), obj));
1995 } else {
1996 Q_ASSERT_X(false, "DomItem::wrap", "non string keys not supported (try .toString()?)");
1997 }
1998 } else if constexpr (IsMap<BaseT>::value) {
1999 if constexpr (std::is_same_v<typename BaseT::key_type, QString>) {
2000 return subMapItem(Map::fromMapRef<typename BaseT::mapped_type>(
2001 pathFromOwner().withComponent(c), obj,
2002 [](const DomItem &map, const PathEls::PathComponent &p,
2003 const typename BaseT::mapped_type &el) { return map.wrap(p, el); }));
2004 } else {
2005 Q_ASSERT_X(false, "DomItem::wrap", "non string keys not supported (try .toString()?)");
2006 }
2007 } else if constexpr (IsList<BaseT>::value) {
2008 if constexpr (IsDomObject<typename BaseT::value_type>::value) {
2009 return subListItem(List::fromQListRef<typename BaseT::value_type>(
2010 pathFromOwner().withComponent(c), obj,
2011 [](const DomItem &list, const PathEls::PathComponent &p,
2012 const typename BaseT::value_type &el) { return list.wrap(p, el); }));
2013 } else {
2014 Q_ASSERT_X(false, "DomItem::wrap", "Unsupported list type T");
2015 return DomItem();
2016 }
2017 } else {
2018 qCWarning(domLog) << "Cannot wrap " << typeid(BaseT).name();
2019 Q_ASSERT_X(false, "DomItem::wrap", "Do not know how to wrap type T");
2020 return DomItem();
2021 }
2022}
2023
2024template<typename T>
2025bool ListPT<T>::iterateDirectSubpaths(const DomItem &self, DirectVisitor v) const
2026{
2027 index_type len = index_type(m_pList.size());
2028 for (index_type i = 0; i < len; ++i) {
2029 if (!v(PathEls::Index(i), [this, &self, i] { return this->index(self, i); }))
2030 return false;
2031 }
2032 return true;
2033}
2034
2035template<typename T>
2036DomItem ListPT<T>::index(const DomItem &self, index_type index) const
2037{
2038 if (index >= 0 && index < m_pList.size())
2039 return self.wrap(PathEls::Index(index), *static_cast<const T *>(m_pList.value(index)));
2040 return DomItem();
2041}
2042
2043// allow inlining of DomBase
2044inline DomKind DomBase::domKind() const
2045{
2046 return kind2domKind(kind());
2047}
2048
2049inline DomItem DomBase::containingObject(const DomItem &self) const
2050{
2051 Path path = pathFromOwner();
2052 DomItem base = self.owner();
2053 if (!path) {
2054 path = canonicalPath(self);
2055 base = self;
2056 }
2057 Source source = path.split();
2058 return base.path(source.pathToSource);
2059}
2060
2061inline quintptr DomBase::id() const
2062{
2063 return quintptr(this);
2064}
2065
2066inline QString DomBase::typeName() const
2067{
2068 return domTypeToString(kind());
2069}
2070
2071inline QList<QString> DomBase::fields(const DomItem &self) const
2072{
2073 QList<QString> res;
2074 self.iterateDirectSubpaths([&res](const PathEls::PathComponent &c, function_ref<DomItem()>) {
2075 if (c.kind() == Path::Kind::Field)
2076 res.append(c.name());
2077 return true;
2078 });
2079 return res;
2080}
2081
2082inline DomItem DomBase::field(const DomItem &self, FieldName name) const
2083{
2084 DomItem res;
2085 self.iterateDirectSubpaths(
2086 [&res, name](const PathEls::PathComponent &c, function_ref<DomItem()> obj) {
2087 if (const PathEls::Field *f = c.asField(); f && f->fieldName == name) {
2088 res = obj();
2089 return false;
2090 }
2091 return true;
2092 });
2093 return res;
2094}
2095
2096inline index_type DomBase::indexes(const DomItem &self) const
2097{
2098 index_type res = 0;
2099 self.iterateDirectSubpaths([&res](const PathEls::PathComponent &c, function_ref<DomItem()>) {
2100 if (c.kind() == Path::Kind::Index) {
2101 index_type i = c.index() + 1;
2102 if (res < i)
2103 res = i;
2104 }
2105 return true;
2106 });
2107 return res;
2108}
2109
2110inline DomItem DomBase::index(const DomItem &self, qint64 index) const
2111{
2112 DomItem res;
2113 self.iterateDirectSubpaths(
2114 [&res, index](const PathEls::PathComponent &c, function_ref<DomItem()> obj) {
2115 if (c.kind() == Path::Kind::Index && c.index() == index) {
2116 res = obj();
2117 return false;
2118 }
2119 return true;
2120 });
2121 return res;
2122}
2123
2124inline QSet<QString> const DomBase::keys(const DomItem &self) const
2125{
2126 QSet<QString> res;
2127 self.iterateDirectSubpaths([&res](const PathEls::PathComponent &c, function_ref<DomItem()>) {
2128 if (c.kind() == Path::Kind::Key)
2129 res.insert(c.name());
2130 return true;
2131 });
2132 return res;
2133}
2134
2135inline DomItem DomBase::key(const DomItem &self, const QString &name) const
2136{
2137 DomItem res;
2138 self.iterateDirectSubpaths(
2139 [&res, name](const PathEls::PathComponent &c, function_ref<DomItem()> obj) {
2140 if (c.kind() == Path::Kind::Key && c.checkName(name)) {
2141 res = obj();
2142 return false;
2143 }
2144 return true;
2145 });
2146 return res;
2147}
2148
2149inline DomItem DomItem::subListItem(const List &list) const
2150{
2151 return DomItem(m_top, m_owner, m_ownerPath, list);
2152}
2153
2154inline DomItem DomItem::subMapItem(const Map &map) const
2155{
2156 return DomItem(m_top, m_owner, m_ownerPath, map);
2157}
2158
2159// TODO
2160// refactor this workaround. ExternalOWningItem is not recognized as an owning type
2161// in ownerAs.
2162std::shared_ptr<ExternalOwningItem> getFileItemOwner(const DomItem &fileItem);
2163
2164} // end namespace Dom
2165} // end namespace QQmlJS
2166
2167QT_END_NAMESPACE
2168#endif // QMLDOMITEM_H
std::pair< AST::Node *, CommentAnchor > CommentKey
void setSemanticScope(const QQmlJSScope::ConstPtr &scope)
QQmlJSScope::ConstPtr m_semanticScope
void updatePathFromOwner(const Path &newPath)
QQmlJSScope::ConstPtr semanticScope() const
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
Path addAnnotation(const Path &selfPathFromOwner, const QmlObject &annotation, QmlObject **aPtr=nullptr)
BindingValue(const QList< QmlObject > &l)
void updatePathFromOwner(const Path &newPath)
BindingValue(const std::shared_ptr< ScriptExpression > &o)
BindingValue(const BindingValue &o)
DomItem value(const DomItem &binding) const
BindingValue & operator=(const BindingValue &o)
BindingValue(const QmlObject &o)
void setValue(std::unique_ptr< BindingValue > &&value)
BindingType bindingType() const
std::shared_ptr< ScriptExpression > scriptExpressionValue() const
RegionComments & comments()
Binding & operator=(const Binding &)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const
void setBindingIdentifiers(const ScriptElementVariant &bindingIdentifiers)
Path addAnnotation(const Path &selfPathFromOwner, const QmlObject &a, QmlObject **aPtr=nullptr)
QList< QmlObject > annotations() const
void updatePathFromOwner(const Path &newPath)
static QString preCodeForName(QStringView n)
QmlObject const * objectValue() const
DomItem valueItem(const DomItem &self) const
QList< QmlObject > * arrayValue()
Binding(const Binding &o)
QList< QmlObject > const * arrayValue() const
static QString postCodeForName(QStringView)
void setAnnotations(const QList< QmlObject > &annotations)
Binding & operator=(Binding &&)=default
void writeOut(const DomItem &self, OutWriter &lw) const
Binding(const QString &m_name, const QString &scriptCode, BindingType bindingType=BindingType::Normal)
const RegionComments & comments() const
Binding(const QString &m_name, std::unique_ptr< BindingValue > &&value, BindingType bindingType=BindingType::Normal)
void writeOutValue(const DomItem &self, OutWriter &lw) const
Binding(const QString &m_name=QString())
static constexpr DomType kindValue
Binding(const QString &m_name, const QmlObject &value, BindingType bindingType=BindingType::Normal)
Binding(Binding &&o)=default
std::shared_ptr< ScriptExpression > scriptExpressionValue()
ScriptElementVariant bindingIdentifiers() const
BindingValueKind valueKind() const
Binding(const QString &m_name, const std::shared_ptr< ScriptExpression > &value, BindingType bindingType=BindingType::Normal)
const RegionComments & comments() const
CommentableDomElement & operator=(const CommentableDomElement &o)=default
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
CommentableDomElement(const CommentableDomElement &o)
CommentableDomElement(const Path &pathFromOwner=Path())
void setIsSingleton(bool isSingleton)
void updatePathFromOwner(const Path &newPath) override
Component(const Path &pathFromOwner=Path())
void setIsComposite(bool isComposite)
Component & operator=(const Component &)=default
void setIsCreatable(bool isCreatable)
void setObjects(const QList< QmlObject > &objects)
const QMultiMap< QString, EnumDecl > & enumerations() const &
Component(const Component &o)=default
Path addObject(const QmlObject &object, QmlObject **oPtr=nullptr)
void setName(const QString &name)
Path attachedTypePath(const DomItem &) const
void setEnumerations(const QMultiMap< QString, EnumDecl > &enumerations)
DomItem field(const DomItem &self, FieldName name) const override
Path addEnumeration(const EnumDecl &enumeration, AddOption option=AddOption::Overwrite, EnumDecl **ePtr=nullptr)
void setAttachedTypeName(const QString &name)
bool iterateDirectSubpaths(const DomItem &, DirectVisitor) const override
QString attachedTypeName() const
Component(const QString &name)
void setAttachedTypePath(const Path &p)
const QList< QmlObject > & objects() const &
DomKind domKind() const override
QCborValue value() const override
quintptr id() const override
static constexpr DomType kindValue
DomType kind() const override
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
ConstantData & operator*()
ConstantData(const Path &pathFromOwner, const QCborValue &value)
const ConstantData & operator*() const
const ConstantData * operator->() const
ConstantData * operator->()
QString typeName() const
virtual DomType kind() const =0
virtual DomKind domKind() const
virtual Path canonicalPath(const DomItem &self) const =0
virtual Path pathFromOwner() const =0
virtual QList< QString > fields(const DomItem &self) const
virtual bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const =0
virtual index_type indexes(const DomItem &self) const
const DomBase * domBase() const
virtual DomItem containingObject(const DomItem &self) const
virtual void writeOut(const DomItem &self, OutWriter &lw) const
virtual QSet< QString > const keys(const DomItem &self) const
virtual quintptr id() const
virtual QCborValue value() const
virtual QString canonicalFilePath(const DomItem &self) const
virtual ~DomBase()=default
virtual void dump(const DomItem &, const Sink &sink, int indent, FilterT filter) const
virtual DomItem key(const DomItem &self, const QString &name) const
virtual DomItem field(const DomItem &self, FieldName name) const
virtual DomItem index(const DomItem &self, index_type index) const
DomElement & operator=(const DomElement &)=default
virtual void updatePathFromOwner(const Path &newPath)
Path canonicalPath(const DomItem &self) const override
Path pathFromOwner() const override
DomElement(const DomElement &o)=default
DomElement(const Path &pathFromOwner=Path())
DomItem containingObject(const DomItem &self) const override
A value type that references any element of the Dom.
DomItem bindings() const
DomItem top() const
DomItem goUp(int) const
QDateTime createdAt() const
T const * as() const
std::shared_ptr< OwningItem > owningItemPtr() const
DomItem operator[](const QString &component) const
std::function< void(const Path &, const DomItem &, const DomItem &)> Callback
QString toString() const
void writeOutPost(OutWriter &lw) const
bool invokeVisitorOnValue(DirectVisitor visitor, const PathEls::PathComponent &c, const T &value) const
bool visitTree(const Path &basePath, ChildrenVisitor visitor, VisitOptions options=VisitOption::Default, ChildrenVisitor openingVisitor=emptyChildrenVisitor, ChildrenVisitor closingVisitor=emptyChildrenVisitor, const FieldFilter &filter=FieldFilter::noFilter()) const
Visits recursively all the children of this item using the given visitors.
bool resolve(const Path &path, Visitor visitor, const ErrorHandler &errorHandler, const Path &fullPath=Path(), QList< Path > *visitedRefs=nullptr) const
DomItem path(const Path &p, const ErrorHandler &h=&defaultErrorHandler) const
DomItem containingFile() const
DomItem filterUp(function_ref< bool(DomType k, const DomItem &)> filter, FilterUpOptions options) const
DomItem methods() const
QString internalKindStr() const
DomItem scope(FilterUpOptions options=FilterUpOptions::ReturnOuter) const
DomItem enumerations() const
DomItem subOwnerItem(const PathEls::PathComponent &c, Owner o) const
bool visitLookup1(const QString &symbolName, function_ref< bool(const DomItem &)> visitor, LookupOptions=LookupOption::Normal, const ErrorHandler &h=nullptr, QSet< quintptr > *visited=nullptr, QList< Path > *visitedRefs=nullptr) const
DomItem child(index_type i) const
DomItem key(QStringView name) const
std::shared_ptr< T > ownerAs() const
DomItem get(const ErrorHandler &h=nullptr, QList< Path > *visitedRefs=nullptr) const
static ErrorGroups myErrors()
DomItem operator[](const char16_t *component) const
bool visitUp(function_ref< bool(const DomItem &)> visitor) const
Let the visitor visit the Dom Tree hierarchy of this DomItem.
bool invokeVisitorOnField(DirectVisitor visitor, FieldName f, T &obj) const
index_type indexes() const
bool hasAnnotations() const
bool iterateSubOwners(function_ref< bool(const DomItem &owner)> visitor) const
MutableDomItem makeCopy(CopyOption option=CopyOption::EnvConnected) const
DomItem refreshed() const
bool iterateErrors(function_ref< bool(const DomItem &, const ErrorMessage &)> visitor, bool iterate, Path inPath=Path()) const
DomItem pragmas() const
DomItem proceedToScope(const ErrorHandler &h=nullptr, QList< Path > *visitedRefs=nullptr) const
Dereference DomItems pointing to other DomItems.
QList< DomItem > values() const
DomItem universe() const
bool iterateDirectSubpaths(DirectVisitor v) const
DomItem container() const
void clearErrors(const ErrorGroups &groups=ErrorGroups({}), bool iterate=true) const
quintptr id() const
QList< QString > fields() const
bool writeOut(const QString &path, const LineWriterOptions &opt=LineWriterOptions(), FileWriter *fw=nullptr, WriteOutChecks extraChecks=WriteOutCheck::Default) const
static DomItem fromCode(const QString &code, DomType fileType=DomType::QmlFile)
Creates a new document with the given code.
QString idStr() const
DomItem(const std::shared_ptr< DomEnvironment > &)
FileWriter::Status dump(const QString &path, function_ref< bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter=noFilter, int indent=0, FileWriter *fw=nullptr) const
DomItem copy(const T &base) const
bool visitScopeChain(function_ref< bool(const DomItem &)> visitor, LookupOptions=LookupOption::Normal, const ErrorHandler &h=nullptr, QSet< quintptr > *visited=nullptr, QList< Path > *visitedRefs=nullptr) const
Let the visitor visit the QML scope hierarchy of this DomItem.
std::shared_ptr< DomTop > topPtr() const
QCborValue value() const
index_type size() const
QDateTime frozenAt() const
DomItem subListItem(const List &list) const
DomItem component(GoTo option=GoTo::Strict) const
DomItem subValueItem(const PathEls::PathComponent &c, const T &value) const
bool visitLookup(const QString &symbolName, function_ref< bool(const DomItem &)> visitor, LookupType type=LookupType::Symbol, LookupOptions=LookupOption::Normal, const ErrorHandler &errorHandler=nullptr, QSet< quintptr > *visited=nullptr, QList< Path > *visitedRefs=nullptr) const
void writeOutPre(OutWriter &lw) const
DomItem lookupFirst(const QString &symbolName, LookupType type=LookupType::Symbol, LookupOptions=LookupOption::Normal, const ErrorHandler &errorHandler=nullptr) const
bool visitIndexes(function_ref< bool(const DomItem &)> visitor) const
DomItem fileObject(GoTo option=GoTo::Strict) const
bool visitKeys(function_ref< bool(const QString &, const DomItem &)> visitor) const
bool invokeVisitorOnReferences(DirectVisitor visitor, FieldName f, const QList< Path > &paths) const
bool visitStaticTypePrototypeChains(function_ref< bool(const DomItem &)> visitor, VisitPrototypesOptions options=VisitPrototypesOption::Normal, const ErrorHandler &h=nullptr, QSet< quintptr > *visited=nullptr, QList< Path > *visitedRefs=nullptr) const
DomItem::visitStaticTypePrototypeChains.
QQmlJSScope::ConstPtr semanticScope() const
bool writeOutForFile(OutWriter &ow, WriteOutChecks extraChecks) const
bool commitToBase(const std::shared_ptr< DomEnvironment > &validPtr=nullptr) const
DomItem operator[](FieldName name) const
DomItem field(FieldName name) const
DomItem ids() const
DomItem copy(const Owner &owner, const Path &ownerPath) const
bool visitSubSymbolsNamed(const QString &name, function_ref< bool(const DomItem &)> visitor) const
DomItem wrap(const PathEls::PathComponent &c, const T &obj) const
QList< DomItem > lookup(const QString &symbolName, LookupType type=LookupType::Symbol, LookupOptions=LookupOption::Normal, const ErrorHandler &errorHandler=nullptr) const
DomItem subDataItem(const PathEls::PathComponent &c, const T &value) const
void dump(const Sink &, int indent=0, function_ref< bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter=noFilter) const
friend QMLDOM_EXPORT bool operator==(const DomItem &, const DomItem &)
QSet< QString > propertyInfoNames() const
static ErrorGroups myResolveErrors()
bool visitDirectAccessibleScopes(function_ref< bool(const DomItem &)> visitor, VisitPrototypesOptions options=VisitPrototypesOption::Normal, const ErrorHandler &h=nullptr, QSet< quintptr > *visited=nullptr, QList< Path > *visitedRefs=nullptr) const
DomItem environment() const
bool isCanonicalChild(const DomItem &child) const
index_type length() const
DomItem operator[](QStringView component) const
DomItem rootQmlObject(GoTo option=GoTo::Strict) const
DomItem directParent() const
DomItem annotations() const
Path canonicalPath() const
QStringList sortedKeys() const
void addError(ErrorMessage &&msg) const
DomItem containingObject() const
DomItem containingScriptExpression() const
QList< DomItem > getAll(const ErrorHandler &h=nullptr, QList< Path > *visitedRefs=nullptr) const
bool visitPrototypeChain(function_ref< bool(const DomItem &)> visitor, VisitPrototypesOptions options=VisitPrototypesOption::Normal, const ErrorHandler &h=nullptr, QSet< quintptr > *visited=nullptr, QList< Path > *visitedRefs=nullptr) const
DomKind domKind() const
bool isContainer() const
DomItem index(index_type) const
DomItem subReferencesItem(const PathEls::PathComponent &c, const QList< Path > &paths) const
DomItem subMapItem(const Map &map) const
bool visitLocalSymbolsNamed(const QString &name, function_ref< bool(const DomItem &)> visitor) const
bool isExternalItem() const
DomItem subReferenceItem(const PathEls::PathComponent &c, const Path &referencedObject) const
void dumpPtr(const Sink &sink) const
auto visitEl(F f) const
InternalKind internalKind() const
bool isOwningItem() const
DomItem owner() const
The owner of an element, for an qmlObject this is the containing qml file.
ErrorHandler errorHandler() const
DomItem operator[](const Path &path) const
static DomItem empty
QQmlJSScope::ConstPtr nearestSemanticScope() const
QString canonicalFilePath() const
void writeOut(OutWriter &lw) const
Path pathFromOwner() const
DomItem qmlObject(GoTo option=GoTo::Strict, FilterUpOptions options=FilterUpOptions::ReturnOuter) const
Returns the QmlObject that this belongs to.
DomItem subScriptElementWrapperItem(const ScriptElementVariant &obj) const
DomItem copy(const Owner &owner, const Path &ownerPath, const T &base) const
DomItem key(const QString &name) const
PropertyInfo propertyInfoWithName(const QString &name) const
QSet< QString > keys() const
QString name() const
DomItem children() const
bool invokeVisitorOnReference(DirectVisitor visitor, FieldName f, const Path &referencedObject) const
std::shared_ptr< QQmlJSResourceFileMapper > resourceFileMapper() const
DomItem propertyDefs() const
DomItem goToFile(const QString &filePath) const
QDateTime lastDataUpdateAt() const
DomItem(const std::shared_ptr< DomUniverse > &)
static ErrorGroup domErrorGroup
bool invokeVisitorOnLazyField(DirectVisitor visitor, FieldName f, F valueF) const
DomItem propertyInfos() const
void dump(const DomItem &, const Sink &s, int indent, function_ref< bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter) const override
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
static constexpr DomType kindValue
Path pathFromOwner() const override
Path canonicalPath(const DomItem &self) const override
const Empty & operator*() const
const Empty * operator->() const
DomType kind() const override
DomItem containingObject(const DomItem &self) const override
quintptr id() const override
const QList< QmlObject > & annotations() const &
const QList< EnumItem > & values() const &
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const override
void setAlias(const QString &aliasName)
void setAnnotations(const QList< QmlObject > &annotations)
EnumDecl(const QString &name=QString(), const QList< EnumItem > &values=QList< EnumItem >(), const Path &pathFromOwner=Path())
void writeOut(const DomItem &self, OutWriter &lw) const override
void updatePathFromOwner(const Path &newP) override
void setName(const QString &name)
Path addAnnotation(const QmlObject &child, QmlObject **cPtr=nullptr)
Path addValue(const EnumItem &value)
static constexpr DomType kindValue
void setValues(const QList< EnumItem > &values)
DomType kind() const override
EnumItem(const QString &name=QString(), int value=0, ValueKind valueKind=ValueKind::ImplicitValue)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
const RegionComments & comments() const
RegionComments & comments()
void writeOut(const DomItem &self, OutWriter &lw) const
static constexpr DomType kindValue
convenience macro creating a new ErrorGroup and registering its groupId as translatable string
Represents a set of tags grouping a set of related error messages.
Represents an error message connected to the dom.
static Export fromString(const Path &source, QStringView exp, const Path &typePath, const ErrorHandler &h)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
static FieldFilter noFilter()
Represents a Node of FileLocations tree.
QString logicalPath() const
QString canonicalPath() const
std::shared_ptr< DomEnvironment > environment() const
void setCanonicalPath(const QString &canonicalPath)
void setLogicalPath(const QString &logicalPath)
std::optional< InMemoryContents > content() const
static FileToLoad fromFileSystem(const std::weak_ptr< DomEnvironment > &environment, const QString &canonicalPath)
static FileToLoad fromMemory(const std::weak_ptr< DomEnvironment > &environment, const QString &path, const QString &data)
FileToLoad(const std::weak_ptr< DomEnvironment > &environment, const QString &canonicalPath, const QString &logicalPath, const std::optional< InMemoryContents > &content)
GlobalComponent(const Path &pathFromOwner=Path())
DomType kind() const override
static constexpr DomType kindValue
Path addAnnotation(const Path &selfPathFromOwner, const QmlObject &ann, QmlObject **aPtr=nullptr)
RegionComments comments
std::shared_ptr< ScriptExpression > value
static constexpr DomType kindValue
Id(const QString &idName=QString(), const Path &referredObject=Path())
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const
void updatePathFromOwner(const Path &pathFromOwner)
QList< QmlObject > annotations
Import(const QmlUri &uri=QmlUri(), Version version=Version(), const QString &importId=QString())
Import baseImport() const
friend bool operator==(const Import &i1, const Import &i2)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const
static QRegularExpression importRe()
void writeOut(const DomItem &self, OutWriter &ow) const
static Import fromFileString(const QString &importStr, const QString &importId=QString(), const ErrorHandler &handler=nullptr)
friend bool operator!=(const Import &i1, const Import &i2)
static Import fromUriString(const QString &importStr, Version v=Version(), const QString &importId=QString(), const ErrorHandler &handler=nullptr)
JsResource(const Path &pathFromOwner=Path())
DomType kind() const override
bool iterateDirectSubpaths(const DomItem &, DirectVisitor) const override
static constexpr DomType kindValue
quintptr id() const override
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor v) const override
void writeOut(const DomItem &self, OutWriter &ow, bool compact) const
static constexpr DomType kindValue
index_type indexes(const DomItem &) const override
virtual void copyTo(ListPBase *) const
virtual void moveTo(ListPBase *) const
void writeOut(const DomItem &self, OutWriter &ow) const override
DomType kind() const override
QList< const void * > m_pList
ListPBase(const Path &pathFromOwner, const QList< const void * > &pList, const QString &elType)
void moveTo(ListPBase *t) const override
ListPT(const Path &pathFromOwner, const QList< T * > &pList, const QString &elType=QString(), ListOptions options=ListOptions::Normal)
DomItem index(const DomItem &self, index_type index) const override
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor v) const override
void copyTo(ListPBase *t) const override
static constexpr DomType kindValue
static constexpr DomType kindValue
ListPBase & operator*()
ListPBase * operator->()
const ListPBase * operator->() const
ListP(const Path &pathFromOwner, const QList< T * > &pList, const QString &elType=QString(), ListOptions options=ListOptions::Normal)
const ListPBase & operator*() const
void writeOut(const DomItem &self, OutWriter &ow, bool compact) const
std::function< bool(const DomItem &, function_ref< bool(index_type, function_ref< DomItem()>)>)> IteratorFunction
List(const Path &pathFromOwner, const LookupFunction &lookup, const Length &length, const IteratorFunction &iterator, const QString &elType)
DomType kind() const override
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
const List & operator*() const
static List fromQList(const Path &pathFromOwner, const QList< T > &list, const std::function< DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper, ListOptions options=ListOptions::Normal)
static List fromQListRef(const Path &pathFromOwner, const QList< T > &list, const std::function< DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper, ListOptions options=ListOptions::Normal)
void dump(const DomItem &, const Sink &s, int indent, function_ref< bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)>) const override
std::function< DomItem(const DomItem &, index_type)> LookupFunction
void writeOut(const DomItem &self, OutWriter &ow) const override
quintptr id() const override
static constexpr DomType kindValue
const List * operator->() const
index_type indexes(const DomItem &self) const override
DomItem index(const DomItem &self, index_type index) const override
std::function< index_type(const DomItem &)> Length
std::function< DomItem(const DomItem &, QString)> LookupFunction
static Map fromMultiMapRef(const Path &pathFromOwner, const QMultiMap< QString, T > &mmap)
const Map * operator->() const
static Map fromFileRegionMap(const Path &pathFromOwner, const QMap< FileLocationRegion, T > &map)
Map(const Path &pathFromOwner, const LookupFunction &lookup, const Keys &keys, const QString &targetType)
const Map & operator*() const
static Map fromMapRef(const Path &pathFromOwner, const QMap< QString, T > &mmap, const std::function< DomItem(const DomItem &, const PathEls::PathComponent &, const T &)> &elWrapper)
static Map fromMultiMap(const Path &pathFromOwner, const QMultiMap< QString, T > &mmap)
std::function< QSet< QString >(const DomItem &)> Keys
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
QSet< QString > const keys(const DomItem &self) const override
quintptr id() const override
DomItem key(const DomItem &self, const QString &name) const override
static constexpr DomType kindValue
DomType kind() const override
std::shared_ptr< ScriptExpression > body
static constexpr DomType kindValue
QList< MethodParameter > parameters
std::shared_ptr< ScriptExpression > returnType
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
void writeOut(const DomItem &self, OutWriter &ow) const
void writePre(const DomItem &self, OutWriter &ow) const
QString signature(const DomItem &self) const
std::shared_ptr< ScriptExpression > defaultValue
void writeOut(const DomItem &self, OutWriter &ow) const
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
void writeOutSignal(const DomItem &self, OutWriter &ow) const
TypeAnnotationStyle typeAnnotationStyle
static constexpr DomType kindValue
std::shared_ptr< ScriptExpression > value
friend bool operator==(const ModuleAutoExport &i1, const ModuleAutoExport &i2)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
friend bool operator!=(const ModuleAutoExport &i1, const ModuleAutoExport &i2)
static constexpr DomType kindValue
std::shared_ptr< T > ownerAs() const
MutableDomItem addChild(QmlObject child)
friend bool operator==(const MutableDomItem &o1, const MutableDomItem &o2)
MutableDomItem addPropertyDef(const PropertyDefinition &propertyDef, AddOption option=AddOption::Overwrite)
bool commitToBase(const std::shared_ptr< DomEnvironment > &validEnvPtr=nullptr)
QString canonicalFilePath() const
MutableDomItem fileObject(GoTo option=GoTo::Strict)
MutableDomItem operator[](const char16_t *component)
FileWriter::Status dump(const QString &path, function_ref< bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter=noFilter, int indent=0, FileWriter *fw=nullptr)
MutableDomItem key(QStringView name)
MutableDomItem operator[](const QString &component)
MutableDomItem addMethod(const MethodInfo &functionDef, AddOption option=AddOption::Overwrite)
void dump(const Sink &s, int indent=0, function_ref< bool(const DomItem &, const PathEls::PathComponent &, const DomItem &)> filter=noFilter)
void writeOut(OutWriter &lw)
MutableDomItem(const DomItem &owner, const Path &pathFromOwner)
MutableDomItem containingObject()
void addError(ErrorMessage &&msg)
std::shared_ptr< DomTop > topPtr()
MutableDomItem key(const QString &name)
QSet< QString > const keys()
MutableDomItem qmlObject(GoTo option=GoTo::Strict, FilterUpOptions fOptions=FilterUpOptions::ReturnOuter)
QList< QString > const fields()
MutableDomItem component(GoTo option=GoTo::Strict)
MutableDomItem path(const Path &p)
MutableDomItem makeCopy(CopyOption option=CopyOption::EnvConnected)
MutableDomItem operator[](const Path &path)
MutableDomItem index(index_type i)
MutableDomItem child(index_type i)
MutableDomItem field(FieldName name)
MutableDomItem(const DomItem &item)
std::shared_ptr< OwningItem > owningItemPtr()
bool writeOut(const QString &path, const LineWriterOptions &opt=LineWriterOptions(), FileWriter *fw=nullptr)
friend bool operator!=(const MutableDomItem &o1, const MutableDomItem &o2)
MutableDomItem rootQmlObject(GoTo option=GoTo::Strict)
DomItem::CopyOption CopyOption
MutableDomItem operator[](QStringView component)
MutableDomItem addBinding(Binding binding, AddOption option=AddOption::Overwrite)
A DomItem that owns other DomItems and is managed through a shared pointer.
QDateTime createdAt() const
virtual bool iterateSubOwners(const DomItem &self, function_ref< bool(const DomItem &owner)> visitor)
virtual int revision() const
QBasicMutex * mutex() const
DomItem containingObject(const DomItem &self) const override
virtual std::shared_ptr< OwningItem > doCopy(const DomItem &self) const =0
std::shared_ptr< OwningItem > makeCopy(const DomItem &self) const
Path pathFromOwner() const override final
OwningItem(const OwningItem &&)=delete
bool iterateErrors(const DomItem &self, function_ref< bool(const DomItem &source, const ErrorMessage &msg)> visitor, const Path &inPath=Path())
virtual void addError(const DomItem &self, ErrorMessage &&msg)
QDateTime frozenAt() const
void addErrorLocal(ErrorMessage &&msg)
virtual QDateTime lastDataUpdateAt() const
void clearErrors(const ErrorGroups &groups=ErrorGroups({}))
Path canonicalPath(const DomItem &self) const override=0
QMultiMap< Path, ErrorMessage > localErrors() const
OwningItem & operator=(const OwningItem &&)=delete
OwningItem(int derivedFrom, const QDateTime &lastDataUpdateAt)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
virtual bool frozen() const
virtual void refreshedDataAt(QDateTime tNew)
OwningItem(int derivedFrom=0)
OwningItem(const OwningItem &o)
Represents an immutable JsonPath like path in the Qml code model (from a DomItem to another DomItem).
static Path fromField(FieldName name)
Source split() const
Splits the path at the last field or current component, or right after a root anchor.
PathEls::Kind Kind
static Path fromRoot(PathRoot r)
Path operator[](int i) const
Path withField(FieldName name) const
Path mid(int offset, int length) const
Path last() const
std::optional< FieldName > headField() const
Path withComponent(PathEls::PathComponent c) const
static Path fromCurrent(PathCurrent c)
Pragma(const QString &pragmaName=QString(), const QStringList &pragmaValues={})
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
void writeOut(const DomItem &self, OutWriter &ow) const
static constexpr DomType kindValue
static constexpr DomType kindValue
ScriptElementVariant m_nameIdentifiers
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
void writeOut(const DomItem &self, OutWriter &lw) const
ScriptElementVariant nameIdentifiers() const
void setNameIdentifiers(const ScriptElementVariant &name)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const
static constexpr DomType kindValue
QQmlDomAstCreatorBase(const MutableDomItem &qmlFile)
void endVisit(AST::UiProgram *) override
std::shared_ptr< DomEnvironment > currentEnvironment()
void throwRecursionDepthError() override
void endVisitHelper(AST::PatternElement *pe, const std::shared_ptr< ScriptElements::GenericScriptElement > &element)
void loadAnnotations(AST::UiObjectMember *el)
bool visit(AST::UiProgram *program) override
void enableLoadFileLazily(bool enable=true)
void enableScriptExpressions(bool enable=true)
virtual QQmlJSASTClassListToVisit void throwRecursionDepthError() override
QQmlDomAstCreatorWithQQmlJSScope(MutableDomItem &qmlFile, QQmlJSLogger *logger, QQmlJSImporter *importer)
QQmlJSScope::ConstPtr semanticScope() const
void setIds(const QMultiMap< QString, Id > &ids)
void setNextComponentPath(const Path &p)
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
QList< DomItem > subComponents(const DomItem &self) const
const QMultiMap< QString, Id > & ids() const &
void updatePathFromOwner(const Path &newPath) override
void setNameIdentifiers(const ScriptElementVariant &name)
static constexpr DomType kindValue
void writeOut(const DomItem &self, OutWriter &) const override
void setSemanticScope(const QQmlJSScope::ConstPtr &scope)
QmlComponent(const QString &name=QString())
QList< QString > subComponentsNames(const DomItem &self) const
Path addId(const Id &id, AddOption option=AddOption::Overwrite, Id **idPtr=nullptr)
ScriptElementVariant nameIdentifiers() const
DomType kind() const override
QList< QString > fields(const DomItem &) const override
void setNameIdentifiers(const ScriptElementVariant &name)
Path addChild(const QmlObject &child, QmlObject **cPtr=nullptr)
MutableDomItem addBinding(MutableDomItem &self, const Binding &binding, AddOption option)
void setMethods(const QMultiMap< QString, MethodInfo > &functionDefs)
void writeOutSortedEnumerations(const QList< DomItem > &descs, OutWriter &ow) const
void writeOut(const DomItem &self, OutWriter &ow, const QString &onTarget) const
DomItem field(const DomItem &self, FieldName name) const override
ScriptElementVariant nameIdentifiers() const
void setName(const QString &name)
QList< std::pair< SourceLocation, DomItem > > orderOfAttributes(const DomItem &self, const DomItem &component) const
bool iterateSubOwners(const DomItem &self, function_ref< bool(const DomItem &owner)> visitor) const
MutableDomItem addChild(MutableDomItem &self, const QmlObject &child)
void setAnnotations(const QList< QmlObject > &annotations)
const QMultiMap< QString, Binding > & bindings() const &
DomType kind() const override
void setNextScopePath(const Path &nextScopePath)
void updatePathFromOwner(const Path &newPath) override
void setChildren(const QList< QmlObject > &children)
Path addBinding(const Binding &binding, AddOption option, Binding **bPtr=nullptr)
Path addAnnotation(const QmlObject &annotation, QmlObject **aPtr=nullptr)
LocallyResolvedAlias resolveAlias(const DomItem &self, std::shared_ptr< ScriptExpression > accessSequence) const
void writeOutId(const DomItem &self, OutWriter &ow) const
QList< QmlObject > children() const
LocallyResolvedAlias resolveAlias(const DomItem &self, const QStringList &accessSequence) const
const QList< Path > & prototypePaths() const &
void writeOutSortedPropertyDefinition(const DomItem &self, OutWriter &ow, QSet< QString > &mergedDefBinding, const QStringList &keys) const
Path addPropertyDef(const PropertyDefinition &propertyDef, AddOption option, PropertyDefinition **pDef=nullptr)
void writeOutSortedAttributes(const DomItem &self, OutWriter &ow, const DomItem &component, const Attributes &attribs) const
const QMultiMap< QString, MethodInfo > & methods() const &
MutableDomItem addPropertyDef(MutableDomItem &self, const PropertyDefinition &propertyDef, AddOption option)
void setBindings(const QMultiMap< QString, Binding > &bindings)
QString localDefaultPropertyName() const
QString defaultPropertyName(const DomItem &self) const
void setPropertyDefs(const QMultiMap< QString, PropertyDefinition > &propertyDefs)
QmlObject(const Path &pathFromOwner=Path())
void writeOutAttributes(OutWriter &ow, const Attributes &attribs, const QString &code) const
MutableDomItem addMethod(MutableDomItem &self, const MethodInfo &functionDef, AddOption option)
void setIdStr(const QString &id)
QQmlJSScope::ConstPtr semanticScope() const
const QMultiMap< QString, PropertyDefinition > & propertyDefs() const &
void writeOut(const DomItem &self, OutWriter &lw) const override
Path addPrototypePath(const Path &prototypePath)
void setSemanticScope(const QQmlJSScope::ConstPtr &scope)
void setDefaultPropertyName(const QString &name)
void setPrototypePaths(const QList< Path > &prototypePaths)
QList< QmlObject > annotations() const
QList< QString > fields() const
QList< std::pair< SourceLocation, DomItem > > Attributes
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
Path addMethod(const MethodInfo &functionDef, AddOption option, MethodInfo **mPtr=nullptr)
bool iterateBaseDirectSubpaths(const DomItem &self, DirectVisitor) const
static QmlUri fromString(const QString &importStr)
QString absoluteLocalPath(const QString &basePath=QString()) const
QString directoryString() const
static QmlUri fromUriString(const QString &importStr)
QString localPath() const
QString moduleUri() const
friend bool operator==(const QmlUri &i1, const QmlUri &i2)
static QmlUri fromDirectoryString(const QString &importStr)
friend bool operator!=(const QmlUri &i1, const QmlUri &i2)
QList< QString > fields(const DomItem &self) const override
quintptr id() const override
static constexpr DomType kindValue
const Reference & operator*() const
DomItem index(const DomItem &, index_type) const override
DomItem get(const DomItem &self, const ErrorHandler &h=nullptr, QList< Path > *visitedRefs=nullptr) const
DomItem field(const DomItem &self, FieldName name) const override
QList< DomItem > getAll(const DomItem &self, const ErrorHandler &h=nullptr, QList< Path > *visitedRefs=nullptr) const
index_type indexes(const DomItem &) const override
DomType kind() const override
QSet< QString > const keys(const DomItem &) const override
const Reference * operator->() const
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const override
Reference(const Path &referredObject=Path(), const Path &pathFromOwner=Path(), const SourceLocation &loc=SourceLocation())
DomItem key(const DomItem &, const QString &) const override
Keeps the comments associated with a DomItem.
const DomBase & operator*() const
const DomBase * operator->() const
static constexpr DomType kindValue
ScriptElementVariant element() const
ScriptElementDomWrapper(const ScriptElementVariant &element)
Use this to contain any script element.
void visitConst(F &&visitor) const
std::optional< ScriptElementT > data()
ScriptElement::PointerType< ScriptElement > base() const
Returns a pointer to the virtual base for virtual method calls.
static ScriptElementVariant fromElement(const T &element)
void setData(const ScriptElementT &data)
void replaceKindForGenericChildren(DomType oldType, DomType newType)
QStringView loc2Str(const SourceLocation &) const
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const override
DomType kind() const override
std::shared_ptr< AstComments > astComments() const
std::shared_ptr< QQmlJS::Engine > engine() const
ScriptExpression(const QString &code, ExpressionType expressionType)
static constexpr DomType kindValue
SourceLocation locationToLocal(const SourceLocation &x) const
std::shared_ptr< ScriptExpression > makeCopy(const DomItem &self) const
ScriptExpression(const ScriptExpression &e)
ExpressionType expressionType() const
void writeOut(const DomItem &self, OutWriter &lw) const override
SourceLocation globalLocation(const DomItem &self) const
ScriptExpression(QStringView code, const std::shared_ptr< QQmlJS::Engine > &engine, AST::Node *ast, const std::shared_ptr< AstComments > &comments, ExpressionType expressionType, const SourceLocation &localOffset=SourceLocation())
void setScriptElement(const ScriptElementVariant &p)
std::shared_ptr< OwningItem > doCopy(const DomItem &) const override
ScriptElementVariant scriptElement()
void astDumper(const Sink &s, AstDumperOptions options) const
SourceLocation localOffset() const
Path canonicalPath(const DomItem &self) const override
static constexpr DomType kindValue
bool iterateDirectSubpaths(const DomItem &, DirectVisitor) const override
SimpleObjectWrapBase(const Path &pathFromOwner, const QVariant &value, quintptr idValue, DomType kind=kindValue, SimpleWrapOptions options=SimpleWrapOption::None)
virtual void moveTo(SimpleObjectWrapBase *) const
DomKind domKind() const final override
virtual void copyTo(SimpleObjectWrapBase *) const
DomType kind() const final override
quintptr id() const final override
SimpleObjectWrapT(const Path &pathFromOwner, const QVariant &v, quintptr idValue, SimpleWrapOptions o)
void moveTo(SimpleObjectWrapBase *target) const override
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const override
static constexpr DomType kindValue
void writeOut(const DomItem &self, OutWriter &lw) const override
void copyTo(SimpleObjectWrapBase *target) const override
const SimpleObjectWrapBase & operator*() const
SimpleObjectWrapBase & operator*()
static SimpleObjectWrap fromObjectRef(const Path &pathFromOwner, T &value)
const SimpleObjectWrapBase * operator->() const
static constexpr DomType kindValue
SimpleObjectWrapBase * operator->()
bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const
QString majorString() const
static constexpr DomType kindValue
Version(qint32 majorVersion=Undefined, qint32 minorVersion=Undefined)
QString minorString() const
int compare(Version o) const
static Version fromString(QStringView v)
QString stringValue() const
static constexpr qint32 Undefined
static constexpr qint32 Latest
QString majorSymbolicString() const
Provides entities to maintain mappings between elements and their location in a file.
void addRegion(const Tree &fLoc, FileLocationRegion region, SourceLocation loc)
Tree ensure(const Tree &base, const Path &basePath)
Path lookupTypePath(const QString &name)
Path loadInfoPath(const Path &el)
Path moduleScopePath(const QString &uri, const ErrorHandler &errorHandler=nullptr)
Path jsFilePath(const QString &path)
Path lookupCppTypePath(const QString &name)
Path qmlFileInfoPath(const QString &canonicalFilePath)
Path moduleIndexPath(const QString &uri, int majorVersion, const ErrorHandler &errorHandler=nullptr)
Path qmlFilePath(const QString &canonicalFilePath)
Path moduleScopePath(const QString &uri, const QString &version, const ErrorHandler &errorHandler=nullptr)
Path lookupSymbolPath(const QString &name)
Path jsFileInfoPath(const QString &path)
Path lookupPropertyPath(const QString &name)
Path moduleScopePath(const QString &uri, Version version, const ErrorHandler &errorHandler=nullptr)
Path qmlFileObjectPath(const QString &canonicalFilePath)
bool operator>(Version v1, Version v2)
QMLDOM_EXPORT bool domTypeIsContainer(DomType k)
bool operator==(Version v1, Version v2)
constexpr bool domTypeIsOwningItem(DomType)
Path appendUpdatableElementInQList(const Path &listPathFromOwner, QList< T > &list, const T &value, T **vPtr=nullptr)
constexpr bool domTypeIsValueWrap(DomType k)
Path insertUpdatableElementInMultiMap(const Path &mapPathFromOwner, QMultiMap< K, T > &mmap, K key, const T &value, AddOption option=AddOption::KeepExisting, T **valuePtr=nullptr)
bool noFilter(const DomItem &, const PathEls::PathComponent &, const DomItem &)
QMLDOM_EXPORT QMap< DomKind, QString > domKindToStringMap()
QMLDOM_EXPORT QDebug operator<<(QDebug debug, const DomItem &c)
std::shared_ptr< ExternalOwningItem > getFileItemOwner(const DomItem &fileItem)
DomKind kind2domKind(DomType k)
static DomItem keyMultiMapHelper(const DomItem &self, const QString &key, const QMultiMap< QString, T > &mmap)
bool operator!=(const DomItem &o1, const DomItem &o2)
QMLDOM_EXPORT bool domTypeIsExternalItem(DomType k)
constexpr bool domTypeIsUnattachedOwningItem(DomType)
QMLDOM_EXPORT QMap< DomType, QString > domTypeToStringMap()
QMLDOM_EXPORT bool domTypeIsScope(DomType k)
QMLDOM_EXPORT QDebug operator<<(QDebug debug, const MutableDomItem &c)
std::disjunction< std::is_same< U, V >... > IsInList
QMLDOM_EXPORT QString domTypeToString(DomType k)
auto writeOutWrap(const T &, const DomItem &, OutWriter &, rank< 0 >) -> void
constexpr bool domTypeIsScriptElement(DomType)
QMLDOM_EXPORT QString domKindToString(DomKind k)
constexpr bool domTypeIsDomElement(DomType)
void updatePathFromOwnerQList(QList< T > &list, const Path &newPath)
constexpr bool domTypeCanBeInline(DomType k)
bool operator<(Version v1, Version v2)
bool operator<=(Version v1, Version v2)
auto writeOutWrap(const T &t, const DomItem &self, OutWriter &lw) -> void
std::variant< std::monostate, std::shared_ptr< ExternalItemInfoBase >, std::shared_ptr< ExternalItemPairBase >, std::shared_ptr< JsFile >, std::shared_ptr< QmlFile >, std::shared_ptr< ScriptExpression >, std::shared_ptr< AstComments >, std::shared_ptr< LoadInfo >, std::shared_ptr< FileLocations::Node >, std::shared_ptr< DomEnvironment >, std::shared_ptr< DomUniverse > > OwnerT
auto writeOutWrap(const T &t, const DomItem &self, OutWriter &lw, rank< 1 >) -> decltype(t.writeOut(self, lw))
std::variant< ConstantData, Empty, List, ListP, Map, Reference, ScriptElementDomWrapper, SimpleObjectWrap, const AstComments *, const FileLocations::Node *, const DomEnvironment *, const DomUniverse *, const EnumDecl *, const ExternalItemInfoBase *, const ExternalItemPairBase *, const GlobalComponent *, const JsFile *, const JsResource *, const LoadInfo *, const QmlComponent *, const QmlFile *, const QmlObject *, const ScriptExpression * > ElementT
bool operator!=(Version v1, Version v2)
bool operator>=(Version v1, Version v2)
bool visitWithCustomListIteration(T *t, AST::Visitor *visitor)
std::function< void(const ErrorMessage &)> ErrorHandler
QMLDOM_EXPORT bool domTypeIsTopItem(DomType k)
QMLDOM_EXPORT void defaultErrorHandler(const ErrorMessage &)
Calls the default error handler (by default errorToQDebug).
void updatePathFromOwnerMultiMap(QMultiMap< K, T > &mmap, const Path &newPath)
constexpr bool domTypeIsObjWrap(DomType k)
bool operator==(const Path &lhs, const Path &rhs)
bool emptyChildrenVisitor(Path, const DomItem &, bool)
static ErrorGroups importErrors
std::variant< std::monostate, std::shared_ptr< DomEnvironment >, std::shared_ptr< DomUniverse > > TopT
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)
#define QMLDOM_EXPORT
#define Q_SCRIPTELEMENT_EXIT_IF(check)
#define Q_SCRIPTELEMENT_DISABLE()
#define NewErrorGroup(name)
A common base class for all the script elements.
void setSemanticScope(const QQmlJSScope::ConstPtr &scope)
virtual void createFileLocations(const std::shared_ptr< FileLocations::Node > &fileLocationOfOwner)=0
std::shared_ptr< T > PointerType
QQmlJSScope::ConstPtr semanticScope()
SubclassStorage & operator=(const SubclassStorage &o)
SubclassStorage(const SubclassStorage &&o)
SubclassStorage(const SubclassStorage &o)