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