source: trunk/src/xmlpatterns/functions/qsequencefns.cpp@ 497

Last change on this file since 497 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 12.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
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"
55
56#include "qsequencefns_p.h"
57
58QT_BEGIN_NAMESPACE
59
60using namespace QPatternist;
61
62bool BooleanFN::evaluateEBV(const DynamicContext::Ptr &context) const
63{
64 return m_operands.first()->evaluateEBV(context);
65}
66
67Expression::Ptr BooleanFN::typeCheck(const StaticContext::Ptr &context,
68 const SequenceType::Ptr &reqType)
69{
70 return EBVExtractor::typeCheck<FunctionCall>(context, reqType, this);
71}
72
73Item::Iterator::Ptr IndexOfFN::evaluateSequence(const DynamicContext::Ptr &context) const
74{
75 return Item::Iterator::Ptr(new IndexOfIterator(m_operands.first()->evaluateSequence(context),
76 m_operands.at(1)->evaluateSingleton(context),
77 comparator(), context,
78 ConstPtr(this)));
79}
80
81Expression::Ptr IndexOfFN::typeCheck(const StaticContext::Ptr &context,
82 const SequenceType::Ptr &reqType)
83{
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());
87
88 if(*CommonSequenceTypes::Empty == *t1 ||
89 *CommonSequenceTypes::Empty == *t2)
90 {
91 return EmptySequence::create(this, context);
92 }
93 else
94 {
95 prepareComparison(fetchComparator(t1, t2, context));
96 return me;
97 }
98}
99
100Item::Iterator::Ptr DistinctValuesFN::evaluateSequence(const DynamicContext::Ptr &context) const
101{
102 return Item::Iterator::Ptr(new DistinctIterator(m_operands.first()->evaluateSequence(context),
103 comparator(),
104 ConstPtr(this),
105 context));
106}
107
108Expression::Ptr DistinctValuesFN::typeCheck(const StaticContext::Ptr &context,
109 const SequenceType::Ptr &reqType)
110{
111 const Expression::Ptr me(FunctionCall::typeCheck(context, reqType));
112 const ItemType::Ptr t1(m_operands.first()->staticType()->itemType());
113
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))
119 return me;
120 else
121 {
122 prepareComparison(fetchComparator(t1, t1, context));
123 return me;
124 }
125}
126
127SequenceType::Ptr DistinctValuesFN::staticType() const
128{
129 const SequenceType::Ptr t(m_operands.first()->staticType());
130 return makeGenericSequenceType(t->itemType(),
131 t->cardinality().allowsMany() ? Cardinality::oneOrMore()
132 : Cardinality::exactlyOne());
133}
134
135Item::Iterator::Ptr InsertBeforeFN::evaluateSequence(const DynamicContext::Ptr &context) const
136{
137 const Item::Iterator::Ptr target(m_operands.first()->evaluateSequence(context));
138 const Item::Iterator::Ptr inserts(m_operands.at(2)->evaluateSequence(context));
139
140 xsInteger position = m_operands.at(1)->evaluateSingleton(context).as<Numeric>()->toInteger();
141
142 if(position < 1)
143 position = 1;
144
145 return Item::Iterator::Ptr(new InsertionIterator(target, position, inserts));
146}
147
148Item InsertBeforeFN::evaluateSingleton(const DynamicContext::Ptr &context) const
149{
150 return evaluateSequence(context)->next();
151}
152
153SequenceType::Ptr InsertBeforeFN::staticType() const
154{
155 const SequenceType::Ptr t1(m_operands.first()->staticType());
156 const SequenceType::Ptr t2(m_operands.last()->staticType());
157
158 return makeGenericSequenceType(t1->itemType() | t2->itemType(),
159 t1->cardinality() + t2->cardinality());
160}
161
162Item::Iterator::Ptr RemoveFN::evaluateSequence(const DynamicContext::Ptr &context) const
163{
164 const xsInteger pos = m_operands.last()->evaluateSingleton(context).as<Numeric>()->toInteger();
165 Item::Iterator::Ptr it(m_operands.first()->evaluateSequence(context));
166
167 if(pos < 1)
168 return it;
169
170 return Item::Iterator::Ptr(new RemovalIterator(it, pos));
171}
172
173Item RemoveFN::evaluateSingleton(const DynamicContext::Ptr &context) const
174{
175 const xsInteger pos = m_operands.last()->evaluateSingleton(context).as<Numeric>()->toInteger();
176 if(pos <= 1)
177 return Item();
178
179 return m_operands.first()->evaluateSingleton(context);
180}
181
182SequenceType::Ptr RemoveFN::staticType() const
183{
184 const SequenceType::Ptr opType(m_operands.first()->staticType());
185 const Cardinality c(opType->cardinality());
186
187 if(c.minimum() == 0)
188 return makeGenericSequenceType(opType->itemType(), c);
189 else
190 {
191 return makeGenericSequenceType(opType->itemType(),
192 Cardinality::fromRange(c.minimum() - 1,
193 c.maximum()));
194 }
195}
196
197Item::Iterator::Ptr ReverseFN::evaluateSequence(const DynamicContext::Ptr &context) const
198{
199 return m_operands.first()->evaluateSequence(context)->toReversed();
200}
201
202Expression::Ptr ReverseFN::typeCheck(const StaticContext::Ptr &context,
203 const SequenceType::Ptr &reqType)
204{
205 if(m_operands.first()->staticType()->cardinality().allowsMany())
206 return FunctionCall::typeCheck(context, reqType);
207 else
208 return m_operands.first()->typeCheck(context, reqType);
209}
210
211SequenceType::Ptr ReverseFN::staticType() const
212{
213 return m_operands.first()->staticType();
214}
215
216SubsequenceFN::SubsequenceFN() : m_hasTypeChecked(false)
217{
218}
219
220Expression::Ptr SubsequenceFN::typeCheck(const StaticContext::Ptr &context,
221 const SequenceType::Ptr &reqType)
222{
223 m_hasTypeChecked = true;
224 return FunctionCall::typeCheck(context, reqType);
225}
226
227Item::Iterator::Ptr SubsequenceFN::evaluateSequence(const DynamicContext::Ptr &context) const
228{
229 Item::Iterator::Ptr it(m_operands.first()->evaluateSequence(context));
230
231 xsInteger startingLoc = m_operands.at(1)->evaluateSingleton(context).as<Numeric>()->round()->toInteger();
232 xsInteger length = -1;
233
234 if(m_operands.count() == 3)
235 {
236 length = m_operands.last()->evaluateSingleton(context).as<Numeric>()->toInteger();
237
238 if(startingLoc + length < 1 || (startingLoc > (startingLoc + length)))
239 return CommonValues::emptyIterator;
240 }
241
242 /* F&O, 15.1.10, "If $startingLoc is zero or negative, the
243 * subsequence includes items from the beginning of the $sourceSeq." */
244 if(startingLoc < 1)
245 startingLoc = 1;
246
247 if(length < 1 && length != -1)
248 return CommonValues::emptyIterator;
249 else
250 return Item::Iterator::Ptr(new SubsequenceIterator(it, startingLoc, length));
251}
252
253Item SubsequenceFN::evaluateSingleton(const DynamicContext::Ptr &context) const
254{
255 return evaluateSequence(context)->next();
256}
257
258Expression::Ptr SubsequenceFN::compress(const StaticContext::Ptr &context)
259{
260 const Expression::Ptr me(FunctionCall::compress(context));
261 if(me != this)
262 return me;
263
264 const Expression::Ptr lenArg(m_operands.value(2));
265 if(lenArg && lenArg->isEvaluated())
266 {
267 const xsInteger length = lenArg->as<Literal>()->item().as<Numeric>()->round()->toInteger();
268
269 if(length <= 0)
270 return EmptySequence::create(this, context);
271 }
272
273 return me;
274}
275
276SequenceType::Ptr SubsequenceFN::staticType() const
277{
278 const SequenceType::Ptr opType(m_operands.first()->staticType());
279 const Cardinality opCard(opType->cardinality());
280
281 /* Optimization: we can do much stronger inference here. If the length is a
282 * constant, we can constrain the range at least upwards of the
283 * cardinality, for instance. */
284
285 /* The subsequence(expr, 1, 1), add empty-sequence() to the static type.
286 *
287 * Note that we cannot do all these inferences before we've typechecked our
288 * operands. The only known case of where our staticType() is called before
289 * typeCheck() is through xmlpatternsview, although it wouldn't be
290 * surprising if the more exotic paths can achieve that too.
291 */
292 if(m_hasTypeChecked &&
293 m_operands.at(1)->isEvaluated() &&
294 m_operands.count() == 3 &&
295 m_operands.at(2)->isEvaluated() &&
296 m_operands.at(1)->as<Literal>()->item().as<Numeric>()->round()->toInteger() == 1 &&
297 m_operands.at(2)->as<Literal>()->item().as<Numeric>()->round()->toInteger() == 1)
298 {
299 return makeGenericSequenceType(opType->itemType(),
300 opCard.toWithoutMany());
301 }
302 else
303 {
304 return makeGenericSequenceType(opType->itemType(),
305 opCard | Cardinality::zeroOrOne());
306 }
307}
308
309Expression::Ptr DocFN::typeCheck(const StaticContext::Ptr &context,
310 const SequenceType::Ptr &reqType)
311{
312 /* See the doxygen documentation for this function for the explanation
313 * to why this implementation is here, as opposed to in
314 * qsequencegeneratingfns.cpp. */
315
316 Q_ASSERT(context);
317
318 prepareStaticBaseURI(context);
319
320 const Expression::Ptr uriOp(m_operands.first());
321
322 if(!uriOp->isEvaluated())
323 return Expression::Ptr(FunctionCall::typeCheck(context, reqType));
324
325 const Item uriItem(uriOp->evaluateSingleton(context->dynamicContext()));
326
327 if(!uriItem)
328 return EmptySequence::create(this, context)->typeCheck(context, reqType); // TODO test this
329
330 /* These two lines were previously in a separate function but are now duplicated
331 * in DocFN::evaluateSingleton(), as part of a workaround for solaris-cc-64. */
332 const QUrl mayRela(AnyURI::toQUrl<ReportContext::FODC0005>(uriItem.stringValue(), context, this));
333 const QUrl uri(context->resolveURI(mayRela, staticBaseURI()));
334
335 /* The URI is supplied statically, so, let's try to be clever. */
336 Q_ASSERT_X(context->resourceLoader(), Q_FUNC_INFO,
337 "No resource loader is set in the StaticContext.");
338 m_type = context->resourceLoader()->announceDocument(uri, ResourceLoader::MayUse);
339
340 if(m_type)
341 {
342 Q_ASSERT(CommonSequenceTypes::ZeroOrOneDocumentNode->matches(m_type));
343 return Expression::Ptr(FunctionCall::typeCheck(context, reqType));
344 }
345 else
346 {
347 context->error(QtXmlPatterns::tr("It will not be possible to retrieve %1.").arg(formatURI(uri)),
348 ReportContext::FODC0002, this);
349 return Expression::Ptr();
350 }
351}
352
353QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.