src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Choose.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) 2015, 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
/*
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one or more
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     7
 * contributor license agreements.  See the NOTICE file distributed with
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     8
 * this work for additional information regarding copyright ownership.
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     9
 * The ASF licenses this file to You under the Apache License, Version 2.0
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    10
 * (the "License"); you may not use this file except in compliance with
975138b77cff 8068842: Better JAXP data handling
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
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *     http://www.apache.org/licenses/LICENSE-2.0
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
 * $Id: Choose.java,v 1.2.4.1 2005/09/01 12:00:14 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.bcel.internal.generic.BranchHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.generic.GOTO;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.bcel.internal.generic.IFEQ;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    38
import java.util.ArrayList;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    39
import java.util.Collections;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    40
import java.util.Enumeration;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    41
import java.util.Iterator;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    42
import java.util.List;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * @author Morten Jorgensen
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
final class Choose extends Instruction {
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
     * Display the element contents (a lot of when's and an otherwise)
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    public void display(int indent) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
        indent(indent);
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
        Util.println("Choose");
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
        indent(indent + IndentIncrement);
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
        displayContents(indent + IndentIncrement);
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
     * Translate this Choose element. Generate a test-chain for the various
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
     * <xsl:when> elements and default to the <xsl:otherwise> if present.
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    66
        final List<SyntaxTreeNode> whenElements = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
        Otherwise otherwise = null;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    68
        Iterator<SyntaxTreeNode> elements = elements();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
        // These two are for reporting errors only
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
        ErrorMsg error = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
        final int line = getLineNumber();
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
        // Traverse all child nodes - must be either When or Otherwise
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    75
        while (elements.hasNext()) {
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    76
            SyntaxTreeNode element = elements.next();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
            // Add a When child element
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
            if (element instanceof When) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    79
                whenElements.add(element);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
            // Add an Otherwise child element
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
            else if (element instanceof Otherwise) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
                if (otherwise == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
                    otherwise = (Otherwise)element;
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
                else {
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
                    error = new ErrorMsg(ErrorMsg.MULTIPLE_OTHERWISE_ERR, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
                    getParser().reportError(Constants.ERROR, error);
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
            else if (element instanceof Text) {
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
                ((Text)element).ignore();
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
            // It is an error if we find some other element here
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
            else {
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
                error = new ErrorMsg(ErrorMsg.WHEN_ELEMENT_ERR, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
                getParser().reportError(Constants.ERROR, error);
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        // Make sure that there is at least one <xsl:when> element
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
        if (whenElements.size() == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
            error = new ErrorMsg(ErrorMsg.MISSING_WHEN_ERR, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
            getParser().reportError(Constants.ERROR, error);
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
            return;
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
        InstructionList il = methodGen.getInstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
        // next element will hold a handle to the beginning of next
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
        // When/Otherwise if test on current When fails
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
        BranchHandle nextElement = null;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   113
        List<InstructionHandle> exitHandles = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
        InstructionHandle exit = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   116
        Enumeration<SyntaxTreeNode> whens = Collections.enumeration(whenElements);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        while (whens.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
            final When when = (When)whens.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
            final Expression test = when.getTest();
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
            InstructionHandle truec = il.getEnd();
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
            if (nextElement != null)
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
                nextElement.setTarget(il.append(NOP));
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
            test.translateDesynthesized(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
            if (test instanceof FunctionCall) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
                FunctionCall call = (FunctionCall)test;
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
                try {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
                    Type type = call.typeCheck(getParser().getSymbolTable());
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
                    if (type != Type.Boolean) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
                        test._falseList.add(il.append(new IFEQ(null)));
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
                    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
                catch (TypeCheckError e) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
                    // handled later!
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
                }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
            // remember end of condition
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
            truec = il.getEnd();
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
            // The When object should be ignored completely in case it tests
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
            // for the support of a non-available element
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
            if (!when.ignore()) when.translateContents(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
            // goto exit after executing the body of when
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   147
            exitHandles.add(il.append(new GOTO(null)));
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
            if (whens.hasMoreElements() || otherwise != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
                nextElement = il.append(new GOTO(null));
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
                test.backPatchFalseList(nextElement);
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
            else
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
                test.backPatchFalseList(exit = il.append(NOP));
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
            test.backPatchTrueList(truec.getNext());
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
        // Translate any <xsl:otherwise> element
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
        if (otherwise != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
            nextElement.setTarget(il.append(NOP));
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
            otherwise.translateContents(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
            exit = il.append(NOP);
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        // now that end is known set targets of exit gotos
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   165
        Enumeration<InstructionHandle> exitGotos = Collections.enumeration(exitHandles);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
        while (exitGotos.hasMoreElements()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
            BranchHandle gotoExit = (BranchHandle)exitGotos.nextElement();
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
            gotoExit.setTarget(exit);
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
}