1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * IBM Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * IBM Corporation. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * IBM Corporation
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "txPatternOptimizer.h"
40 : #include "txXSLTPatterns.h"
41 :
42 : nsresult
43 0 : txPatternOptimizer::optimize(txPattern* aInPattern, txPattern** aOutPattern)
44 : {
45 0 : *aOutPattern = nsnull;
46 0 : nsresult rv = NS_OK;
47 :
48 : // First optimize sub expressions
49 0 : PRUint32 i = 0;
50 : Expr* subExpr;
51 0 : while ((subExpr = aInPattern->getSubExprAt(i))) {
52 0 : Expr* newExpr = nsnull;
53 0 : rv = mXPathOptimizer.optimize(subExpr, &newExpr);
54 0 : NS_ENSURE_SUCCESS(rv, rv);
55 0 : if (newExpr) {
56 0 : delete subExpr;
57 0 : aInPattern->setSubExprAt(i, newExpr);
58 : }
59 :
60 0 : ++i;
61 : }
62 :
63 : // Then optimize sub patterns
64 : txPattern* subPattern;
65 0 : i = 0;
66 0 : while ((subPattern = aInPattern->getSubPatternAt(i))) {
67 0 : txPattern* newPattern = nsnull;
68 0 : rv = optimize(subPattern, &newPattern);
69 0 : NS_ENSURE_SUCCESS(rv, rv);
70 0 : if (newPattern) {
71 0 : delete subPattern;
72 0 : aInPattern->setSubPatternAt(i, newPattern);
73 : }
74 :
75 0 : ++i;
76 : }
77 :
78 : // Finally see if current pattern can be optimized
79 0 : switch (aInPattern->getType()) {
80 : case txPattern::STEP_PATTERN:
81 0 : return optimizeStep(aInPattern, aOutPattern);
82 :
83 : default:
84 : break;
85 : }
86 :
87 0 : return NS_OK;
88 : }
89 :
90 :
91 : nsresult
92 0 : txPatternOptimizer::optimizeStep(txPattern* aInPattern,
93 : txPattern** aOutPattern)
94 : {
95 0 : txStepPattern* step = static_cast<txStepPattern*>(aInPattern);
96 :
97 : // Test for predicates that can be combined into the nodetest
98 : Expr* pred;
99 0 : while ((pred = step->getSubExprAt(0)) &&
100 0 : !pred->canReturnType(Expr::NUMBER_RESULT) &&
101 0 : !pred->isSensitiveTo(Expr::NODESET_CONTEXT)) {
102 : txNodeTest* predTest = new txPredicatedNodeTest(step->getNodeTest(),
103 0 : pred);
104 0 : step->dropFirst();
105 0 : step->setNodeTest(predTest);
106 : }
107 :
108 0 : return NS_OK;
109 : }
|