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 "qdistinctiterator_p.h"
46 #include "qebvextractor_p.h"
47 #include "qemptysequence_p.h"
48 #include "qgenericsequencetype_p.h"
49 #include "qindexofiterator_p.h"
50 #include "qinsertioniterator_p.h"
51 #include "qinteger_p.h"
52 #include "qremovaliterator_p.h"
53 #include "qsequencegeneratingfns_p.h"
54 #include "qsubsequenceiterator_p.h"
56 #include "qsequencefns_p.h"
60 using namespace QPatternist;
62 bool BooleanFN::evaluateEBV(const DynamicContext::Ptr &context) const
64 return m_operands.first()->evaluateEBV(context);
67 Expression::Ptr BooleanFN::typeCheck(const StaticContext::Ptr &context,
68 const SequenceType::Ptr &reqType)
70 return EBVExtractor::typeCheck<FunctionCall>(context, reqType, this);
73 Item::Iterator::Ptr IndexOfFN::evaluateSequence(const DynamicContext::Ptr &context) const
75 return Item::Iterator::Ptr(new IndexOfIterator(m_operands.first()->evaluateSequence(context),
76 m_operands.at(1)->evaluateSingleton(context),
77 comparator(), context,
81 Expression::Ptr IndexOfFN::typeCheck(const StaticContext::Ptr &context,
82 const SequenceType::Ptr &reqType)
84 const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
85 const ItemType::Ptr t1(m_operands.first()->staticType()->itemType());
86 const ItemType::Ptr t2(m_operands.at(1)->staticType()->itemType());
88 if(*CommonSequenceTypes::Empty == *t1 ||
89 *CommonSequenceTypes::Empty == *t2)
91 return EmptySequence::create(this, context);
95 prepareComparison(fetchComparator(t1, t2, context));
100 Item::Iterator::Ptr DistinctValuesFN::evaluateSequence(const DynamicContext::Ptr &context) const
102 return Item::Iterator::Ptr(new DistinctIterator(m_operands.first()->evaluateSequence(context),
108 Expression::Ptr DistinctValuesFN::typeCheck(const StaticContext::Ptr &context,
109 const SequenceType::Ptr &reqType)
111 const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
112 const ItemType::Ptr t1(m_operands.first()->staticType()->itemType());
114 if(*CommonSequenceTypes::Empty == *t1)
115 return EmptySequence::create(this, context);
116 else if(!m_operands.first()->staticType()->cardinality().allowsMany())
117 return m_operands.first();
118 else if(BuiltinTypes::xsAnyAtomicType->xdtTypeMatches(t1))
122 prepareComparison(fetchComparator(t1, t1, context));
127 SequenceType::Ptr DistinctValuesFN::staticType() const
129 const SequenceType::Ptr t(m_operands.first()->staticType());
130 return makeGenericSequenceType(t->itemType(),
131 t->cardinality().allowsMany() ? Cardinality::oneOrMore()
132 : Cardinality::exactlyOne());
135 Item::Iterator::Ptr InsertBeforeFN::evaluateSequence(const DynamicContext::Ptr &context) const
137 const Item::Iterator::Ptr target(m_operands.first()->evaluateSequence(context));
138 const Item::Iterator::Ptr inserts(m_operands.at(2)->evaluateSequence(context));
140 xsInteger position = m_operands.at(1)->evaluateSingleton(context).as<Numeric>()->toInteger();
145 return Item::Iterator::Ptr(new InsertionIterator(target, position, inserts));
148 Item InsertBeforeFN::evaluateSingleton(const DynamicContext::Ptr &context) const
150 return evaluateSequence(context)->next();
153 SequenceType::Ptr InsertBeforeFN::staticType() const
155 const SequenceType::Ptr t1(m_operands.first()->staticType());
156 const SequenceType::Ptr t2(m_operands.last()->staticType());
158 return makeGenericSequenceType(t1->itemType() | t2->itemType(),
159 t1->cardinality() + t2->cardinality());
162 Item::Iterator::Ptr RemoveFN::evaluateSequence(const DynamicContext::Ptr &context) const
164 const xsInteger pos = m_operands.last()->evaluateSingleton(context).as<Numeric>()->toInteger();
165 Item::Iterator::Ptr it(m_operands.first()->evaluateSequence(context));
170 return Item::Iterator::Ptr(new RemovalIterator(it, pos));
173 Item RemoveFN::evaluateSingleton(const DynamicContext::Ptr &context) const
175 const xsInteger pos = m_operands.last()->evaluateSingleton(context).as<Numeric>()->toInteger();
179 return m_operands.first()->evaluateSingleton(context);
182 SequenceType::Ptr RemoveFN::staticType() const
184 const SequenceType::Ptr opType(m_operands.first()->staticType());
185 const Cardinality c(opType->cardinality());
188 return makeGenericSequenceType(opType->itemType(), c);
191 return makeGenericSequenceType(opType->itemType(),
192 Cardinality::fromRange(c.minimum() - 1,
197 Item::Iterator::Ptr ReverseFN::evaluateSequence(const DynamicContext::Ptr &context) const
199 return m_operands.first()->evaluateSequence(context)->toReversed();
202 Expression::Ptr ReverseFN::typeCheck(const StaticContext::Ptr &context,
203 const SequenceType::Ptr &reqType)
205 if(m_operands.first()->staticType()->cardinality().allowsMany())
206 return FunctionCall::typeCheck(context, reqType);
208 return m_operands.first()->typeCheck(context, reqType);
211 SequenceType::Ptr ReverseFN::staticType() const
213 return m_operands.first()->staticType();
216 SubsequenceFN::SubsequenceFN() : m_hasTypeChecked(false)
220 Expression::Ptr SubsequenceFN::typeCheck(const StaticContext::Ptr &context,
221 const SequenceType::Ptr &reqType)
223 m_hasTypeChecked = true;
224 return FunctionCall::typeCheck(context, reqType);
227 Item::Iterator::Ptr SubsequenceFN::evaluateSequence(const DynamicContext::Ptr &context) const
229 Item::Iterator::Ptr it(m_operands.first()->evaluateSequence(context));
231 xsInteger startingLoc = m_operands.at(1)->evaluateSingleton(context).as<Numeric>()->round()->toInteger();
232 xsInteger length = -1;
234 if(m_operands.count() == 3)
236 length = m_operands.last()->evaluateSingleton(context).as<Numeric>()->toInteger();
238 if(startingLoc + length < 1 || (startingLoc > (startingLoc + length)))
239 return CommonValues::emptyIterator;
242 /* F&O, 15.1.10, "If $startingLoc is zero or negative, the
243 * subsequence includes items from the beginning of the $sourceSeq." */
247 if(length < 1 && length != -1)
248 return CommonValues::emptyIterator;
249 return Item::Iterator::Ptr(new SubsequenceIterator(it, startingLoc, length));
252 Item SubsequenceFN::evaluateSingleton(const DynamicContext::Ptr &context) const
254 return evaluateSequence(context)->next();
257 Expression::Ptr SubsequenceFN::compress(const StaticContext::Ptr &context)
259 const Expression::Ptr me(FunctionCall::compress(context));
263 const Expression::Ptr lenArg(m_operands.value(2));
264 if(lenArg && lenArg->isEvaluated())
266 const xsInteger length = lenArg->as<Literal>()->item().as<Numeric>()->round()->toInteger();
269 return EmptySequence::create(this, context);
275 SequenceType::Ptr SubsequenceFN::staticType() const
277 const SequenceType::Ptr opType(m_operands.first()->staticType());
278 const Cardinality opCard(opType->cardinality());
280 /* Optimization: we can do much stronger inference here. If the length is a
281 * constant, we can constrain the range at least upwards of the
282 * cardinality, for instance. */
284 /* The subsequence(expr, 1, 1), add empty-sequence() to the static type.
286 * Note that we cannot do all these inferences before we've typechecked our
287 * operands. The only known case of where our staticType() is called before
288 * typeCheck() is through xmlpatternsview, although it wouldn't be
289 * surprising if the more exotic paths can achieve that too.
291 if(m_hasTypeChecked &&
292 m_operands.at(1)->isEvaluated() &&
293 m_operands.count() == 3 &&
294 m_operands.at(2)->isEvaluated() &&
295 m_operands.at(1)->as<Literal>()->item().as<Numeric>()->round()->toInteger() == 1 &&
296 m_operands.at(2)->as<Literal>()->item().as<Numeric>()->round()->toInteger() == 1)
298 return makeGenericSequenceType(opType->itemType(),
299 opCard.toWithoutMany());
303 return makeGenericSequenceType(opType->itemType(),
304 opCard | Cardinality::zeroOrOne());
308 Expression::Ptr DocFN::typeCheck(const StaticContext::Ptr &context,
309 const SequenceType::Ptr &reqType)
311 /* See the doxygen documentation for this function for the explanation
312 * to why this implementation is here, as opposed to in
313 * qsequencegeneratingfns.cpp. */
317 prepareStaticBaseURI(context);
319 const Expression::Ptr uriOp(m_operands.first());
321 if(!uriOp->isEvaluated())
322 return Expression::Ptr(FunctionCall::typeCheck(context, reqType));
324 const Item uriItem(uriOp->evaluateSingleton(context->dynamicContext()));
327 return EmptySequence::create(this, context)->typeCheck(context, reqType); // TODO test this
329 /* These two lines were previously in a separate function but are now duplicated
330 * in DocFN::evaluateSingleton(), as part of a workaround for solaris-cc-64. */
331 const QUrl mayRela(AnyURI::toQUrl<ReportContext::FODC0005>(uriItem.stringValue(), context, this));
332 const QUrl uri(context->resolveURI(mayRela, staticBaseURI()));
334 /* The URI is supplied statically, so, let's try to be clever. */
335 Q_ASSERT_X(context->resourceLoader(), Q_FUNC_INFO,
336 "No resource loader is set in the StaticContext.");
337 m_type = context->resourceLoader()->announceDocument(uri, ResourceLoader::MayUse);
341 Q_ASSERT(CommonSequenceTypes::ZeroOrOneDocumentNode->matches(m_type));
342 return Expression::Ptr(FunctionCall::typeCheck(context, reqType));
346 context->error(QtXmlPatterns::tr("It will not be possible to retrieve %1.").arg(formatURI(uri)),
347 ReportContext::FODC0002, this);
348 return Expression::Ptr();