src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Expression.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.generic.BranchHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.bcel.internal.generic.GOTO_W;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.bcel.internal.generic.IFEQ;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.BooleanType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.NodeSetType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    38
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * @author Erwin Bolwidt <ejb@klomp.org>
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
abstract class Expression extends SyntaxTreeNode {
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
     * The type of this expression. It is set after calling
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
     * <code>typeCheck()</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    protected Type _type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
     * Instruction handles that comprise the true list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
    protected FlowList _trueList = new FlowList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
     * Instruction handles that comprise the false list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    protected FlowList _falseList = new FlowList();
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
    public Type getType() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
        return _type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    public abstract String toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    public boolean hasPositionCall() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        return false;           // default should be 'false' for StepPattern
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    public boolean hasLastCall() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        return 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
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
     * Returns an object representing the compile-time evaluation
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
     * of an expression. We are only using this for function-available
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
     * and element-available at this time.
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    public Object evaluateAtCompileTime() {
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     * Type check all the children of this node.
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    public Type typeCheck(SymbolTable stable) throws TypeCheckError {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
        return typeCheckContents(stable);
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
     * Translate this node into JVM bytecodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        ErrorMsg msg = new ErrorMsg(ErrorMsg.NOT_IMPLEMENTED_ERR,
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
                                    getClass(), this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        getParser().reportError(FATAL, msg);
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
     * Translate this node into a fresh instruction list.
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
     * The original instruction list is saved and restored.
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    public final InstructionList compile(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
                                         MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
        final InstructionList result, save = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
        methodGen.setInstructionList(result = new InstructionList());
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
        translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        methodGen.setInstructionList(save);
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
     * Redefined by expressions of type boolean that use flow lists.
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    public void translateDesynthesized(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
                                       MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        translate(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        if (_type instanceof BooleanType) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
            desynthesize(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
     * If this expression is of type node-set and it is not a variable
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
     * reference, then call setStartNode() passing the context node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    public void startIterator(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
                                   MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
        // Ignore if type is not node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        if (_type instanceof NodeSetType == false) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
        // setStartNode() should not be called if expr is a variable ref
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        Expression expr = this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
        if (expr instanceof CastExpr) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
            expr = ((CastExpr) expr).getExpr();
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        if (expr instanceof VariableRefBase == false) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
            final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            il.append(methodGen.loadContextNode());
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
            il.append(methodGen.setStartNode());
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     * Synthesize a boolean expression, i.e., either push a 0 or 1 onto the
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     * operand stack for the next statement to succeed. Returns the handle
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     * of the instruction to be backpatched.
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    public void synthesize(ClassGenerator classGen, MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        final ConstantPoolGen cpg = classGen.getConstantPool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        _trueList.backPatch(il.append(ICONST_1));
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        final BranchHandle truec = il.append(new GOTO_W(null));
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        _falseList.backPatch(il.append(ICONST_0));
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
        truec.setTarget(il.append(NOP));
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
    public void desynthesize(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
                             MethodGenerator methodGen) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
        final InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
        _falseList.add(il.append(new IFEQ(null)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    public FlowList getFalseList() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        return _falseList;
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    public FlowList getTrueList() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
        return _trueList;
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
    public void backPatchFalseList(InstructionHandle ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
        _falseList.backPatch(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    public void backPatchTrueList(InstructionHandle ih) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
        _trueList.backPatch(ih);
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
     * Search for a primop in the symbol table that matches the method type
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
     * <code>ctype</code>. Two methods match if they have the same arity.
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     * If a primop is overloaded then the "closest match" is returned. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
     * first entry in the vector of primops that has the right arity is
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
     * considered to be the default one.
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
    public MethodType lookupPrimop(SymbolTable stable, String op,
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
                                   MethodType ctype) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
        MethodType result = null;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   195
        final List<MethodType> primop = stable.lookupPrimop(op);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
        if (primop != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
            final int n = primop.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
            int minDistance = Integer.MAX_VALUE;
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
            for (int i = 0; i < n; i++) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   200
                final MethodType ptype = primop.get(i);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
                // Skip if different arity
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
                if (ptype.argsCount() != ctype.argsCount()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
                    continue;
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
                // The first method with the right arity is the default
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
                if (result == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
                    result = ptype;             // default method
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
                // Check if better than last one found
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
                final int distance = ctype.distanceTo(ptype);
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
                if (distance < minDistance) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
                    minDistance = distance;
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
                    result = ptype;
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
        return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
}