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 "qcommonsequencetypes_p.h"
43 #include "qemptysequence_p.h"
44 #include "qgenericsequencetype_p.h"
45 #include "qitemmappingiterator_p.h"
46 #include "qoptimizationpasses_p.h"
47 #include "qsequencemappingiterator_p.h"
49 #include "qforclause_p.h"
53 using namespace QPatternist;
55 ForClause::ForClause(const VariableSlotID varSlot,
56 const Expression::Ptr &bindingSequence,
57 const Expression::Ptr &returnExpression,
58 const VariableSlotID positionSlot) : PairContainer(bindingSequence, returnExpression),
60 m_positionSlot(positionSlot),
63 Q_ASSERT(m_positionSlot > -2);
66 Item ForClause::mapToItem(const Item &item,
67 const DynamicContext::Ptr &context) const
69 context->setRangeVariable(m_varSlot, item);
70 return m_operand2->evaluateSingleton(context);
73 Item::Iterator::Ptr ForClause::mapToSequence(const Item &item,
74 const DynamicContext::Ptr &context) const
76 context->setRangeVariable(m_varSlot, item);
77 return m_operand2->evaluateSequence(context);
80 void ForClause::riggPositionalVariable(const DynamicContext::Ptr &context,
81 const Item::Iterator::Ptr &source) const
83 if(m_positionSlot > -1)
84 context->setPositionIterator(m_positionSlot, source);
87 Item::Iterator::Ptr ForClause::evaluateSequence(const DynamicContext::Ptr &context) const
89 const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
91 riggPositionalVariable(context, source);
95 return makeSequenceMappingIterator<Item>(ConstPtr(this),
101 return makeItemMappingIterator<Item>(ConstPtr(this),
107 Item ForClause::evaluateSingleton(const DynamicContext::Ptr &context) const
109 return evaluateSequence(context)->next();
112 void ForClause::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
114 Item::Iterator::Ptr it;
115 const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
117 riggPositionalVariable(context, source);
119 Item next(source->next());
123 context->setRangeVariable(m_varSlot, next);
124 m_operand2->evaluateToSequenceReceiver(context);
125 next = source->next();
129 Expression::Ptr ForClause::typeCheck(const StaticContext::Ptr &context,
130 const SequenceType::Ptr &reqType)
132 const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
133 const Cardinality card(m_operand1->staticType()->cardinality());
135 /* If our source is empty we will always evaluate to the empty sequence, so rewrite. */
137 return EmptySequence::create(this, context);
141 /* This breaks because the variable references haven't rewritten themselves, so
142 * they dangle. When this is fixed, evaluateSingleton can be removed. */
144 else if(card->allowsMany())
151 Expression::Ptr ForClause::compress(const StaticContext::Ptr &context)
153 const Expression::Ptr me(PairContainer::compress(context));
157 /* This is done after calling PairContainer::typeCheck(). The advantage of this is that
158 * m_allowsMany is updated according to what the operand says after it has compressed. However,
159 * if it was initialized to false(as it once was..), ForClause::evaluateSequence()
160 * would potentially have been called by PairContainer::compress(), and it would have
161 * used an unsafe branch. */
162 m_allowsMany = m_operand2->staticType()->cardinality().allowsMany();
167 SequenceType::Ptr ForClause::staticType() const
169 const SequenceType::Ptr returnType(m_operand2->staticType());
171 return makeGenericSequenceType(returnType->itemType(),
172 m_operand1->staticType()->cardinality()
173 * /* multiply operator */
174 returnType->cardinality());
177 SequenceType::List ForClause::expectedOperandTypes() const
179 SequenceType::List result;
180 result.append(CommonSequenceTypes::ZeroOrMoreItems);
181 result.append(CommonSequenceTypes::ZeroOrMoreItems);
185 ExpressionVisitorResult::Ptr ForClause::accept(const ExpressionVisitor::Ptr &visitor) const
187 return visitor->visit(this);
190 OptimizationPass::List ForClause::optimizationPasses() const
192 return OptimizationPasses::forPasses;
195 Expression::ID ForClause::id() const