1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtXmlPatterns module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qboolean_p.h"
#include "qcommonvalues_p.h"
#include "qemptysequence_p.h"
#include "qliteral_p.h"
#include "qliteralsequence_p.h"
#include "qoperandsiterator_p.h"
#include "qoptimizerframework_p.h"
#include "qstaticfocuscontext_p.h"
#include "qtypechecker_p.h"
#include "qexpression_p.h"
QT_BEGIN_NAMESPACE
using namespace QPatternist;
Expression::~Expression()
{
}
StaticContext::Ptr Expression::finalizeStaticContext(const StaticContext::Ptr &context) const
{
Q_ASSERT(context);
const ItemType::Ptr focusType(newFocusType());
Q_ASSERT(focusType);
return StaticContext::Ptr(new StaticFocusContext(focusType, context));
}
Expression::Ptr Expression::typeCheck(const StaticContext::Ptr &context,
const SequenceType::Ptr &reqType)
{
Q_ASSERT(reqType);
typeCheckOperands(context);
return TypeChecker::applyFunctionConversion(Expression::Ptr(this), reqType, context);
}
void Expression::typeCheckOperands(const StaticContext::Ptr &context)
{
const Expression::List ops(operands());
/* Check if this expression has any operands at all. */
if(ops.isEmpty())
return; /* We're done, early exit. */
const SequenceType::List opTypes(expectedOperandTypes());
Expression::List result;
/* If we create a focus, we handle the last one specially, so avoid it in the loop. */
const bool createsFocus = has(CreatesFocusForLast);
const SequenceType::List::const_iterator typeEnd(createsFocus ? --opTypes.constEnd()
: opTypes.constEnd());
const Expression::List::const_iterator end(createsFocus ? --ops.constEnd()
: ops.constEnd());
SequenceType::List::const_iterator reqType(opTypes.constBegin());
SequenceType::Ptr t(*reqType);
// TODO we assign twice to t here(also below in loop) when ops.size() > 1
Expression::List::const_iterator it(ops.constBegin());
for(; it != end; ++it)
{
/* This ensures that the last expectedOperandType stays, and is
* used for all other operands. This is used for expressions that
* have an infinite amount of operands, such as the concat() function. */
if(reqType != typeEnd)
{
t = *reqType;
++reqType;
}
/* Let the child & its children typecheck. */
result.append((*it)->typeCheck(context, t));
}
if(createsFocus)
{
const StaticContext::Ptr newContext(finalizeStaticContext(context));
result.append(ops.last()->typeCheck(newContext, opTypes.last()));
}
setOperands(result);
}
Expression::Ptr Expression::invokeOptimizers(const Expression::Ptr &expr,
const StaticContext::Ptr &context)
{
Q_ASSERT(expr);
const OptimizationPass::List opts(expr->optimizationPasses());
if(opts.isEmpty()) /* Early exit. */
{
return expr;
}
const OptimizationPass::List::const_iterator passEnd(opts.constEnd());
OptimizationPass::List::const_iterator passIt(opts.constBegin());
for(; passIt != passEnd; ++passIt) /* Invoke each optimization pass. */
{
const OptimizationPass::Ptr pass(*passIt); /* Alias, for readability. */
OptimizationPass::ExpressionMarker sourceMarker(pass->sourceExpression);
if(pass->startIdentifier && !pass->startIdentifier->matches(expr))
{
/* This pass specified a start identifier and it did
* not match -- let's try the next OptimizationPass. */
continue;
}
ExpressionIdentifier::List::const_iterator idIt(pass->operandIdentifiers.constBegin());
const Expression::List ops(expr->operands());
const Expression::List::const_iterator opEnd(ops.constEnd());
Expression::List::const_iterator opIt(ops.constBegin());
switch(pass->operandsMatchMethod)
{
case OptimizationPass::Sequential:
{
for(; opIt != opEnd; ++opIt)
{
const Expression::Ptr operand(*opIt); /* Alias, for readability. */
const ExpressionIdentifier::Ptr opIdentifier(*idIt); /* Alias, for readability. */
if(opIdentifier && !opIdentifier->matches(operand))
{
break;
}
++idIt;
}
if(opIt == opEnd)
break; /* All operands matched, so this pass matched. */
else
{
/* The loop above did not finish which means all operands did not match.
Therefore, this OptimizationPass did not match -- let's try the next one. */
continue;
}
}
case OptimizationPass::AnyOrder:
{
Q_ASSERT_X(ops.count() == 2, Q_FUNC_INFO,
"AnyOrder is currently only supported for Expressions with two operands.");
if(pass->operandIdentifiers.first()->matches(ops.first()) &&
pass->operandIdentifiers.last()->matches(ops.last()))
{
break;
}
else if(pass->operandIdentifiers.first()->matches(ops.last()) &&
pass->operandIdentifiers.last()->matches(ops.first()))
{
sourceMarker.first() = 1;
sourceMarker[1] = 0;
break; /* This pass matched. */
}
else
continue; /* This pass didn't match, let's loop through the next pass. */
}
}
/* Figure out the source Expression, if any. */
Expression::List operands;
Expression::Ptr sourceExpr;
if(!sourceMarker.isEmpty())
{
const OptimizationPass::ExpressionMarker::const_iterator mEnd(sourceMarker.constEnd());
OptimizationPass::ExpressionMarker::const_iterator mIt(sourceMarker.constBegin());
sourceExpr = expr;
for(; mIt != mEnd; ++mIt)
{
Q_ASSERT(*mIt >= 0);
sourceExpr = sourceExpr->operands().at(*mIt);
}
operands.append(sourceExpr);
}
if(operands.isEmpty())
{
Q_ASSERT(pass->resultCreator);
return pass->resultCreator->create(Expression::List(), context, expr.data())->compress(context);
}
else if(pass->resultCreator)
return pass->resultCreator->create(operands, context, expr.data())->compress(context);
else
{
return sourceExpr;
}
}
return expr;
}
Expression::Ptr Expression::compress(const StaticContext::Ptr &context)
{
if(!compressOperands(context))
{
/* At least one of the operands cannot be evaluated at compile, so
* 'this' Expression cannot const fold. */
return invokeOptimizers(Expression::Ptr(this), context);
}
Expression::Ptr retval;
if(hasDependency(DisableElimination))
retval = Expression::Ptr(this);
else
retval = constantPropagate(context);
return invokeOptimizers(retval, context);
}
Expression::Ptr Expression::constantPropagate(const StaticContext::Ptr &context) const
{
Q_ASSERT(context);
/* Optimization: We rewrite literals to literals here, which is pointless.
* Maybe we should have a property which says "doesn't disable elimination
* but don't eliminate me." */
if(staticType()->cardinality().allowsMany())
{
Item::Iterator::Ptr it(evaluateSequence(context->dynamicContext()));
Item::List result;
Item item(it->next());
while(item)
{
result.append(item);
item = it->next();
}
switch(result.count())
{
case 0:
return EmptySequence::create(this, context);
case 1:
return rewrite(Expression::Ptr(new Literal(result.first())), context);
default:
return rewrite(Expression::Ptr(new LiteralSequence(result)), context);
}
}
else
{
const Item item(evaluateSingleton(context->dynamicContext()));
if(item)
return rewrite(Expression::Ptr(new Literal(item)), context);
else
return EmptySequence::create(this, context);
}
}
Item::Iterator::Ptr Expression::evaluateSequence(const DynamicContext::Ptr &context) const
{
const Item item(evaluateSingleton(context));
if(item)
return makeSingletonIterator(item);
else
return CommonValues::emptyIterator;
}
Item Expression::evaluateSingleton(const DynamicContext::Ptr &context) const
{
return Boolean::fromValue(evaluateEBV(context));
}
bool Expression::evaluateEBV(const DynamicContext::Ptr &context) const
{
return Boolean::evaluateEBV(evaluateSequence(context), context);
}
void Expression::evaluateToSequenceReceiver(const DynamicContext::Ptr &context) const
{
QAbstractXmlReceiver *const receiver = context->outputReceiver();
const Item::Iterator::Ptr it(evaluateSequence(context));
Item next(it->next());
while(next)
{
receiver->item(next);
next = it->next();
}
}
ItemType::Ptr Expression::expectedContextItemType() const
{
Q_ASSERT_X(false, Q_FUNC_INFO,
"expectedContextItemType() must be overridden when RequiresContextItem is set.");
return ItemType::Ptr();
}
Expression::Properties Expression::properties() const
{
return Properties();
}
Expression::Properties Expression::dependencies() const
{
OperandsIterator it(Ptr(const_cast<Expression *>(this)), OperandsIterator::ExcludeParent);
Expression::Ptr next(it.next());
Properties dependencies(properties());
while(next)
{
dependencies |= next->dependencies();
next = it.next();
}
return dependencies & (Expression::RequiresFocus | Expression::IsEvaluated | Expression::DisableElimination);
}
void Expression::announceFocusType(const ItemType::Ptr &itemType)
{
const Expression::List ops(operands());
const int len = ops.count();
for(int i = 0; i < len; ++i)
ops.at(i)->announceFocusType(itemType);
}
Expression::Properties Expression::deepProperties() const
{
Properties props(properties());
const Expression::List ops(operands());
const int len = ops.count();
for(int i = 0; i < len; ++i)
props |= ops.at(i)->deepProperties();
return props;
}
Expression::ID Expression::id() const
{
return IDIgnorableExpression;
}
OptimizationPass::List Expression::optimizationPasses() const
{
return OptimizationPass::List();
}
ItemType::Ptr Expression::newFocusType() const
{
Q_ASSERT_X(false, Q_FUNC_INFO,
"This function must be overridden when CreatesFocusForLast is set.");
return ItemType::Ptr();
}
const SourceLocationReflection *Expression::actualReflection() const
{
return this;
}
QString Expression::description() const
{
return QString::fromLatin1("Expression, id: %1").arg(QString::number(id()));
}
PatternPriority Expression::patternPriority() const
{
return 0.5;
}
QT_END_NAMESPACE
|