/src/mozilla-central/dom/xslt/xpath/txXPathOptimizer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | |
6 | | #include "mozilla/Assertions.h" |
7 | | #include "txXPathOptimizer.h" |
8 | | #include "txExprResult.h" |
9 | | #include "nsAtom.h" |
10 | | #include "nsGkAtoms.h" |
11 | | #include "txXPathNode.h" |
12 | | #include "txExpr.h" |
13 | | #include "txIXPathContext.h" |
14 | | |
15 | | class txEarlyEvalContext : public txIEvalContext |
16 | | { |
17 | | public: |
18 | | explicit txEarlyEvalContext(txResultRecycler* aRecycler) |
19 | | : mRecycler(aRecycler) |
20 | 0 | { |
21 | 0 | } |
22 | | |
23 | | // txIEvalContext |
24 | | nsresult getVariable(int32_t aNamespace, nsAtom* aLName, |
25 | | txAExprResult*& aResult) override |
26 | 0 | { |
27 | 0 | MOZ_CRASH("shouldn't depend on this context"); |
28 | 0 | } |
29 | | nsresult isStripSpaceAllowed(const txXPathNode& aNode, |
30 | | bool& aAllowed) override |
31 | 0 | { |
32 | 0 | MOZ_CRASH("shouldn't depend on this context"); |
33 | 0 | } |
34 | | void* getPrivateContext() override |
35 | 0 | { |
36 | 0 | MOZ_CRASH("shouldn't depend on this context"); |
37 | 0 | } |
38 | | txResultRecycler* recycler() override |
39 | 0 | { |
40 | 0 | return mRecycler; |
41 | 0 | } |
42 | | void receiveError(const nsAString& aMsg, nsresult aRes) override |
43 | 0 | { |
44 | 0 | } |
45 | | const txXPathNode& getContextNode() override |
46 | 0 | { |
47 | 0 | MOZ_CRASH("shouldn't depend on this context"); |
48 | 0 | } |
49 | | uint32_t size() override |
50 | 0 | { |
51 | 0 | MOZ_CRASH("shouldn't depend on this context"); |
52 | 0 | } |
53 | | uint32_t position() override |
54 | 0 | { |
55 | 0 | MOZ_CRASH("shouldn't depend on this context"); |
56 | 0 | } |
57 | | |
58 | | private: |
59 | | txResultRecycler* mRecycler; |
60 | | }; |
61 | | |
62 | | nsresult |
63 | | txXPathOptimizer::optimize(Expr* aInExpr, Expr** aOutExpr) |
64 | 0 | { |
65 | 0 | *aOutExpr = nullptr; |
66 | 0 | nsresult rv = NS_OK; |
67 | 0 |
|
68 | 0 | // First check if the expression will produce the same result |
69 | 0 | // under any context. |
70 | 0 | Expr::ExprType exprType = aInExpr->getType(); |
71 | 0 | if (exprType != Expr::LITERAL_EXPR && |
72 | 0 | !aInExpr->isSensitiveTo(Expr::ANY_CONTEXT)) { |
73 | 0 | RefPtr<txResultRecycler> recycler = new txResultRecycler; |
74 | 0 | txEarlyEvalContext context(recycler); |
75 | 0 | RefPtr<txAExprResult> exprRes; |
76 | 0 |
|
77 | 0 | // Don't throw if this fails since it could be that the expression |
78 | 0 | // is or contains an error-expression. |
79 | 0 | rv = aInExpr->evaluate(&context, getter_AddRefs(exprRes)); |
80 | 0 | if (NS_SUCCEEDED(rv)) { |
81 | 0 | *aOutExpr = new txLiteralExpr(exprRes); |
82 | 0 | } |
83 | 0 |
|
84 | 0 | return NS_OK; |
85 | 0 | } |
86 | 0 |
|
87 | 0 | // Then optimize sub expressions |
88 | 0 | uint32_t i = 0; |
89 | 0 | Expr* subExpr; |
90 | 0 | while ((subExpr = aInExpr->getSubExprAt(i))) { |
91 | 0 | Expr* newExpr = nullptr; |
92 | 0 | rv = optimize(subExpr, &newExpr); |
93 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
94 | 0 | if (newExpr) { |
95 | 0 | delete subExpr; |
96 | 0 | aInExpr->setSubExprAt(i, newExpr); |
97 | 0 | } |
98 | 0 |
|
99 | 0 | ++i; |
100 | 0 | } |
101 | 0 |
|
102 | 0 | // Finally see if current expression can be optimized |
103 | 0 | switch (exprType) { |
104 | 0 | case Expr::LOCATIONSTEP_EXPR: |
105 | 0 | return optimizeStep(aInExpr, aOutExpr); |
106 | 0 |
|
107 | 0 | case Expr::PATH_EXPR: |
108 | 0 | return optimizePath(aInExpr, aOutExpr); |
109 | 0 |
|
110 | 0 | case Expr::UNION_EXPR: |
111 | 0 | return optimizeUnion(aInExpr, aOutExpr); |
112 | 0 |
|
113 | 0 | default: |
114 | 0 | break; |
115 | 0 | } |
116 | 0 | |
117 | 0 | return NS_OK; |
118 | 0 | } |
119 | | |
120 | | nsresult |
121 | | txXPathOptimizer::optimizeStep(Expr* aInExpr, Expr** aOutExpr) |
122 | 0 | { |
123 | 0 | LocationStep* step = static_cast<LocationStep*>(aInExpr); |
124 | 0 |
|
125 | 0 | if (step->getAxisIdentifier() == LocationStep::ATTRIBUTE_AXIS) { |
126 | 0 | // Test for @foo type steps. |
127 | 0 | txNameTest* nameTest = nullptr; |
128 | 0 | if (!step->getSubExprAt(0) && |
129 | 0 | step->getNodeTest()->getType() == txNameTest::NAME_TEST && |
130 | 0 | (nameTest = static_cast<txNameTest*>(step->getNodeTest()))-> |
131 | 0 | mLocalName != nsGkAtoms::_asterisk) { |
132 | 0 |
|
133 | 0 | *aOutExpr = new txNamedAttributeStep(nameTest->mNamespace, |
134 | 0 | nameTest->mPrefix, |
135 | 0 | nameTest->mLocalName); |
136 | 0 | return NS_OK; // return since we no longer have a step-object. |
137 | 0 | } |
138 | 0 | } |
139 | 0 | |
140 | 0 | // Test for predicates that can be combined into the nodetest |
141 | 0 | Expr* pred; |
142 | 0 | while ((pred = step->getSubExprAt(0)) && |
143 | 0 | !pred->canReturnType(Expr::NUMBER_RESULT) && |
144 | 0 | !pred->isSensitiveTo(Expr::NODESET_CONTEXT)) { |
145 | 0 | txNodeTest* predTest = new txPredicatedNodeTest(step->getNodeTest(), pred); |
146 | 0 | step->dropFirst(); |
147 | 0 | step->setNodeTest(predTest); |
148 | 0 | } |
149 | 0 |
|
150 | 0 | return NS_OK; |
151 | 0 | } |
152 | | |
153 | | nsresult |
154 | | txXPathOptimizer::optimizePath(Expr* aInExpr, Expr** aOutExpr) |
155 | 0 | { |
156 | 0 | PathExpr* path = static_cast<PathExpr*>(aInExpr); |
157 | 0 |
|
158 | 0 | uint32_t i; |
159 | 0 | Expr* subExpr; |
160 | 0 | // look for steps like "//foo" that can be turned into "/descendant::foo" |
161 | 0 | // and "//." that can be turned into "/descendant-or-self::node()" |
162 | 0 | for (i = 0; (subExpr = path->getSubExprAt(i)); ++i) { |
163 | 0 | if (path->getPathOpAt(i) == PathExpr::DESCENDANT_OP && |
164 | 0 | subExpr->getType() == Expr::LOCATIONSTEP_EXPR && |
165 | 0 | !subExpr->getSubExprAt(0)) { |
166 | 0 | LocationStep* step = static_cast<LocationStep*>(subExpr); |
167 | 0 | if (step->getAxisIdentifier() == LocationStep::CHILD_AXIS) { |
168 | 0 | step->setAxisIdentifier(LocationStep::DESCENDANT_AXIS); |
169 | 0 | path->setPathOpAt(i, PathExpr::RELATIVE_OP); |
170 | 0 | } |
171 | 0 | else if (step->getAxisIdentifier() == LocationStep::SELF_AXIS) { |
172 | 0 | step->setAxisIdentifier(LocationStep::DESCENDANT_OR_SELF_AXIS); |
173 | 0 | path->setPathOpAt(i, PathExpr::RELATIVE_OP); |
174 | 0 | } |
175 | 0 | } |
176 | 0 | } |
177 | 0 |
|
178 | 0 | // look for expressions that start with a "./" |
179 | 0 | subExpr = path->getSubExprAt(0); |
180 | 0 | LocationStep* step; |
181 | 0 | if (subExpr->getType() == Expr::LOCATIONSTEP_EXPR && |
182 | 0 | path->getSubExprAt(1) && |
183 | 0 | path->getPathOpAt(1) != PathExpr::DESCENDANT_OP) { |
184 | 0 | step = static_cast<LocationStep*>(subExpr); |
185 | 0 | if (step->getAxisIdentifier() == LocationStep::SELF_AXIS && |
186 | 0 | !step->getSubExprAt(0)) { |
187 | 0 | txNodeTest* test = step->getNodeTest(); |
188 | 0 | txNodeTypeTest* typeTest; |
189 | 0 | if (test->getType() == txNodeTest::NODETYPE_TEST && |
190 | 0 | (typeTest = static_cast<txNodeTypeTest*>(test))-> |
191 | 0 | getNodeTestType() == txNodeTypeTest::NODE_TYPE) { |
192 | 0 | // We have a '.' as first step followed by a single '/'. |
193 | 0 |
|
194 | 0 | // Check if there are only two steps. If so, return the second |
195 | 0 | // as resulting expression. |
196 | 0 | if (!path->getSubExprAt(2)) { |
197 | 0 | *aOutExpr = path->getSubExprAt(1); |
198 | 0 | path->setSubExprAt(1, nullptr); |
199 | 0 |
|
200 | 0 | return NS_OK; |
201 | 0 | } |
202 | 0 | |
203 | 0 | // Just delete the '.' step and leave the rest of the PathExpr |
204 | 0 | path->deleteExprAt(0); |
205 | 0 | } |
206 | 0 | } |
207 | 0 | } |
208 | 0 |
|
209 | 0 | return NS_OK; |
210 | 0 | } |
211 | | |
212 | | nsresult |
213 | | txXPathOptimizer::optimizeUnion(Expr* aInExpr, Expr** aOutExpr) |
214 | 0 | { |
215 | 0 | UnionExpr* uni = static_cast<UnionExpr*>(aInExpr); |
216 | 0 |
|
217 | 0 | // Check for expressions like "foo | bar" and |
218 | 0 | // "descendant::foo | descendant::bar" |
219 | 0 |
|
220 | 0 | nsresult rv; |
221 | 0 | uint32_t current; |
222 | 0 | Expr* subExpr; |
223 | 0 | for (current = 0; (subExpr = uni->getSubExprAt(current)); ++current) { |
224 | 0 | if (subExpr->getType() != Expr::LOCATIONSTEP_EXPR || |
225 | 0 | subExpr->getSubExprAt(0)) { |
226 | 0 | continue; |
227 | 0 | } |
228 | 0 | |
229 | 0 | LocationStep* currentStep = static_cast<LocationStep*>(subExpr); |
230 | 0 | LocationStep::LocationStepType axis = currentStep->getAxisIdentifier(); |
231 | 0 |
|
232 | 0 | txUnionNodeTest* unionTest = nullptr; |
233 | 0 |
|
234 | 0 | // Check if there are any other steps with the same axis and merge |
235 | 0 | // them with currentStep |
236 | 0 | uint32_t i; |
237 | 0 | for (i = current + 1; (subExpr = uni->getSubExprAt(i)); ++i) { |
238 | 0 | if (subExpr->getType() != Expr::LOCATIONSTEP_EXPR || |
239 | 0 | subExpr->getSubExprAt(0)) { |
240 | 0 | continue; |
241 | 0 | } |
242 | 0 | |
243 | 0 | LocationStep* step = static_cast<LocationStep*>(subExpr); |
244 | 0 | if (step->getAxisIdentifier() != axis) { |
245 | 0 | continue; |
246 | 0 | } |
247 | 0 | |
248 | 0 | // Create a txUnionNodeTest if needed |
249 | 0 | if (!unionTest) { |
250 | 0 | nsAutoPtr<txNodeTest> owner(unionTest = new txUnionNodeTest); |
251 | 0 | rv = unionTest->addNodeTest(currentStep->getNodeTest()); |
252 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
253 | 0 |
|
254 | 0 | currentStep->setNodeTest(unionTest); |
255 | 0 | owner.forget(); |
256 | 0 | } |
257 | 0 |
|
258 | 0 | // Merge the nodetest into the union |
259 | 0 | rv = unionTest->addNodeTest(step->getNodeTest()); |
260 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
261 | 0 |
|
262 | 0 | step->setNodeTest(nullptr); |
263 | 0 |
|
264 | 0 | // Remove the step from the UnionExpr |
265 | 0 | uni->deleteExprAt(i); |
266 | 0 | --i; |
267 | 0 | } |
268 | 0 |
|
269 | 0 | // Check if all expressions were merged into a single step. If so, |
270 | 0 | // return the step as the new expression. |
271 | 0 | if (unionTest && current == 0 && !uni->getSubExprAt(1)) { |
272 | 0 | // Make sure the step doesn't get deleted when the UnionExpr is |
273 | 0 | uni->setSubExprAt(0, nullptr); |
274 | 0 | *aOutExpr = currentStep; |
275 | 0 |
|
276 | 0 | // Return right away since we no longer have a union |
277 | 0 | return NS_OK; |
278 | 0 | } |
279 | 0 | } |
280 | 0 |
|
281 | 0 | return NS_OK; |
282 | 0 | } |