2006-01-26 04:50:30 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-01-26 04:50:30 +03:00
|
|
|
|
2015-01-06 02:52:20 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2006-01-26 04:50:30 +03:00
|
|
|
#include "txXPathOptimizer.h"
|
|
|
|
#include "txExprResult.h"
|
|
|
|
#include "nsIAtom.h"
|
2011-09-20 00:07:17 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2006-01-26 04:50:30 +03:00
|
|
|
#include "txXPathNode.h"
|
|
|
|
#include "txExpr.h"
|
2006-12-02 03:01:45 +03:00
|
|
|
#include "txIXPathContext.h"
|
|
|
|
|
|
|
|
class txEarlyEvalContext : public txIEvalContext
|
|
|
|
{
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit txEarlyEvalContext(txResultRecycler* aRecycler)
|
2006-12-02 03:01:45 +03:00
|
|
|
: mRecycler(aRecycler)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// txIEvalContext
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult getVariable(int32_t aNamespace, nsIAtom* aLName,
|
2006-12-02 03:01:45 +03:00
|
|
|
txAExprResult*& aResult)
|
|
|
|
{
|
2015-01-06 02:52:20 +03:00
|
|
|
MOZ_CRASH("shouldn't depend on this context");
|
2006-12-02 03:01:45 +03:00
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isStripSpaceAllowed(const txXPathNode& aNode)
|
2006-12-02 03:01:45 +03:00
|
|
|
{
|
2015-01-06 02:52:20 +03:00
|
|
|
MOZ_CRASH("shouldn't depend on this context");
|
2006-12-02 03:01:45 +03:00
|
|
|
}
|
|
|
|
void* getPrivateContext()
|
|
|
|
{
|
2015-01-06 02:52:20 +03:00
|
|
|
MOZ_CRASH("shouldn't depend on this context");
|
2006-12-02 03:01:45 +03:00
|
|
|
}
|
|
|
|
txResultRecycler* recycler()
|
|
|
|
{
|
|
|
|
return mRecycler;
|
|
|
|
}
|
|
|
|
void receiveError(const nsAString& aMsg, nsresult aRes)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
const txXPathNode& getContextNode()
|
|
|
|
{
|
2015-01-06 02:52:20 +03:00
|
|
|
MOZ_CRASH("shouldn't depend on this context");
|
2006-12-02 03:01:45 +03:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size()
|
2006-12-02 03:01:45 +03:00
|
|
|
{
|
2015-01-06 02:52:20 +03:00
|
|
|
MOZ_CRASH("shouldn't depend on this context");
|
2006-12-02 03:01:45 +03:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t position()
|
2006-12-02 03:01:45 +03:00
|
|
|
{
|
2015-01-06 02:52:20 +03:00
|
|
|
MOZ_CRASH("shouldn't depend on this context");
|
2006-12-02 03:01:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
txResultRecycler* mRecycler;
|
|
|
|
};
|
|
|
|
|
2006-01-26 04:50:30 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
txXPathOptimizer::optimize(Expr* aInExpr, Expr** aOutExpr)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aOutExpr = nullptr;
|
2006-01-26 04:50:30 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2006-12-02 03:01:45 +03:00
|
|
|
// 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)) {
|
|
|
|
nsRefPtr<txResultRecycler> recycler = new txResultRecycler;
|
|
|
|
txEarlyEvalContext context(recycler);
|
|
|
|
nsRefPtr<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
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2006-01-26 04:50:30 +03:00
|
|
|
Expr* subExpr;
|
|
|
|
while ((subExpr = aInExpr->getSubExprAt(i))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
Expr* newExpr = nullptr;
|
2006-01-26 04:50:30 +03:00
|
|
|
rv = optimize(subExpr, &newExpr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (newExpr) {
|
|
|
|
delete subExpr;
|
|
|
|
aInExpr->setSubExprAt(i, newExpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2006-12-02 03:01:45 +03:00
|
|
|
// Finally see if current expression can be optimized
|
|
|
|
switch (exprType) {
|
2006-11-10 23:31:59 +03:00
|
|
|
case Expr::LOCATIONSTEP_EXPR:
|
2006-01-26 04:50:30 +03:00
|
|
|
return optimizeStep(aInExpr, aOutExpr);
|
|
|
|
|
2006-11-10 23:31:59 +03:00
|
|
|
case Expr::PATH_EXPR:
|
|
|
|
return optimizePath(aInExpr, aOutExpr);
|
|
|
|
|
|
|
|
case Expr::UNION_EXPR:
|
|
|
|
return optimizeUnion(aInExpr, aOutExpr);
|
|
|
|
|
2006-01-26 04:50:30 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2006-11-10 23:31:59 +03:00
|
|
|
txXPathOptimizer::optimizeStep(Expr* aInExpr, Expr** aOutExpr)
|
2006-01-26 04:50:30 +03:00
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
LocationStep* step = static_cast<LocationStep*>(aInExpr);
|
2006-01-26 04:50:30 +03:00
|
|
|
|
2006-11-10 23:31:59 +03:00
|
|
|
if (step->getAxisIdentifier() == LocationStep::ATTRIBUTE_AXIS) {
|
|
|
|
// Test for @foo type steps.
|
2012-07-30 18:20:58 +04:00
|
|
|
txNameTest* nameTest = nullptr;
|
2006-11-10 23:31:59 +03:00
|
|
|
if (!step->getSubExprAt(0) &&
|
|
|
|
step->getNodeTest()->getType() == txNameTest::NAME_TEST &&
|
2007-07-08 11:08:04 +04:00
|
|
|
(nameTest = static_cast<txNameTest*>(step->getNodeTest()))->
|
2015-04-17 04:09:58 +03:00
|
|
|
mLocalName != nsGkAtoms::_asterisk) {
|
2006-01-26 04:50:30 +03:00
|
|
|
|
2006-11-10 23:31:59 +03:00
|
|
|
*aOutExpr = new txNamedAttributeStep(nameTest->mNamespace,
|
|
|
|
nameTest->mPrefix,
|
|
|
|
nameTest->mLocalName);
|
|
|
|
NS_ENSURE_TRUE(*aOutExpr, NS_ERROR_OUT_OF_MEMORY);
|
2006-01-26 04:50:30 +03:00
|
|
|
|
2006-11-10 23:31:59 +03:00
|
|
|
return NS_OK; // return since we no longer have a step-object.
|
|
|
|
}
|
2006-01-26 04:50:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
NS_ENSURE_TRUE(predTest, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
step->dropFirst();
|
|
|
|
step->setNodeTest(predTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-11-10 23:31:59 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
txXPathOptimizer::optimizePath(Expr* aInExpr, Expr** aOutExpr)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
PathExpr* path = static_cast<PathExpr*>(aInExpr);
|
2006-11-10 23:31:59 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2006-11-10 23:31:59 +03:00
|
|
|
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)) {
|
2007-07-08 11:08:04 +04:00
|
|
|
LocationStep* step = static_cast<LocationStep*>(subExpr);
|
2006-11-10 23:31:59 +03:00
|
|
|
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) {
|
2007-07-08 11:08:04 +04:00
|
|
|
step = static_cast<LocationStep*>(subExpr);
|
2006-11-10 23:31:59 +03:00
|
|
|
if (step->getAxisIdentifier() == LocationStep::SELF_AXIS &&
|
|
|
|
!step->getSubExprAt(0)) {
|
|
|
|
txNodeTest* test = step->getNodeTest();
|
|
|
|
txNodeTypeTest* typeTest;
|
|
|
|
if (test->getType() == txNodeTest::NODETYPE_TEST &&
|
2007-07-08 11:08:04 +04:00
|
|
|
(typeTest = static_cast<txNodeTypeTest*>(test))->
|
2006-11-10 23:31:59 +03:00
|
|
|
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);
|
2012-07-30 18:20:58 +04:00
|
|
|
path->setSubExprAt(1, nullptr);
|
2006-11-10 23:31:59 +03:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
UnionExpr* uni = static_cast<UnionExpr*>(aInExpr);
|
2006-11-10 23:31:59 +03:00
|
|
|
|
|
|
|
// Check for expressions like "foo | bar" and
|
|
|
|
// "descendant::foo | descendant::bar"
|
|
|
|
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t current;
|
2006-11-10 23:31:59 +03:00
|
|
|
Expr* subExpr;
|
|
|
|
for (current = 0; (subExpr = uni->getSubExprAt(current)); ++current) {
|
|
|
|
if (subExpr->getType() != Expr::LOCATIONSTEP_EXPR ||
|
|
|
|
subExpr->getSubExprAt(0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
LocationStep* currentStep = static_cast<LocationStep*>(subExpr);
|
2006-11-10 23:31:59 +03:00
|
|
|
LocationStep::LocationStepType axis = currentStep->getAxisIdentifier();
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
txUnionNodeTest* unionTest = nullptr;
|
2006-11-10 23:31:59 +03:00
|
|
|
|
|
|
|
// Check if there are any other steps with the same axis and merge
|
|
|
|
// them with currentStep
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2006-11-10 23:31:59 +03:00
|
|
|
for (i = current + 1; (subExpr = uni->getSubExprAt(i)); ++i) {
|
|
|
|
if (subExpr->getType() != Expr::LOCATIONSTEP_EXPR ||
|
|
|
|
subExpr->getSubExprAt(0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
LocationStep* step = static_cast<LocationStep*>(subExpr);
|
2006-11-10 23:31:59 +03:00
|
|
|
if (step->getAxisIdentifier() != axis) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a txUnionNodeTest if needed
|
|
|
|
if (!unionTest) {
|
2006-12-13 00:20:15 +03:00
|
|
|
nsAutoPtr<txNodeTest> owner(unionTest = new txUnionNodeTest);
|
2006-11-10 23:31:59 +03:00
|
|
|
NS_ENSURE_TRUE(unionTest, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
rv = unionTest->addNodeTest(currentStep->getNodeTest());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-12-13 00:20:15 +03:00
|
|
|
|
|
|
|
currentStep->setNodeTest(unionTest);
|
|
|
|
owner.forget();
|
2006-11-10 23:31:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the nodetest into the union
|
|
|
|
rv = unionTest->addNodeTest(step->getNodeTest());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
step->setNodeTest(nullptr);
|
2006-12-13 00:20:15 +03:00
|
|
|
|
2006-11-10 23:31:59 +03:00
|
|
|
// 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
|
2012-07-30 18:20:58 +04:00
|
|
|
uni->setSubExprAt(0, nullptr);
|
2006-11-10 23:31:59 +03:00
|
|
|
*aOutExpr = currentStep;
|
|
|
|
|
|
|
|
// Return right away since we no longer have a union
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|