1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qbuiltintypes_p.h"
43 #include "qcommonsequencetypes_p.h"
44 #include "qcommonvalues_p.h"
45 #include "qgenericsequencetype_p.h"
46 #include "qinteger_p.h"
47 #include "qliteral_p.h"
48 #include "qrangeiterator_p.h"
50 #include "qrangeexpression_p.h"
54 using namespace QPatternist;
56 RangeExpression::RangeExpression(const Expression::Ptr &operand1,
57 const Expression::Ptr &operand2) : PairContainer(operand1, operand2)
61 Item::Iterator::Ptr RangeExpression::evaluateSequence(const DynamicContext::Ptr &context) const
63 const Item s(m_operand1->evaluateSingleton(context));
66 return CommonValues::emptyIterator;
68 const Item e(m_operand2->evaluateSingleton(context));
70 return CommonValues::emptyIterator;
72 const xsInteger start = s.as<Numeric>()->toInteger();
73 const xsInteger end = e.as<Numeric>()->toInteger();
76 return CommonValues::emptyIterator;
78 return makeSingletonIterator(s);
80 return Item::Iterator::Ptr(new RangeIterator(start, RangeIterator::Forward, end));
83 Item RangeExpression::evaluateSingleton(const DynamicContext::Ptr &context) const
85 return m_operand1->evaluateSingleton(context);
88 SequenceType::List RangeExpression::expectedOperandTypes() const
90 SequenceType::List result;
91 result.append(CommonSequenceTypes::ZeroOrOneInteger);
92 result.append(CommonSequenceTypes::ZeroOrOneInteger);
96 SequenceType::Ptr RangeExpression::staticType() const
98 /* This logic makes the Cardinality more specific. */
99 Cardinality::Count from;
102 if(m_operand1->is(IDIntegerValue))
104 from = m_operand1->as<Literal>()->item().as<Integer>()->toInteger();
113 /* We can't check whether to is -1 since maybe the user wrote -1. Hence
114 * hasTo is required. */
116 Cardinality::Count to;
118 if(m_operand2->is(IDIntegerValue))
120 const xsInteger asInt = m_operand2->as<Literal>()->item().as<Integer>()->toInteger();
127 /* Cardinality::Count is not the same as type xsInteger. We had overflow. */
142 /* The query is incorrectly written, we'll evaluate to the empty sequence.
143 * Just return what's correct. */
144 return CommonSequenceTypes::ZeroOrMoreIntegers;
148 Cardinality::Count count = (to - from) + 1; /* + 1, since it's inclusive. */
149 return makeGenericSequenceType(BuiltinTypes::xsInteger, Cardinality::fromExact(count));
154 /* We can't do fromExact(from, -1) since the latter can evaluate to a value that actually is
155 * lower than from, although that unfortunately is very unlikely. */
156 return CommonSequenceTypes::ZeroOrMoreIntegers;
160 Expression::Properties RangeExpression::properties() const
162 return Expression::DisableElimination;
165 ExpressionVisitorResult::Ptr RangeExpression::accept(const ExpressionVisitor::Ptr &visitor) const
167 return visitor->visit(this);