1// Copyright (C) 2021 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 QMLDOMFILELOCATIONS_P_H
5#define QMLDOMFILELOCATIONS_P_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 "qqmldomitem_p.h"
20
21#include <memory>
22
23QT_BEGIN_NAMESPACE
24
25namespace QQmlJS {
26namespace Dom {
27namespace FileLocations {
28
29struct Info
30{
31 constexpr static DomType kindValue = DomType::FileLocationsInfo;
32 // mainly used for debugging, for example dumping qmlFile
33 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor) const;
34
35 SourceLocation fullRegion;
36 QMap<FileLocationRegion, SourceLocation> regions;
37};
38
39using Tree = std::shared_ptr<Node>;
40Tree createTree(const Path &basePath);
41Tree ensure(const Tree &base, const Path &basePath);
42Tree find(const Tree &self, const Path &p);
43
44bool visitTree(const Tree &base, function_ref<bool(const Path &, const Tree &)> visitor,
45 const Path &basePath = Path());
46
47// TODO move to some testing utils
48QString canonicalPathForTesting(const Tree &base);
49
50// returns the path looked up and the found tree when looking for the info attached to item
51Tree treeOf(const DomItem &);
52
53// beware of shrinking and reassigning regions (QTBUG-131288)
54void addRegion(const Tree &fLoc, FileLocationRegion region, SourceLocation loc);
55QQmlJS::SourceLocation region(const Tree &fLoc, FileLocationRegion region);
56
57class QMLDOM_EXPORT Node : public OwningItem, public std::enable_shared_from_this<Node>
58{
59public:
60 constexpr static DomType kindValue = DomType::FileLocationsNode;
61 using Ptr = std::shared_ptr<Node>;
62
63 DomType kind() const override { return kindValue; }
64 Path canonicalPath(const DomItem &self) const override { return self.m_ownerPath; }
65 // mainly used for debugging, for example dumping qmlFile
66 bool iterateDirectSubpaths(const DomItem &self, DirectVisitor visitor) const override;
67
68 Node::Ptr makeCopy(const DomItem &self) const
69 {
70 return std::static_pointer_cast<Node>(r: doCopy(self));
71 }
72
73 static Ptr instantiate(const Ptr &parent = nullptr, const Path &p = Path());
74
75 Path path() const { return m_path; }
76 Ptr parent() const { return m_parent.lock(); }
77 QMap<Path, Ptr> subItems() const { return m_subItems; }
78 FileLocations::Info &info() { return m_info; }
79
80 void setPath(const Path &p) { m_path = p; }
81 Ptr insertOrReturnChildAt(const Path &path);
82
83private:
84 // Because of the current implementation (weak_ptr to the parent, extensive use of shared_ptr-s)
85 // it's safer to prohibit construction on the stack or non-shared heap.
86 // Otherwise there is a high chance of misuse leading to double free
87 Node(const Ptr &parent = nullptr, const Path &p = Path()) : m_path(p), m_parent(parent) { }
88 Node(const Node &o) = default;
89
90 std::shared_ptr<OwningItem> doCopy(const DomItem &) const override;
91
92private:
93 Path m_path;
94 std::weak_ptr<Node> m_parent;
95 QMap<Path, Ptr> m_subItems;
96 FileLocations::Info m_info;
97};
98
99} // namespace FileLocations
100} // end namespace Dom
101} // end namespace QQmlJS
102
103QT_END_NAMESPACE
104#endif // QMLDOMFILELOCATIONS_P_H
105

source code of qtdeclarative/src/qmldom/qqmldomfilelocations_p.h