jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Predicate.java
author chegar
Sun, 17 Aug 2014 15:51:56 +0100
changeset 25868 686eef1e7a79
parent 12458 jaxp/src/com/sun/org/apache/xalan/internal/xsltc/compiler/Predicate.java@d601e4bba306
child 44797 8b3b3b911b8a
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 2001-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: Predicate.java,v 1.2.4.1 2005/09/12 11:02:18 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.util.ArrayList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.classfile.Field;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.bcel.internal.generic.ASTORE;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.bcel.internal.generic.CHECKCAST;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.bcel.internal.generic.GETFIELD;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.bcel.internal.generic.INVOKESPECIAL;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.bcel.internal.generic.NEW;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.bcel.internal.generic.PUSH;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.bcel.internal.generic.PUTFIELD;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.FilterGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.IntType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NumberType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ReferenceType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TestGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
import com.sun.org.apache.xalan.internal.xsltc.runtime.Operators;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
final class Predicate extends Expression implements Closure {
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
     * The predicate's expression.
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    private Expression _exp = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     * This flag indicates if optimizations are turned on. The
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
     * method <code>dontOptimize()</code> can be called to turn
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
     * optimizations off.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    private boolean _canOptimize = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
     * Flag indicatig if the nth position optimization is on. It
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
     * is set in <code>typeCheck()</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
    private boolean _nthPositionFilter = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
     * Flag indicatig if the nth position descendant is on. It
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
     * is set in <code>typeCheck()</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    private boolean _nthDescendant = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
     * Cached node type of the expression that owns this predicate.
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    int _ptype = -1;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
     * Name of the inner class.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    private String _className = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     * List of variables in closure.
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    private ArrayList _closureVars = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * Reference to parent closure.
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    private Closure _parentClosure = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     * Cached value of method <code>getCompareValue()</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
    private Expression _value = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
     * Cached value of method <code>getCompareValue()</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
    private Step _step = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
     * Initializes a predicate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
    public Predicate(Expression exp) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        _exp = exp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        _exp.setParent(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
     * Set the parser for this expression.
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    public void setParser(Parser parser) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        super.setParser(parser);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
        _exp.setParser(parser);
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     * Returns a boolean value indicating if the nth position optimization
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
     * is on. Must be call after type checking!
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    public boolean isNthPositionFilter() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        return _nthPositionFilter;
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
     * Returns a boolean value indicating if the nth descendant optimization
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     * is on. Must be call after type checking!
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    public boolean isNthDescendant() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
        return _nthDescendant;
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
     * Turns off all optimizations for this predicate.
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
    public void dontOptimize() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
        _canOptimize = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
     * Returns true if the expression in this predicate contains a call
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
     * to position().
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    public boolean hasPositionCall() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        return _exp.hasPositionCall();
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
     * Returns true if the expression in this predicate contains a call
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
     * to last().
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
    public boolean hasLastCall() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
        return _exp.hasLastCall();
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    // -- Begin Closure interface --------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
     * Returns true if this closure is compiled in an inner class (i.e.
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * if this is a real closure).
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
    public boolean inInnerClass() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
        return (_className != null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
     * Returns a reference to its parent closure or null if outermost.
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    public Closure getParentClosure() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        if (_parentClosure == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
            SyntaxTreeNode node = getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
            do {
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
                if (node instanceof Closure) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
                    _parentClosure = (Closure) node;
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
                    break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
                if (node instanceof TopLevelElement) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
                    break;      // way up in the tree
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
                node = node.getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
            } while (node != null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
        return _parentClosure;
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
     * Returns the name of the auxiliary class or null if this predicate
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
     * is compiled inside the Translet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    public String getInnerClassName() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
        return _className;
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
     * Add new variable to the closure.
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    public void addVariable(VariableRefBase variableRef) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
        if (_closureVars == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
            _closureVars = new ArrayList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
        // Only one reference per variable
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        if (!_closureVars.contains(variableRef)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
            _closureVars.add(variableRef);
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
            // Add variable to parent closure as well
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
            Closure parentClosure = getParentClosure();
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
            if (parentClosure != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
                parentClosure.addVariable(variableRef);
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    // -- End Closure interface ----------------------
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
     * Returns the node type of the expression owning this predicate. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
     * return value is cached in <code>_ptype</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
    public int getPosType() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
        if (_ptype == -1) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
            SyntaxTreeNode parent = getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
            if (parent instanceof StepPattern) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
                _ptype = ((StepPattern)parent).getNodeType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
            else if (parent instanceof AbsoluteLocationPath) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
                AbsoluteLocationPath path = (AbsoluteLocationPath)parent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
                Expression exp = path.getPath();
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
                if (exp instanceof Step) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
                    _ptype = ((Step)exp).getNodeType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
            else if (parent instanceof VariableRefBase) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
                final VariableRefBase ref = (VariableRefBase)parent;
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
                final VariableBase var = ref.getVariable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
                final Expression exp = var.getExpression();
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
                if (exp instanceof Step) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
                    _ptype = ((Step)exp).getNodeType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
            else if (parent instanceof Step) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
                _ptype = ((Step)parent).getNodeType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
        return _ptype;
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
    public boolean parentIsPattern() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        return (getParent() instanceof Pattern);
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
    public Expression getExpr() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
        return _exp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
    public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
        return "pred(" + _exp + ')';
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
     * Type check a predicate expression. If the type of the expression is
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
     * number convert it to boolean by adding a comparison with position().
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
     * Note that if the expression is a parameter, we cannot distinguish
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
     * at compile time if its type is number or not. Hence, expressions of
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
     * reference type are always converted to booleans.
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
     * This method may be called twice, before and after calling
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
     * <code>dontOptimize()</code>. If so, the second time it should honor
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
     * the new value of <code>_canOptimize</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
        Type texp = _exp.typeCheck(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
        // We need explicit type information for reference types - no good!
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
        if (texp instanceof ReferenceType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
            _exp = new CastExpr(_exp, texp = Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
        // A result tree fragment should not be cast directly to a number type,
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
        // but rather to a boolean value, and then to a numer (0 or 1).
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
        // Ref. section 11.2 of the XSLT 1.0 spec
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
        if (texp instanceof ResultTreeType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
            _exp = new CastExpr(_exp, Type.Boolean);
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
            _exp = new CastExpr(_exp, Type.Real);
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
            texp = _exp.typeCheck(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
        // Numerical types will be converted to a position filter
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
        if (texp instanceof NumberType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            // Cast any numerical types to an integer
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
            if (texp instanceof IntType == false) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
                _exp = new CastExpr(_exp, Type.Int);
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
            if (_canOptimize) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
                // Nth position optimization. Expression must not depend on context
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
                _nthPositionFilter =
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
                    !_exp.hasLastCall() && !_exp.hasPositionCall();
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
                // _nthDescendant optimization - only if _nthPositionFilter is on
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
                if (_nthPositionFilter) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
                    SyntaxTreeNode parent = getParent();
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
                    _nthDescendant = (parent instanceof Step) &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
                        (parent.getParent() instanceof AbsoluteLocationPath);
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
                    return _type = Type.NodeSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
           // Reset optimization flags
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
            _nthPositionFilter = _nthDescendant = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
           // Otherwise, expand [e] to [position() = e]
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
           final QName position =
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
                getParser().getQNameIgnoreDefaultNs("position");
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
           final PositionCall positionCall =
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
                new PositionCall(position);
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
           positionCall.setParser(getParser());
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
           positionCall.setParent(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
           _exp = new EqualityExpr(Operators.EQ, positionCall,
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
                                    _exp);
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
           if (_exp.typeCheck(stable) != Type.Boolean) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
               _exp = new CastExpr(_exp, Type.Boolean);
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
           }
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
           return _type = Type.Boolean;
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
            // All other types will be handled as boolean values
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
            if (texp instanceof BooleanType == false) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
                _exp = new CastExpr(_exp, Type.Boolean);
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
            return _type = Type.Boolean;
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
     * Create a new "Filter" class implementing
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
     * <code>CurrentNodeListFilter</code>. Allocate registers for local
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
     * variables and local parameters passed in the closure to test().
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
     * Notice that local variables need to be "unboxed".
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
    private void compileFilter(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
                               MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
        TestGenerator testGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
        LocalVariableGen local;
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
        FilterGenerator filterGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
        _className = getXSLTC().getHelperClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
        filterGen = new FilterGenerator(_className,
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
                                        "java.lang.Object",
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
                                        toString(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
                                        ACC_PUBLIC | ACC_SUPER,
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
                                        new String[] {
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
                                            CURRENT_NODE_LIST_FILTER
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
                                        },
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
                                        classGen.getStylesheet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
        final ConstantPoolGen cpg = filterGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
        final int length = (_closureVars == null) ? 0 : _closureVars.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
        // Add a new instance variable for each var in closure
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
        for (int i = 0; i < length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
            VariableBase var = ((VariableRefBase) _closureVars.get(i)).getVariable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
            filterGen.addField(new Field(ACC_PUBLIC,
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
                                        cpg.addUtf8(var.getEscapedName()),
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
                                        cpg.addUtf8(var.getType().toSignature()),
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
                                        null, cpg.getConstantPool()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
        final InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
        testGen = new TestGenerator(ACC_PUBLIC | ACC_FINAL,
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
                                    com.sun.org.apache.bcel.internal.generic.Type.BOOLEAN,
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
                                    new com.sun.org.apache.bcel.internal.generic.Type[] {
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
                                        com.sun.org.apache.bcel.internal.generic.Type.INT,
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
                                        com.sun.org.apache.bcel.internal.generic.Type.INT,
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
                                        com.sun.org.apache.bcel.internal.generic.Type.INT,
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
                                        com.sun.org.apache.bcel.internal.generic.Type.INT,
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
                                        Util.getJCRefType(TRANSLET_SIG),
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
                                        Util.getJCRefType(NODE_ITERATOR_SIG)
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
                                    },
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
                                    new String[] {
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
                                        "node",
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
                                        "position",
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
                                        "last",
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
                                        "current",
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
                                        "translet",
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
                                        "iterator"
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
                                    },
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
                                    "test", _className, il, cpg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        // Store the dom in a local variable
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
        local = testGen.addLocalVariable("document",
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
                                         Util.getJCRefType(DOM_INTF_SIG),
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
                                         null, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
        final String className = classGen.getClassName();
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
        il.append(filterGen.loadTranslet());
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
        il.append(new CHECKCAST(cpg.addClass(className)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
        il.append(new GETFIELD(cpg.addFieldref(className,
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
                                               DOM_FIELD, DOM_INTF_SIG)));
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   415
        local.setStart(il.append(new ASTORE(local.getIndex())));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
        // Store the dom index in the test generator
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
        testGen.setDomIndex(local.getIndex());
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
        _exp.translate(filterGen, testGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
        il.append(IRETURN);
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
        filterGen.addEmptyConstructor(ACC_PUBLIC);
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   424
        filterGen.addMethod(testGen);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
        getXSLTC().dumpClass(filterGen.getJavaClass());
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
     * Returns true if the predicate is a test for the existance of an
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
     * element or attribute. All we have to do is to get the first node
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
     * from the step, check if it is there, and then return true/false.
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
    public boolean isBooleanTest() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
        return (_exp instanceof BooleanExpr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
     * Method to see if we can optimise the predicate by using a specialised
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
     * iterator for expressions like '/foo/bar[@attr = $var]', which are
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
     * very common in many stylesheets
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
    public boolean isNodeValueTest() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
        if (!_canOptimize) return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
        return (getStep() != null && getCompareValue() != null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
   /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
     * Returns the step in an expression of the form 'step = value'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
     * Null is returned if the expression is not of the right form.
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
     * Optimization if off if null is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
    public Step getStep() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
        // Returned cached value if called more than once
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
        if (_step != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
            return _step;
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
        // Nothing to do if _exp is null
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
        if (_exp == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
        // Ignore if not equality expression
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
        if (_exp instanceof EqualityExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
            EqualityExpr exp = (EqualityExpr)_exp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
            Expression left = exp.getLeft();
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
            Expression right = exp.getRight();
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
            // Unwrap and set _step if appropriate
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
            if (left instanceof CastExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
                left = ((CastExpr) left).getExpr();
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
            if (left instanceof Step) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
                _step = (Step) left;
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
            // Unwrap and set _step if appropriate
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
            if (right instanceof CastExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
                right = ((CastExpr)right).getExpr();
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
            if (right instanceof Step) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
                _step = (Step)right;
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
        return _step;
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
     * Returns the value in an expression of the form 'step = value'.
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
     * A value may be either a literal string or a variable whose
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
     * type is string. Optimization if off if null is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
    public Expression getCompareValue() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
        // Returned cached value if called more than once
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
        if (_value != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
            return _value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
        // Nothing to to do if _exp is null
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
        if (_exp == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
            return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
        // Ignore if not an equality expression
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
        if (_exp instanceof EqualityExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
            EqualityExpr exp = (EqualityExpr) _exp;
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
            Expression left = exp.getLeft();
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
            Expression right = exp.getRight();
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
            // Return if left is literal string
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
            if (left instanceof LiteralExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
                _value = left;
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
                return _value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
            // Return if left is a variable reference of type string
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
            if (left instanceof VariableRefBase &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
                left.getType() == Type.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
                _value = left;
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
                return _value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
            // Return if right is literal string
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
            if (right instanceof LiteralExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
                _value = right;
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
                return _value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
            // Return if left is a variable reference whose type is string
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
            if (right instanceof VariableRefBase &&
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
                right.getType() == Type.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
                _value = right;
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
                return _value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
     * Translate a predicate expression. This translation pushes
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
     * two references on the stack: a reference to a newly created
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
     * filter object and a reference to the predicate's closure.
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
    public void translateFilter(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
                                MethodGenerator methodGen)
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
        // Compile auxiliary class for filter
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
        compileFilter(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
        // Create new instance of filter
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
        il.append(new NEW(cpg.addClass(_className)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
        il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
        il.append(new INVOKESPECIAL(cpg.addMethodref(_className,
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
                                                     "<init>", "()V")));
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
        // Initialize closure variables
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
        final int length = (_closureVars == null) ? 0 : _closureVars.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
        for (int i = 0; i < length; i++) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
            VariableRefBase varRef = (VariableRefBase) _closureVars.get(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
            VariableBase var = varRef.getVariable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
            Type varType = var.getType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
            il.append(DUP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
            // Find nearest closure implemented as an inner class
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
            Closure variableClosure = _parentClosure;
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
            while (variableClosure != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
                if (variableClosure.inInnerClass()) break;
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
                variableClosure = variableClosure.getParentClosure();
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
            // Use getfield if in an inner class
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
            if (variableClosure != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
                il.append(ALOAD_0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
                il.append(new GETFIELD(
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
                    cpg.addFieldref(variableClosure.getInnerClassName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
                        var.getEscapedName(), varType.toSignature())));
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
                // Use a load of instruction if in translet class
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
                il.append(var.loadInstruction());
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
            // Store variable in new closure
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
            il.append(new PUTFIELD(
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
                    cpg.addFieldref(_className, var.getEscapedName(),
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
                        varType.toSignature())));
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
     * Translate a predicate expression. If non of the optimizations apply
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
     * then this translation pushes two references on the stack: a reference
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
     * to a newly created filter object and a reference to the predicate's
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
     * closure. See class <code>Step</code> for further details.
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
        if (_nthPositionFilter || _nthDescendant) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
            _exp.translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
        else if (isNodeValueTest() && (getParent() instanceof Step)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
            _value.translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
            il.append(new CHECKCAST(cpg.addClass(STRING_CLASS)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
            il.append(new PUSH(cpg, ((EqualityExpr)_exp).getOp()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
        else {
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
            translateFilter(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
}