1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qboolean_p.h"
43 #include "qcommonvalues_p.h"
44 #include "qemptysequence_p.h"
45 #include "qliteral_p.h"
46 #include "qliteralsequence_p.h"
47 #include "qoperandsiterator_p.h"
48 #include "qoptimizerframework_p.h"
49 #include "qstaticfocuscontext_p.h"
50 #include "qtypechecker_p.h"
52 #include "qexpression_p.h"
56 using namespace QPatternist;
58 Expression::~Expression()
62 StaticContext::Ptr Expression::finalizeStaticContext(const StaticContext::Ptr &context) const
65 const ItemType::Ptr focusType(newFocusType());
67 return StaticContext::Ptr(new StaticFocusContext(focusType, context));
70 Expression::Ptr Expression::typeCheck(const StaticContext::Ptr &context,
71 const SequenceType::Ptr &reqType)
74 typeCheckOperands(context);
75 return TypeChecker::applyFunctionConversion(Expression::Ptr(this), reqType, context);
78 void Expression::typeCheckOperands(const StaticContext::Ptr &context)
80 const Expression::List ops(operands());
82 /* Check if this expression has any operands at all. */
84 return; /* We're done, early exit. */
86 const SequenceType::List opTypes(expectedOperandTypes());
87 Expression::List result;
89 /* If we create a focus, we handle the last one specially, so avoid it in the loop. */
90 const bool createsFocus = has(CreatesFocusForLast);
91 const SequenceType::List::const_iterator typeEnd(createsFocus ? --opTypes.constEnd()
92 : opTypes.constEnd());
93 const Expression::List::const_iterator end(createsFocus ? --ops.constEnd()
96 SequenceType::List::const_iterator reqType(opTypes.constBegin());
97 SequenceType::Ptr t(*reqType);
98 // TODO we assign twice to t here(also below in loop) when ops.size() > 1
100 Expression::List::const_iterator it(ops.constBegin());
102 for(; it != end; ++it)
104 /* This ensures that the last expectedOperandType stays, and is
105 * used for all other operands. This is used for expressions that
106 * have an infinite amount of operands, such as the concat() function. */
107 if(reqType != typeEnd)
113 /* Let the child & its children typecheck. */
114 result.append((*it)->typeCheck(context, t));
119 const StaticContext::Ptr newContext(finalizeStaticContext(context));
120 result.append(ops.last()->typeCheck(newContext, opTypes.last()));
126 Expression::Ptr Expression::invokeOptimizers(const Expression::Ptr &expr,
127 const StaticContext::Ptr &context)
131 const OptimizationPass::List opts(expr->optimizationPasses());
133 if(opts.isEmpty()) /* Early exit. */
138 const OptimizationPass::List::const_iterator passEnd(opts.constEnd());
139 const OptimizationPass::List::const_iterator end(opts.constEnd());
140 OptimizationPass::List::const_iterator passIt(opts.constBegin());
142 for(; passIt != passEnd; ++passIt) /* Invoke each optimization pass. */
144 const OptimizationPass::Ptr pass(*passIt); /* Alias, for readability. */
145 OptimizationPass::ExpressionMarker sourceMarker(pass->sourceExpression);
147 if(pass->startIdentifier && !pass->startIdentifier->matches(expr))
149 /* This pass specified a start identifier and it did
150 * not match -- let's try the next OptimizationPass. */
154 const ExpressionIdentifier::List::const_iterator idEnd(pass->operandIdentifiers.constEnd());
155 ExpressionIdentifier::List::const_iterator idIt(pass->operandIdentifiers.constBegin());
156 const Expression::List ops(expr->operands());
157 const Expression::List::const_iterator opEnd(ops.constEnd());
158 Expression::List::const_iterator opIt(ops.constBegin());
160 switch(pass->operandsMatchMethod)
162 case OptimizationPass::Sequential:
164 for(; opIt != opEnd; ++opIt)
166 const Expression::Ptr operand(*opIt); /* Alias, for readability. */
167 const ExpressionIdentifier::Ptr opIdentifier(*idIt); /* Alias, for readability. */
168 if(opIdentifier && !opIdentifier->matches(operand))
177 break; /* All operands matched, so this pass matched. */
180 /* The loop above did not finish which means all operands did not match.
181 Therefore, this OptimizationPass did not match -- let's try the next one. */
185 case OptimizationPass::AnyOrder:
187 Q_ASSERT_X(ops.count() == 2, Q_FUNC_INFO,
188 "AnyOrder is currently only supported for Expressions with two operands.");
189 if(pass->operandIdentifiers.first()->matches(ops.first()) &&
190 pass->operandIdentifiers.last()->matches(ops.last()))
194 else if(pass->operandIdentifiers.first()->matches(ops.last()) &&
195 pass->operandIdentifiers.last()->matches(ops.first()))
197 sourceMarker.first() = 1;
199 break; /* This pass matched. */
202 continue; /* This pass didn't match, let's loop through the next pass. */
206 /* Figure out the source Expression, if any. */
207 Expression::List operands;
208 Expression::Ptr sourceExpr;
210 if(!sourceMarker.isEmpty())
212 const OptimizationPass::ExpressionMarker::const_iterator mEnd(sourceMarker.constEnd());
213 OptimizationPass::ExpressionMarker::const_iterator mIt(sourceMarker.constBegin());
216 for(; mIt != mEnd; ++mIt)
219 sourceExpr = sourceExpr->operands().at(*mIt);
222 operands.append(sourceExpr);
225 if(operands.isEmpty())
227 Q_ASSERT(pass->resultCreator);
228 return pass->resultCreator->create(Expression::List(), context, expr.data())->compress(context);
230 else if(pass->resultCreator)
231 return pass->resultCreator->create(operands, context, expr.data())->compress(context);
241 Expression::Ptr Expression::compress(const StaticContext::Ptr &context)
243 if(!compressOperands(context))
245 /* At least one of the operands cannot be evaluated at compile, so
246 * 'this' Expression cannot const fold. */
247 return invokeOptimizers(Expression::Ptr(this), context);
250 Expression::Ptr retval;
252 if(hasDependency(DisableElimination))
253 retval = Expression::Ptr(this);
255 retval = constantPropagate(context);
257 return invokeOptimizers(retval, context);
260 Expression::Ptr Expression::constantPropagate(const StaticContext::Ptr &context) const
264 /* Optimization: We rewrite literals to literals here, which is pointless.
265 * Maybe we should have a property which says "doesn't disable elimination
266 * but don't eliminate me." */
267 if(staticType()->cardinality().allowsMany())
269 Item::Iterator::Ptr it(evaluateSequence(context->dynamicContext()));
271 Item item(it->next());
279 switch(result.count())
282 return EmptySequence::create(this, context);
284 return rewrite(Expression::Ptr(new Literal(result.first())), context);
286 return rewrite(Expression::Ptr(new LiteralSequence(result)), context);
291 const Item item(evaluateSingleton(context->dynamicContext()));
294 return rewrite(Expression::Ptr(new Literal(item)), context);
296 return EmptySequence::create(this, context);
300 Item::Iterator::Ptr Expression::evaluateSequence(const DynamicContext::Ptr &context) const
302 const Item item(evaluateSingleton(context));
305 return makeSingletonIterator(item);
307 return CommonValues::emptyIterator;
310 Item Expression::evaluateSingleton(const DynamicContext::Ptr &context) const
312 return Boolean::fromValue(evaluateEBV(context));
315 bool Expression::evaluateEBV(const DynamicContext::Ptr &context) const
317 return Boolean::evaluateEBV(evaluateSequence(context), context);
320 void Expression::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
322 QAbstractXmlReceiver *const receiver = context->outputReceiver();
323 const Item::Iterator::Ptr it(evaluateSequence(context));
324 Item next(it->next());
328 receiver->item(next);
333 ItemType::Ptr Expression::expectedContextItemType() const
335 Q_ASSERT_X(false, Q_FUNC_INFO,
336 "expectedContextItemType() must be overridden when RequiresContextItem is set.");
337 return ItemType::Ptr();
340 Expression::Properties Expression::properties() const
345 Expression::Properties Expression::dependencies() const
347 OperandsIterator it(Ptr(const_cast<Expression *>(this)), OperandsIterator::ExcludeParent);
348 Expression::Ptr next(it.next());
350 Properties dependencies(properties());
354 dependencies |= next->dependencies();
358 return dependencies & (Expression::RequiresFocus | Expression::IsEvaluated | Expression::DisableElimination);
361 void Expression::announceFocusType(const ItemType::Ptr &itemType)
363 const Expression::List ops(operands());
364 const int len = ops.count();
366 for(int i = 0; i < len; ++i)
367 ops.at(i)->announceFocusType(itemType);
370 Expression::Properties Expression::deepProperties() const
372 Properties props(properties());
373 const Expression::List ops(operands());
374 const int len = ops.count();
376 for(int i = 0; i < len; ++i)
377 props |= ops.at(i)->deepProperties();
382 Expression::ID Expression::id() const
384 return IDIgnorableExpression;
387 OptimizationPass::List Expression::optimizationPasses() const
389 return OptimizationPass::List();
392 ItemType::Ptr Expression::newFocusType() const
394 Q_ASSERT_X(false, Q_FUNC_INFO,
395 "This function must be overridden when CreatesFocusForLast is set.");
396 return ItemType::Ptr();
399 const SourceLocationReflection *Expression::actualReflection() const
404 QString Expression::description() const
406 return QString::fromLatin1("Expression, id: %1").arg(QString::number(id()));
409 PatternPriority Expression::patternPriority() const