1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qcommonsequencetypes_p.h"
41#include "qexceptiterator_p.h"
42#include "qgenericsequencetype_p.h"
43#include "qintersectiterator_p.h"
44#include "qitemmappingiterator_p.h"
45#include "qnodesort_p.h"
46#include "qunioniterator_p.h"
47
48#include "qcombinenodes_p.h"
49
50QT_BEGIN_NAMESPACE
51
52using namespace QPatternist;
53
54CombineNodes::CombineNodes(const Expression::Ptr &operand1,
55 const Operator op,
56 const Expression::Ptr &operand2) : PairContainer(operand1, operand2),
57 m_operator(op)
58{
59 Q_ASSERT(op == Union ||
60 op == Except ||
61 op == Intersect);
62}
63
64Item::Iterator::Ptr CombineNodes::evaluateSequence(const DynamicContext::Ptr &context) const
65{
66 const Item::Iterator::Ptr op1(m_operand1->evaluateSequence(context));
67 const Item::Iterator::Ptr op2(m_operand2->evaluateSequence(context));
68
69 switch(m_operator)
70 {
71 case Intersect:
72 return Item::Iterator::Ptr(new IntersectIterator(op1, op2));
73 case Except:
74 return Item::Iterator::Ptr(new ExceptIterator(op1, op2));
75 default:
76 {
77 Q_ASSERT(m_operator == Union);
78 return Item::Iterator::Ptr(new UnionIterator(op1, op2));
79 }
80 }
81}
82
83Item CombineNodes::evaluateSingleton(const DynamicContext::Ptr &context) const
84{
85 return evaluateSequence(context)->next();
86}
87
88Expression::Ptr CombineNodes::typeCheck(const StaticContext::Ptr &context,
89 const SequenceType::Ptr &reqType)
90{
91
92 const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
93
94 m_operand1 = NodeSortExpression::wrapAround(operand: m_operand1, context);
95 m_operand2 = NodeSortExpression::wrapAround(operand: m_operand2, context);
96
97 return me;
98}
99
100bool CombineNodes::evaluateEBV(const DynamicContext::Ptr &context) const
101{
102 /* If it's the union operator, we can possibly avoid
103 * evaluating the second operand. */
104
105 if(m_operator == Union)
106 {
107 return m_operand1->evaluateEBV(context) ||
108 m_operand2->evaluateEBV(context);
109 }
110 else
111 return PairContainer::evaluateEBV(context);
112}
113
114QString CombineNodes::displayName(const Operator op)
115{
116 switch(op)
117 {
118 case Intersect:
119 return QLatin1String("intersect");
120 case Except:
121 return QLatin1String("except");
122 default:
123 {
124 Q_ASSERT(op == Union);
125 return QLatin1String("union");
126 }
127 }
128}
129
130SequenceType::Ptr CombineNodes::staticType() const
131{
132 const SequenceType::Ptr t1(m_operand1->staticType());
133 const SequenceType::Ptr t2(m_operand2->staticType());
134
135 Cardinality card;
136
137 /* Optimization: the cardinality can be better inferred for
138 * Intersect and Except, although it's not trivial code. */
139 if(m_operator == Union)
140 card = t1->cardinality() | t2->cardinality();
141 else /* Except. */
142 card = Cardinality::zeroOrMore();
143
144 return makeGenericSequenceType(itemType: t1->itemType() | t2->itemType(), cardinality: card);
145}
146
147SequenceType::List CombineNodes::expectedOperandTypes() const
148{
149 SequenceType::List result;
150 result.append(t: CommonSequenceTypes::ZeroOrMoreNodes);
151 result.append(t: CommonSequenceTypes::ZeroOrMoreNodes);
152 return result;
153}
154
155CombineNodes::Operator CombineNodes::operatorID() const
156{
157 return m_operator;
158}
159
160ExpressionVisitorResult::Ptr CombineNodes::accept(const ExpressionVisitor::Ptr &visitor) const
161{
162 return visitor->visit(this);
163}
164
165Expression::ID CombineNodes::id() const
166{
167 return IDCombineNodes;
168}
169
170QT_END_NAMESPACE
171

source code of qtxmlpatterns/src/xmlpatterns/expr/qcombinenodes.cpp