source: trunk/src/xmlpatterns/expr/qexpressionsequence.cpp@ 843

Last change on this file since 843 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 7.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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**
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.
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**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qcardinalityverifier_p.h"
43#include "qcommonsequencetypes_p.h"
44#include "qemptysequence_p.h"
45#include "qsequencemappingiterator_p.h"
46
47#include "qexpressionsequence_p.h"
48
49QT_BEGIN_NAMESPACE
50
51using namespace QPatternist;
52
53ExpressionSequence::ExpressionSequence(const Expression::List &ops) : UnlimitedContainer(ops)
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
59Item::Iterator::Ptr ExpressionSequence::mapToSequence(const Expression::Ptr &expr,
60 const DynamicContext::Ptr &context) const
61{
62 return expr->evaluateSequence(context);
63}
64
65Item::Iterator::Ptr ExpressionSequence::evaluateSequence(const DynamicContext::Ptr &context) const
66{
67 return makeSequenceMappingIterator<Item>(ConstPtr(this),
68 makeListIterator(m_operands),
69 context);
70}
71
72void ExpressionSequence::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
73{
74 Expression::List::const_iterator it(m_operands.constBegin());
75 const Expression::List::const_iterator end(m_operands.constEnd());
76 Expression::List result;
77
78 for(; it != end; ++it)
79 (*it)->evaluateToSequenceReceiver(context);
80}
81
82Expression::Ptr ExpressionSequence::compress(const StaticContext::Ptr &context)
83{
84 const Expression::Ptr me(UnlimitedContainer::compress(context));
85
86 if(me != this)
87 return me;
88
89 Expression::List::const_iterator it(m_operands.constBegin());
90 const Expression::List::const_iterator end(m_operands.constEnd());
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())