Qt 4.8
qforclause.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
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"
48 
49 #include "qforclause_p.h"
50 
52 
53 using namespace QPatternist;
54 
56  const Expression::Ptr &bindingSequence,
57  const Expression::Ptr &returnExpression,
58  const VariableSlotID positionSlot) : PairContainer(bindingSequence, returnExpression),
59  m_varSlot(varSlot),
60  m_positionSlot(positionSlot),
61  m_allowsMany(true)
62 {
64 }
65 
67  const DynamicContext::Ptr &context) const
68 {
69  context->setRangeVariable(m_varSlot, item);
70  return m_operand2->evaluateSingleton(context);
71 }
72 
74  const DynamicContext::Ptr &context) const
75 {
76  context->setRangeVariable(m_varSlot, item);
77  return m_operand2->evaluateSequence(context);
78 }
79 
81  const Item::Iterator::Ptr &source) const
82 {
83  if(m_positionSlot > -1)
84  context->setPositionIterator(m_positionSlot, source);
85 }
86 
88 {
89  const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
90 
91  riggPositionalVariable(context, source);
92 
93  if(m_allowsMany)
94  {
95  return makeSequenceMappingIterator<Item>(ConstPtr(this),
96  source,
97  context);
98  }
99  else
100  {
101  return makeItemMappingIterator<Item>(ConstPtr(this),
102  source,
103  context);
104  }
105 }
106 
108 {
109  return evaluateSequence(context)->next();
110 }
111 
113 {
115  const Item::Iterator::Ptr source(m_operand1->evaluateSequence(context));
116 
117  riggPositionalVariable(context, source);
118 
119  Item next(source->next());
120 
121  while(next)
122  {
123  context->setRangeVariable(m_varSlot, next);
125  next = source->next();
126  }
127 }
128 
130  const SequenceType::Ptr &reqType)
131 {
132  const Expression::Ptr me(PairContainer::typeCheck(context, reqType));
133  const Cardinality card(m_operand1->staticType()->cardinality());
134 
135  /* If our source is empty we will always evaluate to the empty sequence, so rewrite. */
136  if(card.isEmpty())
137  return EmptySequence::create(this, context);
138  else
139  return me;
140 
141  /* This breaks because the variable references haven't rewritten themselves, so
142  * they dangle. When this is fixed, evaluateSingleton can be removed. */
143  /*
144  else if(card->allowsMany())
145  return me;
146  else
147  return m_operand2;
148  */
149 }
150 
152 {
153  const Expression::Ptr me(PairContainer::compress(context));
154  if(me != this)
155  return me;
156 
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. */
163 
164  return me;
165 }
166 
168 {
169  const SequenceType::Ptr returnType(m_operand2->staticType());
170 
171  return makeGenericSequenceType(returnType->itemType(),
173  * /* multiply operator */
174  returnType->cardinality());
175 }
176 
178 {
179  SequenceType::List result;
182  return result;
183 }
184 
186 {
187  return visitor->visit(this);
188 }
189 
191 {
193 }
194 
196 {
197  return IDForClause;
198 }
199 
qint32 VariableSlotID
const VariableSlotID m_positionSlot
Definition: qforclause_p.h:110
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static const SequenceType::Ptr ZeroOrMoreItems
#define it(className, varName)
const VariableSlotID m_varSlot
Definition: qforclause_p.h:109
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qexpression.cpp:70
static Expression::Ptr create(const Expression *const replacementFor, const StaticContext::Ptr &context)
Creates an EmptySequence that is a replacement for replacementFor.
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual void setPositionIterator(const VariableSlotID slot, const Item::Iterator::Ptr &newValue)=0
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
Definition: qforclause.cpp:107
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const
Definition: qforclause.cpp:185
virtual SequenceType::Ptr staticType() const
Definition: qforclause.cpp:167
The namespace for the internal API of QtXmlPatterns.
virtual Cardinality cardinality() const =0
virtual QList< QExplicitlySharedDataPointer< OptimizationPass > > optimizationPasses() const
Definition: qforclause.cpp:190
virtual SequenceType::List expectedOperandTypes() const
Definition: qforclause.cpp:177
virtual void setRangeVariable(const VariableSlotID slot, const Item &newValue)=0
Item mapToItem(const Item &item, const DynamicContext::Ptr &context) const
Definition: qforclause.cpp:66
virtual void evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
Definition: qforclause.cpp:112
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
Definition: qforclause.cpp:87
virtual SequenceType::Ptr staticType() const =0
Represents an item in the XPath 2.0 Data Model.
Definition: qitem_p.h:182
QExplicitlySharedDataPointer< const ForClause > ConstPtr
Definition: qforclause_p.h:108
virtual ID id() const
Definition: qforclause.cpp:195
ForClause(const VariableSlotID varSlot, const Expression::Ptr &bindingSequence, const Expression::Ptr &returnExpression, const VariableSlotID positionSlot)
Definition: qforclause.cpp:55
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
Definition: qforclause.cpp:129
Represents a cardinality, a possible , often represented by occurrence indicators.
void riggPositionalVariable(const DynamicContext::Ptr &context, const Item::Iterator::Ptr &source) const
Definition: qforclause.cpp:80
virtual void evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
Definition: qforclause.cpp:151
virtual Item evaluateSingleton(const DynamicContext::Ptr &context) const
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
Base class for expressions that has exactly two operands.
Item::Iterator::Ptr mapToSequence(const Item &item, const DynamicContext::Ptr &context) const
Definition: qforclause.cpp:73