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 const auto targetType = targetTypeRef->type();
84 if (targetType.isInlineComponentType()
85 && containedInSameType(targetType, currentICTypeRef->type())) {
86 auto icIt = std::find_if(allICs.cbegin(), allICs.cend(), [&](const QV4::CompiledData::InlineComponent &icSearched){
87 return objectContainer->stringAt(icSearched.nameIndex)
88 == targetType.elementName();
89 });
90 Q_ASSERT(icIt != allICs.cend());
91 Node& target = nodes[i];
92 adjacencyList[std::distance(allICs.cbegin(), icIt)].push_back(&target);
93 }
94 }
95 };
96 if (obj->inheritedTypeNameIndex != 0) {
97 QV4::ResolvedTypeReference *parentTypeRef = objectContainer->resolvedType(obj->inheritedTypeNameIndex);
98 createEdgeFromTypeRef(parentTypeRef);
99
100 }
101 auto referencedInICObjectIndex = ic.objectIndex + 1;
102 while (int(referencedInICObjectIndex) < objectContainer->objectCount()) {
103 auto potentiallyReferencedInICObject = objectContainer->objectAt(referencedInICObjectIndex);
104 bool stillInIC
105 = !potentiallyReferencedInICObject->hasFlag(
106 QV4::CompiledData::Object::IsInlineComponentRoot)
107 && potentiallyReferencedInICObject->hasFlag(
108 QV4::CompiledData::Object::IsPartOfInlineComponent);
109 if (!stillInIC)
110 break;
111 createEdgeFromTypeRef(objectContainer->resolvedType(potentiallyReferencedInICObject->inheritedTypeNameIndex));
112 ++referencedInICObjectIndex;
113 }
114 }
115};
116
117inline void topoVisit(Node *node, AdjacencyList &adjacencyList, bool &hasCycle,
118 NodeList &nodesSorted)
119{
120 if (node->hasPermanentMark())
121 return;
122 if (node->hasTemporaryMark()) {
123 hasCycle = true;
124 return;
125 }
127
128 auto const &edges = adjacencyList[node->index()];
129 for (auto edgeTarget =edges.begin(); edgeTarget != edges.end(); ++edgeTarget) {
130 topoVisit(*edgeTarget, adjacencyList, hasCycle, nodesSorted);
131 }
132
134 nodesSorted.push_back(*node);
135};
136
137// Use DFS based topological sorting (https://en.wikipedia.org/wiki/Topological_sorting)
138inline NodeList topoSort(NodeList &nodes, AdjacencyList &adjacencyList, bool &hasCycle)
139{
140 NodeList nodesSorted;
141 nodesSorted.reserve(nodes.size());
142
143 hasCycle = false;
144 auto currentNodeIt = std::find_if(nodes.begin(), nodes.end(), [](const Node& node) {
145 return !node.hasPermanentMark();
146 });
147 // Do a topological sort of all inline components
148 // afterwards, nodesSorted contains the nodes for the inline components in reverse topological order
149 while (currentNodeIt != nodes.end() && !hasCycle) {
150 Node& currentNode = *currentNodeIt;
151 topoVisit(&currentNode, adjacencyList, hasCycle, nodesSorted);
152 currentNodeIt = std::find_if(nodes.begin(), nodes.end(), [](const Node& node) {
153 return !node.hasPermanentMark();
154 });
155 }
156 return nodesSorted;
157}
158}
159
160QT_END_NAMESPACE
161
162#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