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 "qebvextractor_p.h"
47 #include "qliteral_p.h"
48 #include "qoptimizationpasses_p.h"
49 #include "quntypedatomicconverter_p.h"
50 #include "qvaluecomparison_p.h"
52 #include "qgeneralcomparison_p.h"
56 using namespace QPatternist;
58 GeneralComparison::GeneralComparison(const Expression::Ptr &op1,
59 const AtomicComparator::Operator op,
60 const Expression::Ptr &op2,
61 const bool isBackwardsCompat) : PairContainer(op1, op2)
63 , m_isBackwardsCompat(isBackwardsCompat)
67 bool GeneralComparison::generalCompare(const Item &op1,
69 const DynamicContext::Ptr &context) const
75 return compare(op1, op2, comparator(), m_operator);
77 Expression::Ptr a1(new Literal(op1));
78 Expression::Ptr a2(new Literal(op2));
80 const AtomicComparator::Ptr comp(fetchGeneralComparator(a1, a2, context));
81 /* The fetchGeneralComparator call may rewrite a1 and/or a2. */
86 return compare(a1->evaluateSingleton(context),
87 a2->evaluateSingleton(context),
92 bool GeneralComparison::evaluateEBV(const DynamicContext::Ptr &context) const
94 const Item::Iterator::Ptr it1(m_operand1->evaluateSequence(context));
95 Item item1(it1->next());
100 const Item::Iterator::Ptr it2(m_operand2->evaluateSequence(context));
110 if(generalCompare(item1, item2, context))
123 const Item::List::const_iterator end(cache.constEnd());
124 Item::List::const_iterator it(cache.constBegin());
126 for(; it != end; ++it)
127 if(generalCompare(item1, *it, context))
135 Expression::Ptr GeneralComparison::compress(const StaticContext::Ptr &context)
137 const Expression::Ptr me(PairContainer::compress(context));
142 if(ValueComparison::isCaseInsensitiveCompare(m_operand1, m_operand2))
143 useCaseInsensitiveComparator();
148 Expression::Ptr GeneralComparison::typeCheck(const StaticContext::Ptr &context,
149 const SequenceType::Ptr &reqType)
152 const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
154 const ItemType::Ptr t1(m_operand1->staticType()->itemType());
155 const ItemType::Ptr t2(m_operand2->staticType()->itemType());
157 if(*CommonSequenceTypes::Empty == *t1 ||
158 *CommonSequenceTypes::Empty == *t2)
160 return wrapLiteral(CommonValues::BooleanFalse, context, this);
163 if(*BuiltinTypes::xsAnyAtomicType == *t1 ||
164 *BuiltinTypes::xsAnyAtomicType == *t2)
167 prepareComparison(fetchGeneralComparator(m_operand1, m_operand2, context));
169 if(!m_operand1->staticType()->cardinality().allowsMany() &&
170 !m_operand2->staticType()->cardinality().allowsMany())
172 /* Rewrite to a ValueComparison whose operands uses typing rules
173 * as for an general comparison(that's what's done above). */
174 return rewrite(Expression::Ptr(new ValueComparison(m_operand1,
176 m_operand2))->typeCheck(context, reqType),
183 void GeneralComparison::updateType(ItemType::Ptr &type,
184 const Expression::Ptr &source)
186 type = source->staticType()->itemType();
189 AtomicComparator::Ptr GeneralComparison::fetchGeneralComparator(Expression::Ptr &op1,
190 Expression::Ptr &op2,
191 const ReportContext::Ptr &context) const
193 ItemType::Ptr t1(op1->staticType()->itemType());
194 ItemType::Ptr t2(op2->staticType()->itemType());
196 /* a. "If one of the atomic values is an instance of xs:untypedAtomic and
197 * the other is an instance of a numeric type, then the xs:untypedAtomic
198 * value is cast to the type xs:double." */
199 if(BuiltinTypes::numeric->xdtTypeMatches(t1) &&
200 BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t2))
202 op2 = Expression::Ptr(new UntypedAtomicConverter(op2, BuiltinTypes::xsDouble));
204 /* The types might have changed, reload. */
207 else if(BuiltinTypes::numeric->xdtTypeMatches(t2) &&
208 BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t1))
210 op1 = Expression::Ptr(new UntypedAtomicConverter(op1, BuiltinTypes::xsDouble));
212 /* The types might have changed, reload. */
215 /* "If XPath 1.0 compatibility mode is true, a general comparison is
216 * evaluated by applying the following rules, in order:
217 * 1. If either operand is a single atomic value that is an instance of
218 * xs:boolean, then the other operand is converted to xs:boolean by taking
219 * its effective boolean value."
221 * Notably, it's not conversion to boolean, it is EBV extraction.
223 else if(m_isBackwardsCompat && BuiltinTypes::xsBoolean->xdtTypeMatches(t1))
225 op2 = Expression::Ptr(new EBVExtractor(op2));
228 else if(m_isBackwardsCompat && BuiltinTypes::xsBoolean->xdtTypeMatches(t2))
230 op1 = Expression::Ptr(new EBVExtractor(op1));
233 /* b. "If one of the atomic values is an instance of xs:untypedAtomic and
234 * the other is an instance of xs:untypedAtomic or xs:string, then the
235 * xs:untypedAtomic value (or values) is (are) cast to the type xs:string."
237 * c. "If one of the atomic values is an instance of xs:untypedAtomic and the
238 * other is not an instance of xs:string, xs:untypedAtomic, or any numeric
239 * type, then the xs:untypedAtomic value is cast to the dynamic type of the
241 else if(BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t1) &&
242 !BuiltinTypes::xsString->xdtTypeMatches(t2) &&
243 !BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t2) &&
244 !BuiltinTypes::xsAnyURI->xdtTypeMatches(t2))
246 op1 = Expression::Ptr(new UntypedAtomicConverter(op1, t2));
249 else if(BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t2) &&
250 !BuiltinTypes::xsString->xdtTypeMatches(t1) &&
251 !BuiltinTypes::xsUntypedAtomic->xdtTypeMatches(t1) &&
252 !BuiltinTypes::xsAnyURI->xdtTypeMatches(t1))
254 op2 = Expression::Ptr(new UntypedAtomicConverter(op2, t1));
258 /* d. "After performing the conversions described above, the atomic
259 * values are compared using one of the value comparison operators
260 * eq, ne, lt, le, gt, or ge, depending on whether the general comparison
261 * operator was =, !=, <, <=, >, or >=. The values have the required
262 * magnitude relationship if and only if the result of this value comparison
265 return fetchComparator(t1, t2, context);
268 OptimizationPass::List GeneralComparison::optimizationPasses() const
270 Q_ASSERT(!OptimizationPasses::comparisonPasses.isEmpty());
271 return OptimizationPasses::comparisonPasses;
274 SequenceType::List GeneralComparison::expectedOperandTypes() const
276 SequenceType::List result;
277 result.append(CommonSequenceTypes::ZeroOrMoreAtomicTypes);
278 result.append(CommonSequenceTypes::ZeroOrMoreAtomicTypes);
282 SequenceType::Ptr GeneralComparison::staticType() const
284 return CommonSequenceTypes::ExactlyOneBoolean;
287 ExpressionVisitorResult::Ptr GeneralComparison::accept(const ExpressionVisitor::Ptr &visitor) const
289 return visitor->visit(this);
292 Expression::ID GeneralComparison::id() const
294 return IDGeneralComparison;