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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Assertions.h"
#include "txXPathOptimizer.h"
#include "txExprResult.h"
#include "nsAtom.h"
#include "nsGkAtoms.h"
#include "txXPathNode.h"
#include "txExpr.h"
#include "txIXPathContext.h"
using mozilla::UniquePtr;
using mozilla::Unused;
class txEarlyEvalContext : public txIEvalContext {
public:
explicit txEarlyEvalContext(txResultRecycler* aRecycler)
: mRecycler(aRecycler) {}
// txIEvalContext
nsresult getVariable(int32_t aNamespace, nsAtom* aLName,
txAExprResult*& aResult) override {
MOZ_CRASH("shouldn't depend on this context");
}
nsresult isStripSpaceAllowed(const txXPathNode& aNode,
bool& aAllowed) override {
MOZ_CRASH("shouldn't depend on this context");
}
void* getPrivateContext() override {
MOZ_CRASH("shouldn't depend on this context");
}
txResultRecycler* recycler() override { return mRecycler; }
void receiveError(const nsAString& aMsg, nsresult aRes) override {}
const txXPathNode& getContextNode() override {
MOZ_CRASH("shouldn't depend on this context");
}
uint32_t size() override { MOZ_CRASH("shouldn't depend on this context"); }
uint32_t position() override {
MOZ_CRASH("shouldn't depend on this context");
}
private:
txResultRecycler* mRecycler;
};
nsresult txXPathOptimizer::optimize(Expr* aInExpr, Expr** aOutExpr) {
*aOutExpr = nullptr;
nsresult rv = NS_OK;
// First check if the expression will produce the same result
// under any context.
Expr::ExprType exprType = aInExpr->getType();
if (exprType != Expr::LITERAL_EXPR &&
!aInExpr->isSensitiveTo(Expr::ANY_CONTEXT)) {
RefPtr<txResultRecycler> recycler = new txResultRecycler;
txEarlyEvalContext context(recycler);
RefPtr<txAExprResult> exprRes;
// Don't throw if this fails since it could be that the expression
// is or contains an error-expression.
rv = aInExpr->evaluate(&context, getter_AddRefs(exprRes));
if (NS_SUCCEEDED(rv)) {
*aOutExpr = new txLiteralExpr(exprRes);
}
return NS_OK;
}
// Then optimize sub expressions
uint32_t i = 0;
Expr* subExpr;
while ((subExpr = aInExpr->getSubExprAt(i))) {
Expr* newExpr = nullptr;
rv = optimize(subExpr, &newExpr);
NS_ENSURE_SUCCESS(rv, rv);
if (newExpr) {
delete subExpr;
aInExpr->setSubExprAt(i, newExpr);
}
++i;
}
// Finally see if current expression can be optimized
switch (exprType) {
case Expr::LOCATIONSTEP_EXPR:
return optimizeStep(aInExpr, aOutExpr);
case Expr::PATH_EXPR:
return optimizePath(aInExpr, aOutExpr);
case Expr::UNION_EXPR:
return optimizeUnion(aInExpr, aOutExpr);
default:
break;
}
return NS_OK;
}
nsresult txXPathOptimizer::optimizeStep(Expr* aInExpr, Expr** aOutExpr) {
LocationStep* step = static_cast<LocationStep*>(aInExpr);
if (step->getAxisIdentifier() == LocationStep::ATTRIBUTE_AXIS) {
// Test for @foo type steps.
txNameTest* nameTest = nullptr;
if (!step->getSubExprAt(0) &&
step->getNodeTest()->getType() == txNameTest::NAME_TEST &&
(nameTest = static_cast<txNameTest*>(step->getNodeTest()))
->mLocalName != nsGkAtoms::_asterisk) {
*aOutExpr = new txNamedAttributeStep(
nameTest->mNamespace, nameTest->mPrefix, nameTest->mLocalName);
return NS_OK; // return since we no longer have a step-object.
}
}
// Test for predicates that can be combined into the nodetest
Expr* pred;
while ((pred = step->getSubExprAt(0)) &&
!pred->canReturnType(Expr::NUMBER_RESULT) &&
!pred->isSensitiveTo(Expr::NODESET_CONTEXT)) {
txNodeTest* predTest = new txPredicatedNodeTest(step->getNodeTest(), pred);
step->dropFirst();
step->setNodeTest(predTest);
}
return NS_OK;
}
nsresult txXPathOptimizer::optimizePath(Expr* aInExpr, Expr** aOutExpr) {
PathExpr* path = static_cast<PathExpr*>(aInExpr);
uint32_t i;
Expr* subExpr;
// look for steps like "//foo" that can be turned into "/descendant::foo"
// and "//." that can be turned into "/descendant-or-self::node()"
for (i = 0; (subExpr = path->getSubExprAt(i)); ++i) {
if (path->getPathOpAt(i) == PathExpr::DESCENDANT_OP &&
subExpr->getType() == Expr::LOCATIONSTEP_EXPR &&
!subExpr->getSubExprAt(0)) {
LocationStep* step = static_cast<LocationStep*>(subExpr);
if (step->getAxisIdentifier() == LocationStep::CHILD_AXIS) {
step->setAxisIdentifier(LocationStep::DESCENDANT_AXIS);
path->setPathOpAt(i, PathExpr::RELATIVE_OP);
} else if (step->getAxisIdentifier() == LocationStep::SELF_AXIS) {
step->setAxisIdentifier(LocationStep::DESCENDANT_OR_SELF_AXIS);
path->setPathOpAt(i, PathExpr::RELATIVE_OP);
}
}
}
// look for expressions that start with a "./"
subExpr = path->getSubExprAt(0);
LocationStep* step;
if (subExpr->getType() == Expr::LOCATIONSTEP_EXPR && path->getSubExprAt(1) &&
path->getPathOpAt(1) != PathExpr::DESCENDANT_OP) {
step = static_cast<LocationStep*>(subExpr);
if (step->getAxisIdentifier() == LocationStep::SELF_AXIS &&
!step->getSubExprAt(0)) {
txNodeTest* test = step->getNodeTest();
if (test->getType() == txNodeTest::NODETYPE_TEST &&
(static_cast<txNodeTypeTest*>(test))->getNodeTestType() ==
txNodeTypeTest::NODE_TYPE) {
// We have a '.' as first step followed by a single '/'.
// Check if there are only two steps. If so, return the second
// as resulting expression.
if (!path->getSubExprAt(2)) {
*aOutExpr = path->getSubExprAt(1);
path->setSubExprAt(1, nullptr);
return NS_OK;
}
// Just delete the '.' step and leave the rest of the PathExpr
path->deleteExprAt(0);
}
}
}
return NS_OK;
}
nsresult txXPathOptimizer::optimizeUnion(Expr* aInExpr, Expr** aOutExpr) {
UnionExpr* uni = static_cast<UnionExpr*>(aInExpr);
// Check for expressions like "foo | bar" and
// "descendant::foo | descendant::bar"
nsresult rv;
uint32_t current;
Expr* subExpr;
for (current = 0; (subExpr = uni->getSubExprAt(current)); ++current) {
if (subExpr->getType() != Expr::LOCATIONSTEP_EXPR ||
subExpr->getSubExprAt(0)) {
continue;
}
LocationStep* currentStep = static_cast<LocationStep*>(subExpr);
LocationStep::LocationStepType axis = currentStep->getAxisIdentifier();
txUnionNodeTest* unionTest = nullptr;
// Check if there are any other steps with the same axis and merge
// them with currentStep
uint32_t i;
for (i = current + 1; (subExpr = uni->getSubExprAt(i)); ++i) {
if (subExpr->getType() != Expr::LOCATIONSTEP_EXPR ||
subExpr->getSubExprAt(0)) {
continue;
}
LocationStep* step = static_cast<LocationStep*>(subExpr);
if (step->getAxisIdentifier() != axis) {
continue;
}
// Create a txUnionNodeTest if needed
if (!unionTest) {
UniquePtr<txNodeTest> owner(unionTest = new txUnionNodeTest);
rv = unionTest->addNodeTest(currentStep->getNodeTest());
NS_ENSURE_SUCCESS(rv, rv);
currentStep->setNodeTest(unionTest);
Unused << owner.release();
}
// Merge the nodetest into the union
rv = unionTest->addNodeTest(step->getNodeTest());
NS_ENSURE_SUCCESS(rv, rv);
step->setNodeTest(nullptr);
// Remove the step from the UnionExpr
uni->deleteExprAt(i);
--i;
}
// Check if all expressions were merged into a single step. If so,
// return the step as the new expression.
if (unionTest && current == 0 && !uni->getSubExprAt(1)) {
// Make sure the step doesn't get deleted when the UnionExpr is
uni->setSubExprAt(0, nullptr);
*aOutExpr = currentStep;
// Return right away since we no longer have a union
return NS_OK;
}
}
return NS_OK;
}
|