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
inlinecomponentutils_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// Qt-Security score:significant
4#ifndef INLINECOMPONENTUTILS_P_H
5#define INLINECOMPONENTUTILS_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 <private/qqmlmetatype_p.h>
19#include <private/qv4compileddata_p.h>
20#include <private/qv4resolvedtypereference_p.h>
21
22QT_BEGIN_NAMESPACE
23
24namespace icutils {
25struct Node {
26private:
31 quint32_le_bitfield_union<IndexField, TemporaryMarkField, PermanentMarkField> m_data;
32
33public:
34 Node() = default;
35 Node(const Node &) = default;
36 Node(Node &&) = default;
37 Node& operator=(Node const &) = default;
38 Node& operator=(Node &&) = default;
39 bool operator==(Node const &other) const {return m_data.data() == other.m_data.data(); }
40
41 Node(IndexType s) : m_data(QSpecialIntegerBitfieldZero) { m_data.set<IndexField>(s); }
42
43 bool hasPermanentMark() const { return m_data.get<PermanentMarkField>(); }
44 bool hasTemporaryMark() const { return m_data.get<TemporaryMarkField>(); }
45
47 {
48 m_data.set<TemporaryMarkField>(0);
49 m_data.set<PermanentMarkField>(1);
50 }
51
53 {
54 m_data.set<TemporaryMarkField>(1);
55 }
56
57 IndexType index() const { return m_data.get<IndexField>(); }
58};
59
62
63inline bool containedInSameType(const QQmlType &a, const QQmlType &b)
64{
65 return QQmlMetaType::equalBaseUrls(a.sourceUrl(), b.sourceUrl());
66}
67
68template<typename ObjectContainer, typename InlineComponent>
69void fillAdjacencyListForInlineComponents(ObjectContainer *objectContainer,
70 AdjacencyList &adjacencyList, NodeList &nodes,
71 const std::vector<InlineComponent> &allICs)
72{
73 using CompiledObject = typename ObjectContainer::CompiledObject;
74 // add an edge from A to B if A and B are inline components with the same containing type
75 // and A inherits from B (ignore indirect chains through external types for now)
76 // or if A instantiates B
77 for (typename std::vector<InlineComponent>::size_type i = 0; i < allICs.size(); ++i) {
78 const auto& ic = allICs[i];
79 const CompiledObject *obj = objectContainer->objectAt(ic.objectIndex);
80 QV4::ResolvedTypeReference *currentICTypeRef = objectContainer->resolvedType(ic.nameIndex);
81 auto createEdgeFromTypeRef = [&](QV4::ResolvedTypeReference *targetTypeRef) {
82 if (!targetTypeRef)
83 return;
84
85 const QQmlType targetType = targetTypeRef->type();
86 if (!targetType.isInlineComponent()
87 || !containedInSameType(targetType, currentICTypeRef->type())) {
88 return;
89 }
90
91 const auto icIt =
92 std::find_if(allICs.cbegin(), allICs.cend(),
93 [&](const QV4::CompiledData::InlineComponent &icSearched) {
94 return objectContainer->stringAt(icSearched.nameIndex)
95 == targetType.elementName();
96 });
97 Q_ASSERT(icIt != allICs.cend());
98
99 adjacencyList[std::distance(allICs.cbegin(), icIt)].push_back(&nodes[i]);
100 };
101 if (obj->inheritedTypeNameIndex != 0) {
102 QV4::ResolvedTypeReference *parentTypeRef = objectContainer->resolvedType(obj->inheritedTypeNameIndex);
103 createEdgeFromTypeRef(parentTypeRef);
104
105 }
106 auto referencedInICObjectIndex = ic.objectIndex + 1;
107 while (int(referencedInICObjectIndex) < objectContainer->objectCount()) {
108 auto potentiallyReferencedInICObject = objectContainer->objectAt(referencedInICObjectIndex);
109 bool stillInIC
110 = !potentiallyReferencedInICObject->hasFlag(
111 QV4::CompiledData::Object::IsInlineComponentRoot)
112 && potentiallyReferencedInICObject->hasFlag(
113 QV4::CompiledData::Object::IsPartOfInlineComponent);
114 if (!stillInIC)
115 break;
116 createEdgeFromTypeRef(objectContainer->resolvedType(potentiallyReferencedInICObject->inheritedTypeNameIndex));
117 ++referencedInICObjectIndex;
118 }
119 }
120};
121
122inline void topoVisit(Node *node, AdjacencyList &adjacencyList, bool &hasCycle,
123 NodeList &nodesSorted)
124{
125 if (node->hasPermanentMark())
126 return;
127 if (node->hasTemporaryMark()) {
128 hasCycle = true;
129 return;
130 }
132
133 auto const &edges = adjacencyList[node->index()];
134 for (auto edgeTarget =edges.begin(); edgeTarget != edges.end(); ++edgeTarget) {
135 topoVisit(*edgeTarget, adjacencyList, hasCycle, nodesSorted);
136 }
137
139 nodesSorted.push_back(*node);
140};
141
142// Use DFS based topological sorting (https://en.wikipedia.org/wiki/Topological_sorting)
143inline NodeList topoSort(NodeList &nodes, AdjacencyList &adjacencyList, bool &hasCycle)
144{
145 NodeList nodesSorted;
146 nodesSorted.reserve(nodes.size());
147
148 hasCycle = false;
149 auto currentNodeIt = std::find_if(nodes.begin(), nodes.end(), [](const Node& node) {
150 return !node.hasPermanentMark();
151 });
152 // Do a topological sort of all inline components
153 // afterwards, nodesSorted contains the nodes for the inline components in reverse topological order
154 while (currentNodeIt != nodes.end() && !hasCycle) {
155 Node& currentNode = *currentNodeIt;
156 topoVisit(&currentNode, adjacencyList, hasCycle, nodesSorted);
157 currentNodeIt = std::find_if(nodes.begin(), nodes.end(), [](const Node& node) {
158 return !node.hasPermanentMark();
159 });
160 }
161 return nodesSorted;
162}
163}
164
165QT_END_NAMESPACE
166
167#endif // INLINECOMPONENTUTILS_P_H
NodeList topoSort(NodeList &nodes, AdjacencyList &adjacencyList, bool &hasCycle)
void topoVisit(Node *node, AdjacencyList &adjacencyList, bool &hasCycle, NodeList &nodesSorted)
void fillAdjacencyListForInlineComponents(ObjectContainer *objectContainer, AdjacencyList &adjacencyList, NodeList &nodes, const std::vector< InlineComponent > &allICs)
bool containedInSameType(const QQmlType &a, const QQmlType &b)
Node(Node &&)=default
Node(const Node &)=default
Node & operator=(Node const &)=default
Node()=default
IndexType index() const
bool hasTemporaryMark() const
bool operator==(Node const &other) const
bool hasPermanentMark() const
Node & operator=(Node &&)=default