Qt 4.8
qexpressionsequence.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 "qcardinalityverifier_p.h"
43 #include "qcommonsequencetypes_p.h"
44 #include "qemptysequence_p.h"
46 
47 #include "qexpressionsequence_p.h"
48 
50 
51 using namespace QPatternist;
52 
54 {
55  Q_ASSERT_X(1 < ops.count(), Q_FUNC_INFO,
56  "It makes no sense to have an ExpressionSequence containing less than two expressions.");
57 }
58 
60  const DynamicContext::Ptr &context) const
61 {
62  return expr->evaluateSequence(context);
63 }
64 
66 {
67  return makeSequenceMappingIterator<Item>(ConstPtr(this),
69  context);
70 }
71 
73 {
76  Expression::List result;
77 
78  for(; it != end; ++it)
79  (*it)->evaluateToSequenceReceiver(context);
80 }
81 
83 {
85 
86  if(me != this)
87  return me;
88 
91  Expression::List result;
92 
93  for(; it != end; ++it)
94  {
95  const ID Id = (*it)->id();
96 
97  /* Remove empty sequences. This is rather important because we have some steps in the parser that
98  * intentionally, unconditionally and for temporary reasons create expressions like (expr, ()). Of course,
99  * empty sequences also occur as part of optimizations.
100  *
101  * User function call sites that are of type empty-sequence() must be avoided since
102  * they may contain calls to fn:error(), which we would rewrite away otherwise. */
103  if(Id != IDUserFunctionCallsite && (*it)->staticType()->cardinality().isEmpty())
104  {
105  /* Rewrite "(1, (), 2)" into "(1, 2)" by not
106  * adding (*it) to result. */
107  continue;
108  }
109  else if(Id == IDExpressionSequence)
110  {
111  /* Rewrite "(1, (2, 3), 4)" into "(1, 2, 3, 4)" */
112  Expression::List::const_iterator seqIt((*it)->operands().constBegin());
113  const Expression::List::const_iterator seqEnd((*it)->operands().constEnd());
114 
115  for(; seqIt != seqEnd; ++seqIt)
116  result.append(*seqIt);
117  }
118  else
119  result.append(*it);
120  }
121 
122  if(result.isEmpty())
123  return EmptySequence::create(this, context);
124  else if(result.count() == 1)
125  return result.first();
126  else
127  {
128  m_operands = result;
129  return me;
130  }
131 }
132 
134  const SequenceType::Ptr &reqType)
135 {
136  Q_ASSERT(reqType);
139 
140  /* We treat the cardinality differently here by allowing the empty sequence
141  * for each individual Expression, since the Cardinality can be conformed to by
142  * the ExpressionSequence as a whole(which we check for at the end). */
143  const SequenceType::Ptr testOnlyIT(makeGenericSequenceType(reqType->itemType(),
145  reqType->cardinality()));
146 
147  for(; it != end; ++it)
148  *it = (*it)->typeCheck(context, testOnlyIT);
149 
150  /* The above loop is only guaranteed to find item type errors, but the cardinality
151  * can still be wrong since the operands were treated individually. */
152  return CardinalityVerifier::verifyCardinality(Expression::Ptr(this), reqType->cardinality(), context);
153 }
154 
156 {
159  bool allEvaled = true;
160  Expression::Properties props(DisableElimination); /* Why do we have this flag? */
161 
162  for(it = m_operands.constBegin(); it != end; ++it)
163  {
164  const Expression::Properties newp((*it)->properties());
165  props |= newp;
166 
167  if((newp & IsEvaluated) != IsEvaluated)
168  {
169  allEvaled = false;
170  break;
171  }
172  }
173 
174  if(!allEvaled)
175  props &= ~IsEvaluated; /* Remove IsEvaluated. */
176 
177  /* One of our children might need the focus, but we don't, so
178  * cut it out. */
179  return props & ~RequiresFocus;
180 }
181 
183 {
184  return operandsUnionType<ProductOfCardinality>();
185 }
186 
188 {
189  SequenceType::List result;
190  /* ExpressionSequence is a bit strange type wise since it has an
191  * infinite amount of operands. */
193  return result;
194 }
195 
197 {
198  return visitor->visit(this);
199 }
200 
202 {
203  return IDExpressionSequence;
204 }
205 
virtual Expression::Properties properties() const
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static const SequenceType::Ptr ZeroOrMoreItems
#define it(className, varName)
virtual SequenceType::Ptr staticType() const
ExpressionSequence(const Expression::List &operands)
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlist.h:267
The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object...
Definition: qshareddata.h:136
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
QAbstractXmlForwardIterator< T >::Ptr makeListIterator(const QList< T > &list)
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
static Expression::Ptr create(const Expression *const replacementFor, const StaticContext::Ptr &context)
Creates an EmptySequence that is a replacement for replacementFor.
Item::Iterator::Ptr mapToSequence(const Expression::Ptr &, const DynamicContext::Ptr &) const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
static Expression::Ptr verifyCardinality(const Expression::Ptr &operand, const Cardinality &card, const StaticContext::Ptr &context, const ReportContext::ErrorCode code=ReportContext::XPTY0004)
friend class const_iterator
Definition: qlist.h:264
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
static Cardinality empty()
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:270
The namespace for the internal API of QtXmlPatterns.
virtual Cardinality cardinality() const =0
friend class iterator
Definition: qlist.h:226
virtual void evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
virtual SequenceType::List expectedOperandTypes() const
Base class for expressions that has any amount of operands.
virtual Expression::Ptr typeCheck(const StaticContext::Ptr &context, const SequenceType::Ptr &reqType)
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
virtual ItemType::Ptr itemType() const =0
QExplicitlySharedDataPointer< const ExpressionSequence > ConstPtr
static const KeyPair *const end
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &) const
virtual Item::Iterator::Ptr evaluateSequence(const DynamicContext::Ptr &context) const
virtual ExpressionVisitorResult::Ptr accept(const ExpressionVisitor::Ptr &visitor) const
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272
virtual Expression::Ptr compress(const StaticContext::Ptr &context)
#define Q_FUNC_INFO
Definition: qglobal.h:1871