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 "qgenericsequencetype_p.h"
45 #include "qnodesort_p.h"
46 #include "qpatternistlocale_p.h"
47 #include "qsequencemappingiterator_p.h"
48 #include "qtypechecker_p.h"
54 using namespace QPatternist;
56 Path::Path(const Expression::Ptr &operand1,
57 const Expression::Ptr &operand2,
58 const Kind kind) : PairContainer(operand1, operand2)
59 , m_hasCreatedSorter(kind != RegularPath)
61 , m_checkXPTY0018(kind == RegularPath)
66 Item::Iterator::Ptr Path::mapToSequence(const Item &item,
67 const DynamicContext::Ptr &context) const
69 /* item is the focus here. That is in <e/>/1, item is <e/>. However,
70 * we don't use it, since the context item is accessed through
71 * DynamicContext::focusIterator() and friends. */
73 Q_UNUSED(item); /* Needed when compiling in release mode. */
74 return m_operand2->evaluateSequence(context);
77 Item::Iterator::Ptr Path::evaluateSequence(const DynamicContext::Ptr &context) const
79 /* Note, we use the old context for m_operand1. */
80 const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
82 const DynamicContext::Ptr focus(context->createFocus());
83 focus->setFocusIterator(source);
85 const Item::Iterator::Ptr result(makeSequenceMappingIterator<Item>(ConstPtr(this), source, focus));
89 /* This is an expensive code path, but it should happen very rarely. */
96 } hasFound = FoundNone;
98 Item::List whenChecked;
100 Item next(result->next());
104 const FoundItem found = next.isAtomicValue() ? FoundAtomicValue : FoundNode;
106 if(hasFound != FoundNone && hasFound != found)
108 /* It's an atomic value and we've already found a node. Mixed content. */
109 context->error(QtXmlPatterns::tr("The last step in a path must contain either nodes "
110 "or atomic values. It cannot be a mixture between the two."),
111 ReportContext::XPTY0018, this);
116 whenChecked.append(next);
117 next = result->next();
120 return makeListIterator(whenChecked);
126 Item Path::evaluateSingleton(const DynamicContext::Ptr &context) const
128 /* This function is called if both operands' cardinality is exactly-one. Therefore
129 * we manually go forward in the focus by calling next().
131 * We don't check for XPTY0018, only in evaluateSequence(), since if we're guaranteed
132 * to evaluate to one item, we can only evaluate to one node or one atomic value.
135 /* Note, we use the old context for m_operand1. */
136 const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
138 const DynamicContext::Ptr focus(context->createFocus());
139 focus->setFocusIterator(source);
141 /* This test is needed because if the focus is empty, we don't want to(nor can't) evaluate
143 // TODO Why are we at all invoked then?
145 return m_operand2->evaluateSingleton(focus);
150 void Path::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
152 /* Note, we use the old context for m_operand1. */
153 const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
155 const DynamicContext::Ptr focus(context->createFocus());
156 focus->setFocusIterator(source);
158 while(source->next())
159 m_operand2->evaluateToSequenceReceiver(focus);
162 Expression::Ptr Path::compress(const StaticContext::Ptr &context)
164 const Expression::Ptr me(PairContainer::compress(context));
166 /* "./expr" is by now equal to "expr" since we've done
167 * focus/type checks, and a node sorter has been inserted. */
168 if(m_operand1->is(IDContextItem))
171 /* We do this as late as we can, such that we pick up the most recent type
172 * from the operand. */
173 if(m_isLast && m_kind != XSLTForEach && m_operand2->staticType()->itemType() == BuiltinTypes::item)
174 m_checkXPTY0018 = true;
179 Expression::Ptr Path::typeCheck(const StaticContext::Ptr &context,
180 const SequenceType::Ptr &reqType)
182 m_operand2->announceFocusType(newFocusType());
184 /* Here we apply the function conversion first, and with the error code
185 * that we want -- XPTY0019 instead of XPTY0004. Unfortunately
186 * PairContainer::typeCheck() will do the type check again, which is
187 * redundant in the case of when we're not XSLTForEach.
189 * If we're XSLTForEach, it means we're a synthetic "map" expression for
190 * implementing various XSL-T expressions, and hence don't have the
191 * constraint of XPTY0019.
193 * It's important that typeCheck() is run for the operands(of course), and the call to
194 * PairContainer::typeCheck() ensures that below, in the case that we're XSL-T code.
196 * The type we expect, CommonSequenceTypes::ZeroOrMoreNodes() needs to be in sync with
197 * what we return in expectedOperandTypes(). */
198 if(m_kind != XSLTForEach)
200 m_operand1 = TypeChecker::applyFunctionConversion(m_operand1,
201 CommonSequenceTypes::ZeroOrMoreNodes,
203 m_kind == ForApplyTemplate ? ReportContext::XTTE0520
204 : ReportContext::XPTY0019);
207 /* If our step ends with atomic values, we cannot sort.
209 * We must smack the NodeSortExpression ontop before calling typeCheck(), since the latter
210 * may insert an Atomizer, as possibly mandated by reqType. By doing it after, the Atomizer
211 * will be a parent to NodeSortExpression, as opposed to a child.
213 if(!m_hasCreatedSorter)
215 m_hasCreatedSorter = true;
217 return NodeSortExpression::wrapAround(Expression::Ptr(this), context)->typeCheck(context, reqType);
220 return PairContainer::typeCheck(context, reqType);
223 SequenceType::List Path::expectedOperandTypes() const
225 SequenceType::List result;
227 /* This value needs to be in sync with what we pass to
228 * applyFunctionConversion() in typeCheck() above.
230 * We don't have the XPTY0019 restriction when we're synthetic XSL-T code.
232 if(m_kind == XSLTForEach)
233 result.append(CommonSequenceTypes::ZeroOrMoreItems);
235 result.append(CommonSequenceTypes::ZeroOrMoreNodes);
237 result.append(CommonSequenceTypes::ZeroOrMoreItems);
241 SequenceType::Ptr Path::staticType() const
243 const SequenceType::Ptr opType(m_operand2->staticType());
245 /* For each parent step, we evaluate the child step. So multiply the two
247 return makeGenericSequenceType(opType->itemType(),
248 m_operand1->staticType()->cardinality() * opType->cardinality());
251 ExpressionVisitorResult::Ptr Path::accept(const ExpressionVisitor::Ptr &visitor) const
253 return visitor->visit(this);
256 Expression::Properties Path::properties() const
258 return CreatesFocusForLast | ((m_operand1->properties() | m_operand2->properties()) & (RequiresCurrentItem | DisableElimination));
261 ItemType::Ptr Path::newFocusType() const
263 return m_operand1->staticType()->itemType();
266 Expression::ID Path::id() const