[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
[561] | 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
[2] | 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 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 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"
|
---|
| 49 |
|
---|
| 50 | #include "qpath_p.h"
|
---|
| 51 |
|
---|
| 52 | QT_BEGIN_NAMESPACE
|
---|
| 53 |
|
---|
| 54 | using namespace QPatternist;
|
---|
| 55 |
|
---|
| 56 | Path::Path(const Expression::Ptr &operand1,
|
---|
| 57 | const Expression::Ptr &operand2,
|
---|
| 58 | const Kind kind) : PairContainer(operand1, operand2)
|
---|
| 59 | , m_hasCreatedSorter(kind != RegularPath)
|
---|
| 60 | , m_isLast(false)
|
---|
| 61 | , m_checkXPTY0018(kind == RegularPath)
|
---|
| 62 | , m_kind(kind)
|
---|
| 63 | {
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | Item::Iterator::Ptr Path::mapToSequence(const Item &item,
|
---|
| 67 | const DynamicContext::Ptr &context) const
|
---|
| 68 | {
|
---|
| 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. */
|
---|
| 72 | Q_ASSERT(item);
|
---|
| 73 | Q_UNUSED(item); /* Needed when compiling in release mode. */
|
---|
| 74 | return m_operand2->evaluateSequence(context);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | Item::Iterator::Ptr Path::evaluateSequence(const DynamicContext::Ptr &context) const
|
---|
| 78 | {
|
---|
| 79 | /* Note, we use the old context for m_operand1. */
|
---|
| 80 | const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
|
---|
| 81 |
|
---|
| 82 | const DynamicContext::Ptr focus(context->createFocus());
|
---|
| 83 | focus->setFocusIterator(source);
|
---|
| 84 |
|
---|
| 85 | const Item::Iterator::Ptr result(makeSequenceMappingIterator<Item>(ConstPtr(this), source, focus));
|
---|
| 86 |
|
---|
| 87 | if(m_checkXPTY0018)
|
---|
| 88 | {
|
---|
| 89 | /* This is an expensive code path, but it should happen very rarely. */
|
---|
| 90 |
|
---|
| 91 | enum FoundItem
|
---|
| 92 | {
|
---|
| 93 | FoundNone,
|
---|
| 94 | FoundNode,
|
---|
| 95 | FoundAtomicValue
|
---|
| 96 | } hasFound = FoundNone;
|
---|
| 97 |
|
---|
| 98 | Item::List whenChecked;
|
---|
| 99 |
|
---|
| 100 | Item next(result->next());
|
---|
| 101 |
|
---|
| 102 | while(next)
|
---|
| 103 | {
|
---|
| 104 | const FoundItem found = next.isAtomicValue() ? FoundAtomicValue : FoundNode;
|
---|
| 105 |
|
---|
| 106 | if(hasFound != FoundNone && hasFound != found)
|
---|
| 107 | {
|
---|
| 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);
|
---|
| 112 | }
|
---|
| 113 | else
|
---|
| 114 | hasFound = found;
|
---|
| 115 |
|
---|
| 116 | whenChecked.append(next);
|
---|
| 117 | next = result->next();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | return makeListIterator(whenChecked);
|
---|
| 121 | }
|
---|
| 122 | else
|
---|
| 123 | return result;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | Item Path::evaluateSingleton(const DynamicContext::Ptr &context) const
|
---|
| 127 | {
|
---|
| 128 | /* This function is called if both operands' cardinality is exactly-one. Therefore
|
---|
| 129 | * we manually go forward in the focus by calling next().
|
---|
| 130 | *
|
---|
| 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.
|
---|
| 133 | */
|
---|
| 134 |
|
---|
| 135 | /* Note, we use the old context for m_operand1. */
|
---|
| 136 | const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
|
---|
| 137 |
|
---|
| 138 | const DynamicContext::Ptr focus(context->createFocus());
|
---|
| 139 | focus->setFocusIterator(source);
|
---|
| 140 |
|
---|
| 141 | /* This test is needed because if the focus is empty, we don't want to(nor can't) evaluate
|
---|
| 142 | * the next step. */
|
---|
| 143 | // TODO Why are we at all invoked then?
|
---|
| 144 | if(source->next())
|
---|
| 145 | return m_operand2->evaluateSingleton(focus);
|
---|
| 146 | else
|
---|
| 147 | return Item();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | void Path::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
|
---|
| 151 | {
|
---|
| 152 | /* Note, we use the old context for m_operand1. */
|
---|
| 153 | const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
|
---|
| 154 |
|
---|
| 155 | const DynamicContext::Ptr focus(context->createFocus());
|
---|
| 156 | focus->setFocusIterator(source);
|
---|
| 157 |
|
---|
| 158 | while(source->next())
|
---|
| 159 | m_operand2->evaluateToSequenceReceiver(focus);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | Expression::Ptr Path::compress(const StaticContext::Ptr &context)
|
---|
| 163 | {
|
---|
| 164 | const Expression::Ptr me(PairContainer::compress(context));
|
---|
| 165 |
|
---|
| 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))
|
---|
| 169 | return m_operand2;
|
---|
| 170 |
|
---|
| 171 | /* We do this as late as we can, such that we pick up the most recent type
|
---|
| 172 | * from the operand. */
|
---|
[561] | 173 | if(m_isLast && m_kind != XSLTForEach && m_operand2->staticType()->itemType() == BuiltinTypes::item)
|
---|
[2] | 174 | m_checkXPTY0018 = true;
|
---|
| 175 |
|
---|
| 176 | return me;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | Expression::Ptr Path::typeCheck(const StaticContext::Ptr &context,
|
---|
| 180 | const SequenceType::Ptr &reqType)
|
---|
| 181 | {
|
---|
| 182 | m_operand2->announceFocusType(newFocusType());
|
---|
| 183 |
|
---|
| 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.
|
---|
| 188 | *
|
---|
| 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.
|
---|
| 192 | *
|
---|
| 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.
|
---|
| 195 | *
|
---|
| 196 | * The type we expect, CommonSequenceTypes::ZeroOrMoreNodes() needs to be in sync with
|
---|
| 197 | * what we return in expectedOperandTypes(). */
|
---|
| 198 | if(m_kind != XSLTForEach)
|
---|
| 199 | {
|
---|
| 200 | m_operand1 = TypeChecker::applyFunctionConversion(m_operand1,
|
---|
| 201 | CommonSequenceTypes::ZeroOrMoreNodes,
|
---|
| 202 | context,
|
---|
| 203 | m_kind == ForApplyTemplate ? ReportContext::XTTE0520
|
---|
| 204 | : ReportContext::XPTY0019);
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /* If our step ends with atomic values, we cannot sort.
|
---|
| 208 | *
|
---|
| 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.
|
---|
| 212 | */
|
---|
| 213 | if(!m_hasCreatedSorter)
|
---|
| 214 | {
|
---|
| 215 | m_hasCreatedSorter = true;
|
---|
| 216 |
|
---|
| 217 | return NodeSortExpression::wrapAround(Expression::Ptr(this), context)->typeCheck(context, reqType);
|
---|
| 218 | }
|
---|
| 219 | else
|
---|
| 220 | return PairContainer::typeCheck(context, reqType);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | SequenceType::List Path::expectedOperandTypes() const
|
---|
| 224 | {
|
---|
| 225 | SequenceType::List result;
|
---|
| 226 |
|
---|
| 227 | /* This value needs to be in sync with what we pass to
|
---|
| 228 | * applyFunctionConversion() in typeCheck() above.
|
---|
| 229 | *
|
---|
| 230 | * We don't have the XPTY0019 restriction when we're synthetic XSL-T code.
|
---|
| 231 | */
|
---|
| 232 | if(m_kind == XSLTForEach)
|
---|
| 233 | result.append(CommonSequenceTypes::ZeroOrMoreItems);
|
---|
| 234 | else
|
---|
| 235 | result.append(CommonSequenceTypes::ZeroOrMoreNodes);
|
---|
| 236 |
|
---|
| 237 | result.append(CommonSequenceTypes::ZeroOrMoreItems);
|
---|
| 238 | return result;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | SequenceType::Ptr Path::staticType() const
|
---|
| 242 | {
|
---|
| 243 | const SequenceType::Ptr opType(m_operand2->staticType());
|
---|
| 244 |
|
---|
| 245 | /* For each parent step, we evaluate the child step. So multiply the two
|
---|
| 246 | * cardinalities. */
|
---|
| 247 | return makeGenericSequenceType(opType->itemType(),
|
---|
| 248 | m_operand1->staticType()->cardinality() * opType->cardinality());
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | ExpressionVisitorResult::Ptr Path::accept(const ExpressionVisitor::Ptr &visitor) const
|
---|
| 252 | {
|
---|
| 253 | return visitor->visit(this);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | Expression::Properties Path::properties() const
|
---|
| 257 | {
|
---|
| 258 | return CreatesFocusForLast | ((m_operand1->properties() | m_operand2->properties()) & (RequiresCurrentItem | DisableElimination));
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | ItemType::Ptr Path::newFocusType() const
|
---|
| 262 | {
|
---|
| 263 | return m_operand1->staticType()->itemType();
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | Expression::ID Path::id() const
|
---|
| 267 | {
|
---|
| 268 | return IDPath;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | QT_END_NAMESPACE
|
---|