source: trunk/src/xmlpatterns/expr/qevaluationcache.cpp@ 395

Last change on this file since 395 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 10.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/**
43 * @file
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.
46 */
47
48template<bool IsForGlobal>
49EvaluationCache<IsForGlobal>::EvaluationCache(const Expression::Ptr &op,
50 const VariableDeclaration::Ptr &varDecl,
51 const VariableSlotID aSlot) : SingleContainer(op)
52 , m_declaration(varDecl)
53 , m_varSlot(aSlot)
54{
55 Q_ASSERT(m_declaration);
56 Q_ASSERT(m_varSlot > -1);
57}
58
59template<bool IsForGlobal>
60DynamicContext::Ptr EvaluationCache<IsForGlobal>::topFocusContext(const DynamicContext::Ptr &context)
61{
62 DynamicContext::Ptr result(context);
63
64 while(true)
65 {
66 DynamicContext::Ptr candidate(result->previousContext());
67
68 /* We want the top focus, not GenericDynamicContext. */
69 if(candidate && candidate->focusIterator())
70 result = candidate;
71 else
72 return result;
73 }
74}
75
76template<bool IsForGlobal>
77Item EvaluationCache<IsForGlobal>::evaluateSingleton(const DynamicContext::Ptr &context) const
78{
79 ItemCacheCell &cell = IsForGlobal ? context->globalItemCacheCell(m_varSlot) : context->itemCacheCell(m_varSlot);
80
81 if(cell.cacheState == ItemCacheCell::Full)
82 return cell.cachedItem;
83 else
84 {
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;
89 }
90}
91
92#if defined(Q_OS_IRIX) && defined(Q_CC_MIPS)
93/**
94 * @short Compile workaround for MIPSPro on IRIX.
95 *
96 * This function is never called.
97 *
98 * It's mere presence means the MIPSPro compiler can accept some other code below.
99 *
100 * I recommend Buddism.
101 */
102static inline Item::Iterator::Ptr workaroundIrixMIPSPro(const ItemSequenceCacheCell &cell)
103{
104 return Item::Iterator::Ptr(new ListIterator<Item, Item::List>(cell.cachedItems));
105}
106#endif
107
108template<bool IsForGlobal>
109Item::Iterator::Ptr EvaluationCache<IsForGlobal>::evaluateSequence(const DynamicContext::Ptr &context) const
110{
111 ItemSequenceCacheCell::Vector &cells = IsForGlobal ? context->globalItemSequenceCacheCells(m_varSlot) : context->itemSequenceCacheCells(m_varSlot);
112 ItemSequenceCacheCell &cell = cells[m_varSlot];
113
114
115 if(cell.inUse)
116 {
117 context->error(QtXmlPatterns::tr("Circularity detected"),
118 ReportContext::XTDE0640, this);
119 }
120
121 switch(cell.cacheState)
122 {
123 case ItemSequenceCacheCell::Full:
124 {
125 /**
126 * We don't use makeListIterator() here because the MIPSPro compiler can't handle it.
127 */
128 return Item::Iterator::Ptr(new ListIterator<Item, Item::List>(cell.cachedItems));
129 }
130 case ItemSequenceCacheCell::Empty:
131 {
132 cell.inUse = true;
133 cell.sourceIterator = m_operand->evaluateSequence(IsForGlobal ? topFocusContext(context) : context);
134 cell.cacheState = ItemSequenceCacheCell::PartiallyPopulated;
135 /* Fallthrough. */
136 }
137 case ItemSequenceCacheCell::PartiallyPopulated:
138 {
139 cell.inUse = false;
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));
143 }
144 default:
145 {
146 Q_ASSERT_X(false, Q_FUNC_INFO, "This path is not supposed to be run.");
147 return Item::Iterator::Ptr();
148 }
149 }
150}
151
152template<bool IsForGlobal>
153Expression::Ptr EvaluationCache<IsForGlobal>::typeCheck(const StaticContext::Ptr &context,
154 const SequenceType::Ptr &reqType)
155{
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));
160
161 OperandsIterator it(me, OperandsIterator::ExcludeParent);
162 Expression::Ptr next(it.next());
163
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:
167 *
168 * <tt>for $i in expr
169 * let $v := $i/p
170 * return ($v, $v)</tt>
171 *
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).
175 *
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. */
180 while(next)
181 {