txXPathOptimizer.cpp 8.9 KB

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