1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 * @short This file is included by qevaluationcache_p.h.
45 * If you need includes in this file, put them in qevaluationcache_p.h, outside of the namespace.
48 template<bool IsForGlobal>
49 EvaluationCache<IsForGlobal>::EvaluationCache(const Expression::Ptr &op,
50 const VariableDeclaration::Ptr &varDecl,
51 const VariableSlotID aSlot) : SingleContainer(op)
52 , m_declaration(varDecl)
55 Q_ASSERT(m_declaration);
56 Q_ASSERT(m_varSlot > -1);
59 template<bool IsForGlobal>
60 DynamicContext::Ptr EvaluationCache<IsForGlobal>::topFocusContext(const DynamicContext::Ptr &context)
62 DynamicContext::Ptr result(context);
66 DynamicContext::Ptr candidate(result->previousContext());
68 /* We want the top focus, not GenericDynamicContext. */
69 if(candidate && candidate->focusIterator())
76 template<bool IsForGlobal>
77 Item EvaluationCache<IsForGlobal>::evaluateSingleton(const DynamicContext::Ptr &context) const
79 ItemCacheCell &cell = IsForGlobal ? context->globalItemCacheCell(m_varSlot) : context->itemCacheCell(m_varSlot);
81 if(cell.cacheState == ItemCacheCell::Full)
82 return cell.cachedItem;
85 Q_ASSERT(cell.cacheState == ItemCacheCell::Empty);
86 cell.cachedItem = m_operand->evaluateSingleton(IsForGlobal ? topFocusContext(context) : context);
87 cell.cacheState = ItemCacheCell::Full;
88 return cell.cachedItem;
92 #if defined(Q_OS_IRIX) && defined(Q_CC_MIPS)
94 * @short Compile workaround for MIPSPro on IRIX.
96 * This function is never called.
98 * It's mere presence means the MIPSPro compiler can accept some other code below.
100 * I recommend Buddism.
102 static inline Item::Iterator::Ptr workaroundIrixMIPSPro(const ItemSequenceCacheCell &cell)
104 return Item::Iterator::Ptr(new ListIterator<Item, Item::List>(cell.cachedItems));
108 template<bool IsForGlobal>
109 Item::Iterator::Ptr EvaluationCache<IsForGlobal>::evaluateSequence(const DynamicContext::Ptr &context) const
111 ItemSequenceCacheCell::Vector &cells = IsForGlobal ? context->globalItemSequenceCacheCells(m_varSlot) : context->itemSequenceCacheCells(m_varSlot);
112 ItemSequenceCacheCell &cell = cells[m_varSlot];
117 context->error(QtXmlPatterns::tr("Circularity detected"),
118 ReportContext::XTDE0640, this);
121 switch(cell.cacheState)
123 case ItemSequenceCacheCell::Full:
126 * We don't use makeListIterator() here because the MIPSPro compiler can't handle it.
128 return Item::Iterator::Ptr(new ListIterator<Item, Item::List>(cell.cachedItems));
130 case ItemSequenceCacheCell::Empty:
133 cell.sourceIterator = m_operand->evaluateSequence(IsForGlobal ? topFocusContext(context) : context);
134 cell.cacheState = ItemSequenceCacheCell::PartiallyPopulated;
137 case ItemSequenceCacheCell::PartiallyPopulated:
140 Q_ASSERT_X(cells.at(m_varSlot).sourceIterator, Q_FUNC_INFO,
141 "This trigger for a cache bug which hasn't yet been analyzed.");
142 return Item::Iterator::Ptr(new CachingIterator(cells, m_varSlot, IsForGlobal ? topFocusContext(context) : context));
146 Q_ASSERT_X(false, Q_FUNC_INFO, "This path is not supposed to be run.");
147 return Item::Iterator::Ptr();
152 template<bool IsForGlobal>
153 Expression::Ptr EvaluationCache<IsForGlobal>::typeCheck(const StaticContext::Ptr &context,
154 const SequenceType::Ptr &reqType)
156 /* It's important that we do the typeCheck() before checking for the use of local variables,
157 * because ExpressionVariableReference can reference an expression that is a local variable,
158 * so it must rewrite itself to it operand before, and it does that in EvaluationCache::typeCheck(). */
159 const Expression::Ptr me(SingleContainer::typeCheck(context, reqType));
161 OperandsIterator it(me, OperandsIterator::ExcludeParent);
162 Expression::Ptr next(it.next());
164 /* If our operand or any sub operand gets its value from a for-loop, we cannot
165 * cache it since then our cache would be filled -- but not invalidated -- on the
166 * first for-iteration. Consider this query:
170 * return ($v, $v)</tt>
172 * An evaluation cache is inserted for the two operands in the return clause. However,
173 * $i changes for each iteration so the cache can only be active on a per-iteration basis,
174 * it it's possible(which it isn't).
176 * This means that for some queries we don't cache what we really should, and hence evaluate
177 * in a sub-optimal way, since this DependsOnLocalVariable don't communicate whether it references
178 * a loop that affects us. The correct fix for this would be to let ForExpression reset the
179 * relevant caches only, but we don't know which ones that are. */
182 if(next->has(DependsOnLocalVariable))
183 return m_operand->typeCheck(context, reqType);
191 template<bool IsForGlobal>
192 Expression::Ptr EvaluationCache<IsForGlobal>::compress(const StaticContext::Ptr &context)
194 const Expression::Ptr me(SingleContainer::compress(context));
199 if(m_operand->is(IDRangeVariableReference))
202 if(m_declaration->usedByMany())
204 /* If it's only an atomic value an EvaluationCache is overkill. However,
205 * it's still needed for functions like fn:current-time() that must adhere to
206 * query stability. */
207 const Properties props(m_operand->properties());
209 if(props.testFlag(EvaluationCacheRedundant) ||
210 ((props.testFlag(IsEvaluated)) &&
211 !props.testFlag(DisableElimination) &&
212 CommonSequenceTypes::ExactlyOneAtomicType->matches(m_operand->staticType())))
221 /* If we're only used once, there's no need for an EvaluationCache. */
226 template<bool IsForGlobal>
227 SequenceType::Ptr EvaluationCache<IsForGlobal>::staticType() const
229 return m_operand->staticType();
232 template<bool IsForGlobal>
233 SequenceType::List EvaluationCache<IsForGlobal>::expectedOperandTypes() const
235 /* Remember that EvaluationCache::typeCheck() will be called from multiple locations,
236 * which potentially have different type requirements. For instance, one wants a node,
237 * and another requires atomization and casting.
239 * Returning ZeroOrMoreItems is safe here because staticType() returns the operand's type
240 * and therefore the convertors like Atomizer will be parents to us, and hence only affect
243 * ZeroOrMoreItems also make sense logically since we're actually only used where the
244 * variable references reference us. */
245 SequenceType::List result;
246 result.append(CommonSequenceTypes::ZeroOrMoreItems);
251 template<bool IsForGlobal>
252 Expression::Properties EvaluationCache<IsForGlobal>::properties() const
254 /* We cannot return the operand's properties unconditionally, because some
255 * doesn't hold for this Expression.
257 * However, some of the properties must propagate through, which are the ones being OR'd here.
259 return m_operand->properties() & (DisableElimination | IsEvaluated | DisableTypingDeduction);
262 template<bool IsForGlobal>
263 ExpressionVisitorResult::Ptr
264 EvaluationCache<IsForGlobal>::accept(const ExpressionVisitor::Ptr &visitor) const
266 return visitor->visit(this);
269 template<bool IsForGlobal>
270 const SourceLocationReflection *EvaluationCache<IsForGlobal>::actualReflection() const
272 return m_operand->actualReflection();