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 "qabstractfloat_p.h"
43 #include "qboolean_p.h"
44 #include "qbuiltintypes_p.h"
45 #include "qcommonsequencetypes_p.h"
46 #include "qemptysequence_p.h"
47 #include "qfirstitempredicate_p.h"
48 #include "qgenericsequencetype_p.h"
49 #include "qitemmappingiterator_p.h"
50 #include "qliteral_p.h"
51 #include "qpatternistlocale_p.h"
52 #include "qtruthpredicate_p.h"
54 #include "qgenericpredicate_p.h"
58 using namespace QPatternist;
60 GenericPredicate::GenericPredicate(const Expression::Ptr &sourceExpression,
61 const Expression::Ptr &predicate) : PairContainer(sourceExpression,
66 Expression::Ptr GenericPredicate::create(const Expression::Ptr &sourceExpression,
67 const Expression::Ptr &predicateExpression,
68 const StaticContext::Ptr &context,
69 const QSourceLocation &location)
71 Q_ASSERT(sourceExpression);
72 Q_ASSERT(predicateExpression);
74 const ItemType::Ptr type(predicateExpression->staticType()->itemType());
76 if(predicateExpression->is(IDIntegerValue) &&
77 predicateExpression->as<Literal>()->item().as<Numeric>()->toInteger() == 1)
79 return createFirstItem(sourceExpression);
81 else if(BuiltinTypes::numeric->xdtTypeMatches(type))
82 { /* A numeric predicate, other than [1]. */
83 /* TODO at somepoint we'll return a specialized expr here, NumericPredicate or so.
84 * Dependency analysis is a bit tricky, since the contained expression can depend on
85 * some loop component. */
86 return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
88 else if(*CommonSequenceTypes::Empty == *type)
90 return EmptySequence::create(predicateExpression.data(), context);
92 else if(*BuiltinTypes::item == *type ||
93 *BuiltinTypes::xsAnyAtomicType == *type)
95 /* The type couldn't be narrowed at compile time, so we use
96 * a generic predicate. This check is before the CommonSequenceTypes::EBV check,
97 * because the latter matches these types as well. */
98 return Expression::Ptr(new GenericPredicate(sourceExpression, predicateExpression));
100 else if(CommonSequenceTypes::EBV->itemType()->xdtTypeMatches(type))
102 return Expression::Ptr(new TruthPredicate(sourceExpression, predicateExpression));
106 context->error(QtXmlPatterns::tr("A value of type %1 cannot be a "
107 "predicate. A predicate must have "
108 "either a numeric type or an "
109 "Effective Boolean Value type.")
110 .arg(formatType(context->namePool(),
111 sourceExpression->staticType())),
112 ReportContext::FORG0006, location);
113 return Expression::Ptr(); /* Silence compiler warning. */
117 Expression::Ptr GenericPredicate::createFirstItem(const Expression::Ptr &sourceExpression)
120 return Expression::Ptr(new FirstItemPredicate(sourceExpression));
123 Item GenericPredicate::mapToItem(const Item &item,
124 const DynamicContext::Ptr &context) const
126 const Item::Iterator::Ptr it(m_operand2->evaluateSequence(context));
127 const Item pcateItem(it->next());
130 return Item(); /* The predicate evaluated to the empty sequence */
131 else if(pcateItem.isNode())
133 /* Ok, now it must be an AtomicValue */
134 else if(BuiltinTypes::numeric->xdtTypeMatches(pcateItem.type()))
135 { /* It's a positional predicate. */
138 context->error(QtXmlPatterns::tr("A positional predicate must "
139 "evaluate to a single numeric "
141 ReportContext::FORG0006, this);
145 if(Double::isEqual(static_cast<xsDouble>(context->contextPosition()),
146 pcateItem.as<Numeric>()->toDouble()))
153 else if(Boolean::evaluateEBV(pcateItem, it, context)) /* It's a truth predicate. */
159 Item::Iterator::Ptr GenericPredicate::evaluateSequence(const DynamicContext::Ptr &context) const
161 const Item::Iterator::Ptr focus(m_operand1->evaluateSequence(context));
162 const DynamicContext::Ptr newContext(context->createFocus());
163 newContext->setFocusIterator(focus);
165 return makeItemMappingIterator<Item>(ConstPtr(this),
170 Item GenericPredicate::evaluateSingleton(const DynamicContext::Ptr &context) const
172 const Item::Iterator::Ptr focus(m_operand1->evaluateSequence(context));
173 const DynamicContext::Ptr newContext(context->createFocus());
174 newContext->setFocusIterator(focus);
175 return mapToItem(focus->next(), newContext);
178 SequenceType::List GenericPredicate::expectedOperandTypes() const
180 SequenceType::List result;
181 result.append(CommonSequenceTypes::ZeroOrMoreItems);
182 result.append(CommonSequenceTypes::ZeroOrMoreItems);
186 SequenceType::Ptr GenericPredicate::staticType() const
188 const SequenceType::Ptr type(m_operand1->staticType());
189 return makeGenericSequenceType(type->itemType(),
190 type->cardinality() | Cardinality::zeroOrOne());
193 ExpressionVisitorResult::Ptr GenericPredicate::accept(const ExpressionVisitor::Ptr &visitor) const
195 return visitor->visit(this);
198 ItemType::Ptr GenericPredicate::newFocusType() const
200 return m_operand1->staticType()->itemType();
203 Expression::Properties GenericPredicate::properties() const
205 return CreatesFocusForLast;
208 QString GenericPredicate::description() const
210 return QLatin1String("predicate");
213 Expression::ID GenericPredicate::id() const
215 return IDGenericPredicate;