| 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 | // |
| 41 | // W A R N I N G |
| 42 | // ------------- |
| 43 | // |
| 44 | // This file is not part of the Qt API. It exists purely as an |
| 45 | // implementation detail. This header file may change from version to |
| 46 | // version without notice, or even be removed. |
| 47 | // |
| 48 | // We mean it. |
| 49 | // |
| 50 | |
| 51 | /** |
| 52 | * @file |
| 53 | * @short This file is included by qevaluationcache_p.h. |
| 54 | * If you need includes in this file, put them in qevaluationcache_p.h, outside of the namespace. |
| 55 | */ |
| 56 | |
| 57 | template<bool IsForGlobal> |
| 58 | EvaluationCache<IsForGlobal>::EvaluationCache(const Expression::Ptr &op, |
| 59 | const VariableDeclaration::Ptr &varDecl, |
| 60 | const VariableSlotID aSlot) : SingleContainer(op) |
| 61 | , m_declaration(varDecl) |
| 62 | , m_varSlot(aSlot) |
| 63 | { |
| 64 | Q_ASSERT(m_declaration); |
| 65 | Q_ASSERT(m_varSlot > -1); |
| 66 | } |
| 67 | |
| 68 | template<bool IsForGlobal> |
| 69 | DynamicContext::Ptr EvaluationCache<IsForGlobal>::topFocusContext(const DynamicContext::Ptr &context) |
| 70 | { |
| 71 | DynamicContext::Ptr result(context); |
| 72 | |
| 73 | while(true) |
| 74 | { |
| 75 | DynamicContext::Ptr candidate(result->previousContext()); |
| 76 | |
| 77 | /* We want the top focus, not GenericDynamicContext. */ |
| 78 | if(candidate && candidate->focusIterator()) |
| 79 | result = candidate; |
| 80 | else |
| 81 | return result; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | template<bool IsForGlobal> |
| 86 | Item EvaluationCache<IsForGlobal>::evaluateSingleton(const DynamicContext::Ptr &context) const |
| 87 | { |
| 88 | ItemCacheCell &cell = IsForGlobal ? context->globalItemCacheCell(slot: m_varSlot) : context->itemCacheCell(slot: m_varSlot); |
| 89 | |
| 90 | if(cell.cacheState == ItemCacheCell::Full) |
| 91 | return cell.cachedItem; |
| 92 | else |
| 93 | { |
| 94 | Q_ASSERT(cell.cacheState == ItemCacheCell::Empty); |
| 95 | cell.cachedItem = m_operand->evaluateSingleton(context: IsForGlobal ? topFocusContext(context) : context); |
| 96 | cell.cacheState = ItemCacheCell::Full; |
| 97 | return cell.cachedItem; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | #if defined(Q_OS_IRIX) && defined(Q_CC_MIPS) |
| 102 | /** |
| 103 | * @short Compile workaround for MIPSPro on IRIX. |
| 104 | * |
| 105 | * This function is never called. |
| 106 | * |
| 107 | * It's mere presence means the MIPSPro compiler can accept some other code below. |
| 108 | * |
| 109 | * I recommend Buddism. |
| 110 | */ |
| 111 | static inline Item::Iterator::Ptr workaroundIrixMIPSPro(const ItemSequenceCacheCell &cell) |
| 112 | { |
| 113 | return Item::Iterator::Ptr(new ListIterator<Item, Item::List>(cell.cachedItems)); |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | template<bool IsForGlobal> |
| 118 | Item::Iterator::Ptr EvaluationCache<IsForGlobal>::evaluateSequence(const DynamicContext::Ptr &context) const |
| 119 | { |
| 120 | ItemSequenceCacheCell::Vector &cells = IsForGlobal ? context->globalItemSequenceCacheCells(slot: m_varSlot) : context->itemSequenceCacheCells(slot: m_varSlot); |
| 121 | ItemSequenceCacheCell &cell = cells[m_varSlot]; |
| 122 | |
| 123 | |
| 124 | if(cell.inUse) |
| 125 | { |
| 126 | context->error(QtXmlPatterns::tr(sourceText: "Circularity detected" ), |
| 127 | ReportContext::XTDE0640, this); |
| 128 | } |
| 129 | |
| 130 | switch(cell.cacheState) |
| 131 | { |
| 132 | case ItemSequenceCacheCell::Full: |
| 133 | { |
| 134 | /** |
| 135 | * We don't use makeListIterator() here because the MIPSPro compiler can't handle it. |
| 136 | */ |
| 137 | return Item::Iterator::Ptr(new ListIterator<Item, Item::List>(cell.cachedItems)); |
| 138 | } |
| 139 | case ItemSequenceCacheCell::Empty: |
| 140 | { |
| 141 | cell.inUse = true; |
| 142 | cell.sourceIterator = m_operand->evaluateSequence(context: IsForGlobal ? topFocusContext(context) : context); |
| 143 | cell.cacheState = ItemSequenceCacheCell::PartiallyPopulated; |
| 144 | Q_FALLTHROUGH(); |
| 145 | } |
| 146 | case ItemSequenceCacheCell::PartiallyPopulated: |
| 147 | { |
| 148 | cell.inUse = false; |
| 149 | Q_ASSERT_X(cells.at(m_varSlot).sourceIterator, Q_FUNC_INFO, |
| 150 | "This trigger for a cache bug which hasn't yet been analyzed." ); |
| 151 | return Item::Iterator::Ptr(new CachingIterator(cells, m_varSlot, IsForGlobal ? topFocusContext(context) : context)); |
| 152 | } |
| 153 | default: |
| 154 | { |
| 155 | Q_ASSERT_X(false, Q_FUNC_INFO, "This path is not supposed to be run." ); |
| 156 | return Item::Iterator::Ptr(); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | template<bool IsForGlobal> |
| 162 | Expression::Ptr EvaluationCache<IsForGlobal>::typeCheck(const StaticContext::Ptr &context, |
| 163 | const SequenceType::Ptr &reqType) |
| 164 | { |
| 165 | /* It's important that we do the typeCheck() before checking for the use of local variables, |
| 166 | * because ExpressionVariableReference can reference an expression that is a local variable, |
| 167 | * so it must rewrite itself to it operand before, and it does that in EvaluationCache::typeCheck(). */ |
| 168 | const Expression::Ptr me(SingleContainer::typeCheck(context, reqType)); |
| 169 | |
| 170 | OperandsIterator it(me, OperandsIterator::ExcludeParent); |
| 171 | Expression::Ptr next(it.next()); |
| 172 | |
| 173 | /* If our operand or any sub operand gets its value from a for-loop, we cannot |
| 174 | * cache it since then our cache would be filled -- but not invalidated -- on the |
| 175 | * first for-iteration. Consider this query: |
| 176 | * |
| 177 | * <tt>for $i in expr |
| 178 | * let $v := $i/p |
| 179 | * return ($v, $v)</tt> |
| 180 | * |
| 181 | * An evaluation cache is inserted for the two operands in the return clause. However, |
| 182 | * $i changes for each iteration so the cache can only be active on a per-iteration basis, |
| 183 | * it it's possible(which it isn't). |
| 184 | * |
| 185 | * This means that for some queries we don't cache what we really should, and hence evaluate |
| 186 | * in a sub-optimal way, since this DependsOnLocalVariable don't communicate whether it references |
| 187 | * a loop that affects us. The correct fix for this would be to let ForExpression reset the |
| 188 | * relevant caches only, but we don't know which ones that are. */ |
| 189 | while(next) |
| 190 | { |
| 191 | if(next->has(prop: DependsOnLocalVariable)) |
| 192 | return m_operand->typeCheck(context, reqType); |
| 193 | |
| 194 | next = it.next(); |
| 195 | } |
| 196 | |
| 197 | return me; |
| 198 | } |
| 199 | |
| 200 | template<bool IsForGlobal> |
| 201 | Expression::Ptr EvaluationCache<IsForGlobal>::compress(const StaticContext::Ptr &context) |
| 202 | { |
| 203 | const Expression::Ptr me(SingleContainer::compress(context)); |
| 204 | |
| 205 | if(me != this) |
| 206 | return me; |
| 207 | |
| 208 | if(m_operand->is(i: IDRangeVariableReference)) |
| 209 | return m_operand; |
| 210 | |
| 211 | if(m_declaration->usedByMany()) |
| 212 | { |
| 213 | /* If it's only an atomic value an EvaluationCache is overkill. However, |
| 214 | * it's still needed for functions like fn:current-time() that must adhere to |
| 215 | * query stability. */ |
| 216 | const Properties props(m_operand->properties()); |
| 217 | |
| 218 | if(props.testFlag(flag: EvaluationCacheRedundant) || |
| 219 | ((props.testFlag(flag: IsEvaluated)) && |
| 220 | !props.testFlag(flag: DisableElimination) && |
| 221 | CommonSequenceTypes::ExactlyOneAtomicType->matches(other: m_operand->staticType()))) |
| 222 | { |
| 223 | return m_operand; |
| 224 | } |
| 225 | else |
| 226 | return me; |
| 227 | } |
| 228 | else |
| 229 | { |
| 230 | /* If we're only used once, there's no need for an EvaluationCache. */ |
| 231 | return m_operand; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | template<bool IsForGlobal> |
| 236 | SequenceType::Ptr EvaluationCache<IsForGlobal>::staticType() const |
| 237 | { |
| 238 | return m_operand->staticType(); |
| 239 | } |
| 240 | |
| 241 | template<bool IsForGlobal> |
| 242 | SequenceType::List EvaluationCache<IsForGlobal>::expectedOperandTypes() const |
| 243 | { |
| 244 | /* Remember that EvaluationCache::typeCheck() will be called from multiple locations, |
| 245 | * which potentially have different type requirements. For instance, one wants a node, |
| 246 | * and another requires atomization and casting. |
| 247 | * |
| 248 | * Returning ZeroOrMoreItems is safe here because staticType() returns the operand's type |
| 249 | * and therefore the convertors like Atomizer will be parents to us, and hence only affect |
| 250 | * the relevant path. |
| 251 | * |
| 252 | * ZeroOrMoreItems also make sense logically since we're actually only used where the |
| 253 | * variable references reference us. */ |
| 254 | SequenceType::List result; |
| 255 | result.append(t: CommonSequenceTypes::ZeroOrMoreItems); |
| 256 | |
| 257 | return result; |
| 258 | } |
| 259 | |
| 260 | template<bool IsForGlobal> |
| 261 | Expression::Properties EvaluationCache<IsForGlobal>::properties() const |
| 262 | { |
| 263 | /* We cannot return the operand's properties unconditionally, because some |
| 264 | * doesn't hold for this Expression. |
| 265 | * |
| 266 | * However, some of the properties must propagate through, which are the ones being OR'd here. |
| 267 | */ |
| 268 | return m_operand->properties() & (DisableElimination | IsEvaluated | DisableTypingDeduction); |
| 269 | } |
| 270 | |
| 271 | template<bool IsForGlobal> |
| 272 | ExpressionVisitorResult::Ptr |
| 273 | EvaluationCache<IsForGlobal>::accept(const ExpressionVisitor::Ptr &visitor) const |
| 274 | { |
| 275 | return visitor->visit(this); |
| 276 | } |
| 277 | |
| 278 | template<bool IsForGlobal> |
| 279 | const SourceLocationReflection *EvaluationCache<IsForGlobal>::actualReflection() const |
| 280 | { |
| 281 | return m_operand->actualReflection(); |
| 282 | } |
| 283 | |
| 284 | |