1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qcardinalityverifier_p.h" |
41 | #include "qcommonsequencetypes_p.h" |
42 | #include "qemptysequence_p.h" |
43 | #include "qabstractxmlforwarditerator_p.h" |
44 | #include "qliteral_p.h" |
45 | #include "qsequencemappingiterator_p.h" |
46 | |
47 | #include "qexpressionsequence_p.h" |
48 | |
49 | #include <algorithm> |
50 | #include <iterator> |
51 | |
52 | QT_BEGIN_NAMESPACE |
53 | |
54 | using namespace QPatternist; |
55 | |
56 | ExpressionSequence::ExpressionSequence(const Expression::List &ops) : UnlimitedContainer(ops) |
57 | { |
58 | Q_ASSERT_X(1 < ops.count(), Q_FUNC_INFO, |
59 | "It makes no sense to have an ExpressionSequence containing less than two expressions." ); |
60 | } |
61 | |
62 | Item::Iterator::Ptr ExpressionSequence::mapToSequence(const Expression::Ptr &expr, |
63 | const DynamicContext::Ptr &context) const |
64 | { |
65 | return expr->evaluateSequence(context); |
66 | } |
67 | |
68 | Item::Iterator::Ptr ExpressionSequence::evaluateSequence(const DynamicContext::Ptr &context) const |
69 | { |
70 | return makeSequenceMappingIterator<Item>(mapper: ConstPtr(this), |
71 | source: makeListIterator(list: m_operands), |
72 | context); |
73 | } |
74 | |
75 | void ExpressionSequence::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const |
76 | { |
77 | Expression::List::const_iterator it(m_operands.constBegin()); |
78 | const Expression::List::const_iterator end(m_operands.constEnd()); |
79 | Expression::List result; |
80 | |
81 | for(; it != end; ++it) |
82 | (*it)->evaluateToSequenceReceiver(context); |
83 | } |
84 | |
85 | Expression::Ptr ExpressionSequence::compress(const StaticContext::Ptr &context) |
86 | { |
87 | const Expression::Ptr me(UnlimitedContainer::compress(context)); |
88 | |
89 | if(me != this) |
90 | return me; |
91 | |
92 | Expression::List::const_iterator it(m_operands.constBegin()); |
93 | const Expression::List::const_iterator end(m_operands.constEnd()); |
94 | Expression::List result; |
95 | |
96 | for(; it != end; ++it) |
97 | { |
98 | const ID Id = (*it)->id(); |
99 | |
100 | /* Remove empty sequences. This is rather important because we have some steps in the parser that |
101 | * intentionally, unconditionally and for temporary reasons create expressions like (expr, ()). Of course, |
102 | * empty sequences also occur as part of optimizations. |
103 | * |
104 | * User function call sites that are of type empty-sequence() must be avoided since |
105 | * they may contain calls to fn:error(), which we would rewrite away otherwise. */ |
106 | if(Id != IDUserFunctionCallsite && (*it)->staticType()->cardinality().isEmpty()) |
107 | { |
108 | /* Rewrite "(1, (), 2)" into "(1, 2)" by not |
109 | * adding (*it) to result. */ |
110 | continue; |
111 | } |
112 | else if(Id == IDExpressionSequence) |
113 | { |
114 | /* Rewrite "(1, (2, 3), 4)" into "(1, 2, 3, 4)" */ |
115 | const auto &operands = (*it)->operands(); |
116 | std::copy(first: operands.cbegin(), last: operands.cend(), result: std::back_inserter(x&: result)); |
117 | } else if (Id == IDLiteralSequence) { |
118 | /* Rewrite "(1, (2, 3), 4)" into "(1, 2, 3, 4)" */ |
119 | // Note: LiteralSequence does not use the dynamic context, so we pass in a nullptr. |
120 | Item::Iterator::Ptr seqIt = (*it)->evaluateSequence(context: DynamicContext::Ptr(nullptr)); |
121 | |
122 | while (!seqIt->next().isNull()) |
123 | result.append(t: Literal::Ptr(new Literal(seqIt->current()))); |
124 | } |
125 | else |
126 | result.append(t: *it); |
127 | } |
128 | |
129 | if(result.isEmpty()) |
130 | return EmptySequence::create(replacementFor: this, context); |
131 | else if(result.count() == 1) |
132 | return result.first(); |
133 | else |
134 | { |
135 | m_operands = result; |
136 | return me; |
137 | } |
138 | } |
139 | |
140 | Expression::Ptr ExpressionSequence::typeCheck(const StaticContext::Ptr &context, |
141 | const SequenceType::Ptr &reqType) |
142 | { |
143 | Q_ASSERT(reqType); |
144 | Expression::List::iterator it(m_operands.begin()); |
145 | const Expression::List::iterator end(m_operands.end()); |
146 | |
147 | /* We treat the cardinality differently here by allowing the empty sequence |
148 | * for each individual Expression, since the Cardinality can be conformed to by |
149 | * the ExpressionSequence as a whole(which we check for at the end). */ |
150 | const SequenceType::Ptr testOnlyIT(makeGenericSequenceType(itemType: reqType->itemType(), |
151 | cardinality: Cardinality::empty() | |
152 | reqType->cardinality())); |
153 | |
154 | for(; it != end; ++it) |
155 | *it = (*it)->typeCheck(context, reqType: testOnlyIT); |
156 | |
157 | /* The above loop is only guaranteed to find item type errors, but the cardinality |
158 | * can still be wrong since the operands were treated individually. */ |
159 | return CardinalityVerifier::verifyCardinality(operand: Expression::Ptr(this), card: reqType->cardinality(), context); |
160 | } |
161 | |
162 | Expression::Properties ExpressionSequence::properties() const |
163 | { |
164 | const Expression::List::const_iterator end(m_operands.constEnd()); |
165 | Expression::List::const_iterator it; |
166 | bool allEvaled = true; |
167 | Expression::Properties props(DisableElimination); /* Why do we have this flag? */ |
168 | |
169 | for(it = m_operands.constBegin(); it != end; ++it) |
170 | { |
171 | const Expression::Properties newp((*it)->properties()); |
172 | props |= newp; |
173 | |
174 | if((newp & IsEvaluated) != IsEvaluated) |
175 | { |
176 | allEvaled = false; |
177 | break; |
178 | } |
179 | } |
180 | |
181 | if(!allEvaled) |
182 | props &= ~IsEvaluated; /* Remove IsEvaluated. */ |
183 | |
184 | /* One of our children might need the focus, but we don't, so |
185 | * cut it out. */ |
186 | return props & ~RequiresFocus; |
187 | } |
188 | |
189 | SequenceType::Ptr ExpressionSequence::staticType() const |
190 | { |
191 | return operandsUnionType<ProductOfCardinality>(); |
192 | } |
193 | |
194 | SequenceType::List ExpressionSequence::expectedOperandTypes() const |
195 | { |
196 | SequenceType::List result; |
197 | /* ExpressionSequence is a bit strange type wise since it has an |
198 | * infinite amount of operands. */ |
199 | result.append(t: CommonSequenceTypes::ZeroOrMoreItems); |
200 | return result; |
201 | } |
202 | |
203 | ExpressionVisitorResult::Ptr ExpressionSequence::accept(const ExpressionVisitor::Ptr &visitor) const |
204 | { |
205 | return visitor->visit(this); |
206 | } |
207 | |
208 | Expression::ID ExpressionSequence::id() const |
209 | { |
210 | return IDExpressionSequence; |
211 | } |
212 | |
213 | QT_END_NAMESPACE |
214 | |