1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qcommonsequencetypes_p.h"
43 #include "qexceptiterator_p.h"
44 #include "qgenericsequencetype_p.h"
45 #include "qintersectiterator_p.h"
46 #include "qitemmappingiterator_p.h"
47 #include "qnodesort_p.h"
48 #include "qunioniterator_p.h"
50 #include "qcombinenodes_p.h"
54 using namespace QPatternist;
56 CombineNodes::CombineNodes(const Expression::Ptr &operand1,
58 const Expression::Ptr &operand2) : PairContainer(operand1, operand2),
61 Q_ASSERT(op == Union ||
66 Item::Iterator::Ptr CombineNodes::evaluateSequence(const DynamicContext::Ptr &context) const
68 const Item::Iterator::Ptr op1(m_operand1->evaluateSequence(context));
69 const Item::Iterator::Ptr op2(m_operand2->evaluateSequence(context));
74 return Item::Iterator::Ptr(new IntersectIterator(op1, op2));
76 return Item::Iterator::Ptr(new ExceptIterator(op1, op2));
79 Q_ASSERT(m_operator == Union);
80 return Item::Iterator::Ptr(new UnionIterator(op1, op2));
85 Item CombineNodes::evaluateSingleton(const DynamicContext::Ptr &context) const
87 return evaluateSequence(context)->next();
90 Expression::Ptr CombineNodes::typeCheck(const StaticContext::Ptr &context,
91 const SequenceType::Ptr &reqType)
94 const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
96 m_operand1 = NodeSortExpression::wrapAround(m_operand1, context);
97 m_operand2 = NodeSortExpression::wrapAround(m_operand2, context);
102 bool CombineNodes::evaluateEBV(const DynamicContext::Ptr &context) const
104 /* If it's the union operator, we can possibly avoid
105 * evaluating the second operand. */
107 if(m_operator == Union)
109 return m_operand1->evaluateEBV(context) ||
110 m_operand2->evaluateEBV(context);
113 return PairContainer::evaluateEBV(context);
116 QString CombineNodes::displayName(const Operator op)
121 return QLatin1String("intersect");
123 return QLatin1String("except");
126 Q_ASSERT(op == Union);
127 return QLatin1String("union");
132 SequenceType::Ptr CombineNodes::staticType() const
134 const SequenceType::Ptr t1(m_operand1->staticType());
135 const SequenceType::Ptr t2(m_operand2->staticType());
139 /* Optimization: the cardinality can be better inferred for
140 * Intersect and Except, although it's not trivial code. */
141 if(m_operator == Union)
142 card = t1->cardinality() | t2->cardinality();
144 card = Cardinality::zeroOrMore();
146 return makeGenericSequenceType(t1->itemType() | t2->itemType(), card);
149 SequenceType::List CombineNodes::expectedOperandTypes() const
151 SequenceType::List result;
152 result.append(CommonSequenceTypes::ZeroOrMoreNodes);
153 result.append(CommonSequenceTypes::ZeroOrMoreNodes);
157 CombineNodes::Operator CombineNodes::operatorID() const
162 ExpressionVisitorResult::Ptr CombineNodes::accept(const ExpressionVisitor::Ptr &visitor) const
164 return visitor->visit(this);
167 Expression::ID CombineNodes::id() const
169 return IDCombineNodes;