langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Operators.java
author mcimadamore
Fri, 16 Dec 2016 15:27:34 +0000
changeset 42827 36468b5fa7f4
parent 39920 4923274643f2
permissions -rw-r--r--
8181370: Convert anonymous inner classes into lambdas/method references Reviewed-by: jjg, rfield, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     1
/*
39806
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     4
 *
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    10
 *
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    16
 *
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    20
 *
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    23
 * questions.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    24
 */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    25
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    27
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    28
import com.sun.tools.javac.code.Symbol;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    29
import com.sun.tools.javac.code.Symbol.OperatorSymbol;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    30
import com.sun.tools.javac.code.Symtab;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    31
import com.sun.tools.javac.code.Type;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    32
import com.sun.tools.javac.code.Type.MethodType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    33
import com.sun.tools.javac.code.TypeTag;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    34
import com.sun.tools.javac.code.Types;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    35
import com.sun.tools.javac.jvm.ByteCodes;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    36
import com.sun.tools.javac.resources.CompilerProperties.Errors;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    38
import com.sun.tools.javac.tree.JCTree.Tag;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    39
import com.sun.tools.javac.util.Assert;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    40
import com.sun.tools.javac.util.Context;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    41
import com.sun.tools.javac.util.JCDiagnostic;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    42
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    43
import com.sun.tools.javac.util.List;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    44
import com.sun.tools.javac.util.Log;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    45
import com.sun.tools.javac.util.Name;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    46
import com.sun.tools.javac.util.Names;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    47
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    48
import java.util.HashMap;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    49
import java.util.Map;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    50
import java.util.Optional;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    51
import java.util.function.BiPredicate;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    52
import java.util.function.Function;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    53
import java.util.function.Predicate;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    54
import java.util.function.Supplier;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    55
import java.util.stream.Stream;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    56
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    57
import static com.sun.tools.javac.jvm.ByteCodes.*;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    58
import static com.sun.tools.javac.comp.Operators.OperatorType.*;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    59
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    60
/**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    61
 * This class contains the logic for unary and binary operator resolution/lookup.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    62
 *
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    63
 * <p><b>This is NOT part of any supported API.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    64
 * If you write code that depends on this, you do so at your own risk.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    65
 * This code and its internal interfaces are subject to change or
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    66
 * deletion without notice.</b>
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    67
 */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    68
public class Operators {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    69
    protected static final Context.Key<Operators> operatorsKey = new Context.Key<>();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    70
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    71
    private final Names names;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    72
    private final Log log;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    73
    private final Symtab syms;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    74
    private final Types types;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    75
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    76
    /** Unary operators map. */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    77
    private Map<Name, List<UnaryOperatorHelper>> unaryOperators = new HashMap<>(Tag.getNumberOfOperators());
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    78
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    79
    /** Binary operators map. */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    80
    private Map<Name, List<BinaryOperatorHelper>> binaryOperators = new HashMap<>(Tag.getNumberOfOperators());
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    81
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    82
    /** The names of all operators. */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    83
    private Name[] opname = new Name[Tag.getNumberOfOperators()];
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    84
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    85
    public static Operators instance(Context context) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    86
        Operators instance = context.get(operatorsKey);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    87
        if (instance == null)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    88
            instance = new Operators(context);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    89
        return instance;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    90
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    91
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    92
    protected Operators(Context context) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    93
        context.put(operatorsKey, this);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    94
        syms = Symtab.instance(context);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    95
        names = Names.instance(context);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    96
        log = Log.instance(context);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    97
        types = Types.instance(context);
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
    98
        noOpSymbol = new OperatorSymbol(names.empty, Type.noType, -1, syms.noSymbol);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
    99
        initOperatorNames();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   100
        initUnaryOperators();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   101
        initBinaryOperators();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   102
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   103
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   104
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   105
     * Perform unary promotion of a type; this routine implements JLS 5.6.1.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   106
     * If the input type is not supported by unary promotion, it is returned unaltered.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   107
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   108
    Type unaryPromotion(Type t) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   109
        Type unboxed = types.unboxedTypeOrType(t);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   110
        switch (unboxed.getTag()) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   111
            case BYTE:
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   112
            case SHORT:
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   113
            case CHAR:
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   114
                return syms.intType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   115
            default:
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   116
                return unboxed;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   117
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   118
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   119
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   120
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   121
     * Perform binary promotion of a pair of types; this routine implements JLS 5.6.2.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   122
     * If the input types are not supported by unary promotion, if such types are identical to
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   123
     * a type C, then C is returned, otherwise Object is returned.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   124
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   125
    Type binaryPromotion(Type t1, Type t2) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   126
        Type unboxedT1 = types.unboxedTypeOrType(t1);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   127
        Type unboxedT2 = types.unboxedTypeOrType(t2);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   128
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   129
        if (unboxedT1.isNumeric() && unboxedT2.isNumeric()) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   130
            if (unboxedT1.hasTag(TypeTag.DOUBLE) || unboxedT2.hasTag(TypeTag.DOUBLE)) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   131
                return syms.doubleType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   132
            } else if (unboxedT1.hasTag(TypeTag.FLOAT) || unboxedT2.hasTag(TypeTag.FLOAT)) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   133
                return syms.floatType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   134
            } else if (unboxedT1.hasTag(TypeTag.LONG) || unboxedT2.hasTag(TypeTag.LONG)) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   135
                return syms.longType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   136
            } else {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   137
                return syms.intType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   138
            }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   139
        } else if (types.isSameType(unboxedT1, unboxedT2)) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   140
            return unboxedT1;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   141
        } else {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   142
            return syms.objectType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   143
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   144
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   145
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   146
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   147
     * Entry point for resolving a unary operator given an operator tag and an argument type.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   148
     */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   149
    OperatorSymbol resolveUnary(DiagnosticPosition pos, JCTree.Tag tag, Type op) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   150
        return resolve(tag,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   151
                unaryOperators,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   152
                unop -> unop.test(op),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   153
                unop -> unop.resolve(op),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   154
                () -> reportErrorIfNeeded(pos, tag, op));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   155
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   156
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   157
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   158
     * Entry point for resolving a binary operator given an operator tag and a pair of argument types.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   159
     */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   160
    OperatorSymbol resolveBinary(DiagnosticPosition pos, JCTree.Tag tag, Type op1, Type op2) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   161
        return resolve(tag,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   162
                binaryOperators,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   163
                binop -> binop.test(op1, op2),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   164
                binop -> binop.resolve(op1, op2),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   165
                () -> reportErrorIfNeeded(pos, tag, op1, op2));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   166
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   167
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   168
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   169
     * Main operator lookup routine; lookup an operator (either unary or binary) in its corresponding
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   170
     * map. If there's a matching operator, its resolve routine is called and the result is returned;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   171
     * otherwise the result of a fallback function is returned.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   172
     */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   173
    private <O> OperatorSymbol resolve(Tag tag, Map<Name, List<O>> opMap, Predicate<O> opTestFunc,
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   174
                       Function<O, OperatorSymbol> resolveFunc, Supplier<OperatorSymbol> noResultFunc) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   175
        return opMap.get(operatorName(tag)).stream()
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   176
                .filter(opTestFunc)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   177
                .map(resolveFunc)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   178
                .findFirst()
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   179
                .orElseGet(noResultFunc);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   180
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   181
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   182
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   183
     * Creates an operator symbol.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   184
     */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   185
    private OperatorSymbol makeOperator(Name name, List<OperatorType> formals, OperatorType res, int... opcodes) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   186
        MethodType opType = new MethodType(
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   187
                formals.stream()
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   188
                        .map(o -> o.asType(syms))
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   189
                        .collect(List.collector()),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   190
                res.asType(syms), List.nil(), syms.methodClass);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   191
        return new OperatorSymbol(name, opType, mergeOpcodes(opcodes), syms.noSymbol);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   192
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   193
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   194
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   195
     * Fold two opcodes in a single int value (if required).
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   196
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   197
    private int mergeOpcodes(int... opcodes) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   198
        int opcodesLen = opcodes.length;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   199
        Assert.check(opcodesLen == 1 || opcodesLen == 2);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   200
        return (opcodesLen == 1) ?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   201
                opcodes[0] :
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   202
                ((opcodes[0] << ByteCodes.preShift) | opcodes[1]);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   203
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   204
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   205
    /** A symbol that stands for a missing operator.
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   206
     */
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   207
    public final OperatorSymbol noOpSymbol;
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   208
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   209
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   210
     * Report an operator lookup error.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   211
     */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   212
    private OperatorSymbol reportErrorIfNeeded(DiagnosticPosition pos, Tag tag, Type... args) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   213
        if (Stream.of(args).noneMatch(Type::isErroneous)) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   214
            Name opName = operatorName(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   215
            JCDiagnostic.Error opError = (args.length) == 1 ?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   216
                    Errors.OperatorCantBeApplied(opName, args[0]) :
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   217
                    Errors.OperatorCantBeApplied1(opName, args[0], args[1]);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   218
            log.error(pos, opError);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   219
        }
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   220
        return noOpSymbol;
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   221
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   222
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   223
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   224
     * Return name of operator with given tree tag.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   225
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   226
    public Name operatorName(JCTree.Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   227
        return opname[tag.operatorIndex()];
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   228
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   229
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   230
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   231
     * The constants in this enum represent the types upon which all the operator helpers
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   232
     * operate upon. This allows lazy and consise mapping between a type name and a type instance.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   233
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   234
    enum OperatorType {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   235
        BYTE(syms -> syms.byteType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   236
        SHORT(syms -> syms.shortType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   237
        INT(syms -> syms.intType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   238
        LONG(syms -> syms.longType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   239
        FLOAT(syms -> syms.floatType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   240
        DOUBLE(syms -> syms.doubleType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   241
        CHAR(syms -> syms.charType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   242
        BOOLEAN(syms -> syms.booleanType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   243
        OBJECT(syms -> syms.objectType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   244
        STRING(syms -> syms.stringType),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   245
        BOT(syms -> syms.botType);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   246
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   247
        final Function<Symtab, Type> asTypeFunc;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   248
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   249
        OperatorType(Function<Symtab, Type> asTypeFunc) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   250
            this.asTypeFunc = asTypeFunc;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   251
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   252
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   253
        Type asType(Symtab syms) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   254
            return asTypeFunc.apply(syms);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   255
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   256
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   257
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   258
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   259
     * Common root for all operator helpers. An operator helper instance is associated with a
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   260
     * given operator (i.e. '+'); it contains routines to perform operator lookup, i.e. find
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   261
     * which version of the '+' operator is the best given an argument type list. Supported
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   262
     * operator symbols are initialized lazily upon first lookup request - this is in order to avoid
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   263
     * initialization circularities between this class and {@code Symtab}.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   264
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   265
    abstract class OperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   266
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   267
        /** The operator name. */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   268
        final Name name;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   269
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   270
        /** The list of symbols associated with this operator (lazily populated). */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   271
        Optional<OperatorSymbol[]> alternatives = Optional.empty();
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   272
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   273
        /** An array of operator symbol suppliers (used to lazily populate the symbol list). */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   274
        List<Supplier<OperatorSymbol>> operatorSuppliers = List.nil();
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   275
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   276
        @SuppressWarnings("varargs")
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   277
        OperatorHelper(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   278
            this.name = operatorName(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   279
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   280
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   281
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   282
         * This routine implements the main operator lookup process. Each operator is tested
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   283
         * using an applicability predicate; if the test suceeds that same operator is returned,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   284
         * otherwise a dummy symbol is returned.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   285
         */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   286
        final OperatorSymbol doLookup(Predicate<OperatorSymbol> applicabilityTest) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   287
            return Stream.of(alternatives.orElseGet(this::initOperators))
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   288
                    .filter(applicabilityTest)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   289
                    .findFirst()
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   290
                    .orElse(noOpSymbol);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   291
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   292
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   293
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   294
         * This routine performs lazy instantiation of the operator symbols supported by this helper.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   295
         * After initialization is done, the suppliers are cleared, to free up memory.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   296
         */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   297
        private OperatorSymbol[] initOperators() {
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   298
            OperatorSymbol[] operators = operatorSuppliers.stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 39920
diff changeset
   299
                    .map(Supplier::get)
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   300
                    .toArray(OperatorSymbol[]::new);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   301
            alternatives = Optional.of(operators);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   302
            operatorSuppliers = null; //let GC do its work
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   303
            return operators;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   304
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   305
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   306
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   307
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   308
     * Common superclass for all unary operator helpers.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   309
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   310
    abstract class UnaryOperatorHelper extends OperatorHelper implements Predicate<Type> {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   311
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   312
        UnaryOperatorHelper(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   313
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   314
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   315
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   316
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   317
         * This routine implements the unary operator lookup process. It customizes the behavior
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   318
         * of the shared lookup routine in {@link OperatorHelper}, by using an unary applicability test
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   319
         * (see {@link UnaryOperatorHelper#isUnaryOperatorApplicable(OperatorOperatorSymbol, Type)}
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   320
         */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   321
        final OperatorSymbol doLookup(Type t) {
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   322
            return doLookup(op -> isUnaryOperatorApplicable(op, t));
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   323
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   324
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   325
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   326
         * Unary operator applicability test - is the input type the same as the expected operand type?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   327
         */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   328
        boolean isUnaryOperatorApplicable(OperatorSymbol op, Type t) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   329
            return types.isSameType(op.type.getParameterTypes().head, t);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   330
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   331
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   332
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   333
         * Adds a unary operator symbol.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   334
         */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   335
        final UnaryOperatorHelper addUnaryOperator(OperatorType arg, OperatorType res, int... opcode) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   336
            operatorSuppliers = operatorSuppliers.prepend(() -> makeOperator(name, List.of(arg), res, opcode));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   337
            return this;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   338
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   339
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   340
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   341
         * This method will be overridden by unary operator helpers to provide custom resolution
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   342
         * logic.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   343
         */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   344
        abstract OperatorSymbol resolve(Type t);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   345
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   346
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   347
    abstract class BinaryOperatorHelper extends OperatorHelper implements BiPredicate<Type, Type> {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   348
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   349
        BinaryOperatorHelper(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   350
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   351
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   352
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   353
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   354
         * This routine implements the binary operator lookup process. It customizes the behavior
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   355
         * of the shared lookup routine in {@link OperatorHelper}, by using an unary applicability test
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   356
         * (see {@link BinaryOperatorHelper#isBinaryOperatorApplicable(OperatorSymbol, Type, Type)}
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   357
         */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   358
        final OperatorSymbol doLookup(Type t1, Type t2) {
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   359
            return doLookup(op -> isBinaryOperatorApplicable(op, t1, t2));
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   360
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   361
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   362
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   363
         * Binary operator applicability test - are the input types the same as the expected operand types?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   364
         */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   365
        boolean isBinaryOperatorApplicable(OperatorSymbol op, Type t1, Type t2) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   366
            List<Type> formals = op.type.getParameterTypes();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   367
            return types.isSameType(formals.head, t1) &&
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   368
                    types.isSameType(formals.tail.head, t2);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   369
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   370
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   371
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   372
         * Adds a binary operator symbol.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   373
         */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   374
        final BinaryOperatorHelper addBinaryOperator(OperatorType arg1, OperatorType arg2, OperatorType res, int... opcode) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   375
            operatorSuppliers = operatorSuppliers.prepend(() -> makeOperator(name, List.of(arg1, arg2), res, opcode));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   376
            return this;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   377
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   378
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   379
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   380
         * This method will be overridden by binary operator helpers to provide custom resolution
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   381
         * logic.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   382
         */
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   383
        abstract OperatorSymbol resolve(Type t1, Type t2);
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   384
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   385
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   386
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   387
     * Class representing unary operator helpers that operate on reference types.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   388
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   389
    class UnaryReferenceOperator extends UnaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   390
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   391
        UnaryReferenceOperator(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   392
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   393
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   394
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   395
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   396
        public boolean test(Type type) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   397
            return type.isNullOrReference();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   398
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   399
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   400
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   401
        public OperatorSymbol resolve(Type arg) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   402
            return doLookup(syms.objectType);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   403
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   404
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   405
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   406
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   407
     * Class representing unary operator helpers that operate on numeric types (either boxed or unboxed).
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   408
     * Operator lookup is performed after applying numeric promotion of the input type.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   409
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   410
    class UnaryNumericOperator extends UnaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   411
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   412
        Predicate<Type> numericTest;
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   413
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   414
        UnaryNumericOperator(Tag tag) {
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   415
            this(tag, Type::isNumeric);
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   416
        }
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   417
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   418
        UnaryNumericOperator(Tag tag, Predicate<Type> numericTest) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   419
            super(tag);
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   420
            this.numericTest = numericTest;
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   421
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   422
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   423
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   424
        public boolean test(Type type) {
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   425
            return numericTest.test(unaryPromotion(type));
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   426
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   427
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   428
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   429
        public OperatorSymbol resolve(Type arg) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   430
            return doLookup(unaryPromotion(arg));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   431
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   432
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   433
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   434
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   435
     * Class representing unary operator helpers that operate on boolean types  (either boxed or unboxed).
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   436
     * Operator lookup is performed assuming the input type is a boolean type.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   437
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   438
    class UnaryBooleanOperator extends UnaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   439
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   440
        UnaryBooleanOperator(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   441
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   442
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   443
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   444
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   445
        public boolean test(Type type) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   446
            return types.unboxedTypeOrType(type).hasTag(TypeTag.BOOLEAN);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   447
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   448
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   449
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   450
        public OperatorSymbol resolve(Type arg) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   451
            return doLookup(syms.booleanType);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   452
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   453
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   454
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   455
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   456
     * Class representing prefix/postfix unary operator helpers. Operates on numeric types (either
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   457
     * boxed or unboxed). Operator lookup is performed on the unboxed version of the input type.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   458
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   459
    class UnaryPrefixPostfixOperator extends UnaryNumericOperator {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   460
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   461
        UnaryPrefixPostfixOperator(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   462
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   463
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   464
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   465
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   466
        public OperatorSymbol resolve(Type arg) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   467
            return doLookup(types.unboxedTypeOrType(arg));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   468
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   469
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   470
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   471
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   472
     * Class representing binary operator helpers that operate on numeric types (either boxed or unboxed).
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   473
     * Operator lookup is performed after applying binary numeric promotion of the input types.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   474
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   475
    class BinaryNumericOperator extends BinaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   476
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   477
        Predicate<Type> numericTest;
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   478
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   479
        BinaryNumericOperator(Tag tag) {
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   480
            this(tag, Type::isNumeric);
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   481
        }
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   482
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   483
        BinaryNumericOperator(Tag tag, Predicate<Type> numericTest) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   484
            super(tag);
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   485
            this.numericTest = numericTest;
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   486
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   487
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   488
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   489
        public OperatorSymbol resolve(Type arg1, Type arg2) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   490
            Type t = binaryPromotion(arg1, arg2);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   491
            return doLookup(t, t);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   492
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   493
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   494
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   495
        public boolean test(Type arg1, Type arg2) {
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   496
            return numericTest.test(unaryPromotion(arg1)) &&
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   497
                    numericTest.test(unaryPromotion(arg2));
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   498
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   499
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   500
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   501
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   502
     * Class representing bitwise operator helpers that operate on boolean types (either boxed or unboxed).
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   503
     * Operator lookup is performed assuming both input types are boolean types.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   504
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   505
    class BinaryBooleanOperator extends BinaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   506
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   507
        BinaryBooleanOperator(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   508
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   509
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   510
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   511
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   512
        public OperatorSymbol resolve(Type arg1, Type arg2) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   513
            return doLookup(syms.booleanType, syms.booleanType);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   514
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   515
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   516
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   517
        public boolean test(Type arg1, Type arg2) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   518
            return types.unboxedTypeOrType(arg1).hasTag(TypeTag.BOOLEAN) &&
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   519
                    types.unboxedTypeOrType(arg2).hasTag(TypeTag.BOOLEAN);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   520
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   521
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   522
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   523
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   524
     * Class representing string concatenation operator helper that operates on at least an
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   525
     * string operand. Input types subject to an operator lookup undergoes a special string promotion
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   526
     * (see {@link BinaryStringOperator#stringPromotion(Type)}.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   527
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   528
    class BinaryStringOperator extends BinaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   529
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   530
        BinaryStringOperator(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   531
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   532
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   533
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   534
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   535
        public OperatorSymbol resolve(Type arg1, Type arg2) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   536
            return doLookup(stringPromotion(arg1), stringPromotion(arg2));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   537
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   538
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   539
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   540
        public boolean test(Type arg1, Type arg2) {
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   541
            boolean hasStringOp = types.isSameType(arg1, syms.stringType) ||
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   542
                    types.isSameType(arg2, syms.stringType);
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   543
            boolean hasVoidOp = arg1.hasTag(TypeTag.VOID) || arg2.hasTag(TypeTag.VOID);
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   544
            return hasStringOp && !hasVoidOp;
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   545
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   546
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   547
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   548
         * This routine applies following mappings:
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   549
         * - if input type is primitive, apply numeric promotion
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   550
         * - if input type is either 'void', 'null' or 'String' leave it untouched
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   551
         * - otherwise return 'Object'
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   552
         */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   553
        private Type stringPromotion(Type t) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   554
            if (t.isPrimitive()) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   555
                return unaryPromotion(t);
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   556
            } else if (t.hasTag(TypeTag.VOID) || t.hasTag(TypeTag.BOT) ||
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   557
                    types.isSameType(t, syms.stringType)) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   558
                return t;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   559
            } else if (t.hasTag(TypeTag.TYPEVAR)) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   560
                return stringPromotion(t.getUpperBound());
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   561
            } else {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   562
                return syms.objectType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   563
            }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   564
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   565
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   566
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   567
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   568
     * Class representing shift operator helper that operates on integral operand types (either boxed
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   569
     * or unboxed). Operator lookup is performed after applying unary numeric promotion to each input type.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   570
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   571
    class BinaryShiftOperator extends BinaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   572
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   573
        BinaryShiftOperator(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   574
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   575
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   576
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   577
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   578
        public OperatorSymbol resolve(Type arg1, Type arg2) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   579
            return doLookup(unaryPromotion(arg1), unaryPromotion(arg2));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   580
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   581
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   582
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   583
        public boolean test(Type arg1, Type arg2) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   584
            TypeTag op1 = unaryPromotion(arg1).getTag();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   585
            TypeTag op2 = unaryPromotion(arg2).getTag();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   586
            return (op1 == TypeTag.LONG || op1 == TypeTag.INT) &&
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   587
                    (op2 == TypeTag.LONG || op2 == TypeTag.INT);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   588
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   589
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   590
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   591
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   592
     * This enum represent the possible kinds of an comparison test ('==' and '!=').
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   593
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   594
    enum ComparisonKind {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   595
        /** equality between numeric or boolean operands. */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   596
        NUMERIC_OR_BOOLEAN,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   597
        /** equality between reference operands. */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   598
        REFERENCE,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   599
        /** erroneous equality */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   600
        INVALID
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   601
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   602
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   603
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   604
     * Class representing equality operator helper that operates on either numeric, boolean or reference
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   605
     * types. Operator lookup for numeric/boolean equality test is performed after binary numeric
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   606
     * promotion to the input types. Operator lookup for reference equality test is performed assuming
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   607
     * the input type is 'Object'.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   608
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   609
    class BinaryEqualityOperator extends BinaryOperatorHelper {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   610
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   611
        BinaryEqualityOperator(Tag tag) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   612
            super(tag);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   613
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   614
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   615
        @Override
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   616
        public boolean test(Type arg1, Type arg2) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   617
            return getKind(arg1, arg2) != ComparisonKind.INVALID;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   618
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   619
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   620
        @Override
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   621
        public OperatorSymbol resolve(Type t1, Type t2) {
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   622
            ComparisonKind kind = getKind(t1, t2);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   623
            Type t = (kind == ComparisonKind.NUMERIC_OR_BOOLEAN) ?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   624
                    binaryPromotion(t1, t2) :
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   625
                    syms.objectType;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   626
            return doLookup(t, t);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   627
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   628
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   629
        /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   630
         * Retrieve the comparison kind associated with the given argument type pair.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   631
         */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   632
        private ComparisonKind getKind(Type arg1, Type arg2) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   633
            boolean arg1Primitive = arg1.isPrimitive();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   634
            boolean arg2Primitive = arg2.isPrimitive();
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   635
            if (arg1Primitive && arg2Primitive) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   636
                return ComparisonKind.NUMERIC_OR_BOOLEAN;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   637
            } else if (arg1Primitive) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   638
                return unaryPromotion(arg2).isPrimitive() ?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   639
                        ComparisonKind.NUMERIC_OR_BOOLEAN : ComparisonKind.INVALID;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   640
            } else if (arg2Primitive) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   641
                return unaryPromotion(arg1).isPrimitive() ?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   642
                        ComparisonKind.NUMERIC_OR_BOOLEAN : ComparisonKind.INVALID;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   643
            } else {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   644
                return arg1.isNullOrReference() && arg2.isNullOrReference() ?
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   645
                        ComparisonKind.REFERENCE : ComparisonKind.INVALID;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   646
            }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   647
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   648
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   649
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   650
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   651
     * Initialize all unary operators.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   652
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   653
    private void initUnaryOperators() {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   654
        initOperators(unaryOperators,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   655
                new UnaryNumericOperator(Tag.POS)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   656
                        .addUnaryOperator(DOUBLE, DOUBLE, nop)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   657
                        .addUnaryOperator(FLOAT, FLOAT, nop)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   658
                        .addUnaryOperator(LONG, LONG, nop)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   659
                        .addUnaryOperator(INT, INT, nop),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   660
                new UnaryNumericOperator(Tag.NEG)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   661
                        .addUnaryOperator(DOUBLE, DOUBLE, dneg)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   662
                        .addUnaryOperator(FLOAT, FLOAT, fneg)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   663
                        .addUnaryOperator(LONG, LONG, lneg)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   664
                        .addUnaryOperator(INT, INT, ineg),
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   665
                new UnaryNumericOperator(Tag.COMPL, Type::isIntegral)
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   666
                        .addUnaryOperator(LONG, LONG, lxor)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   667
                        .addUnaryOperator(INT, INT, ixor),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   668
                new UnaryPrefixPostfixOperator(Tag.POSTINC)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   669
                        .addUnaryOperator(DOUBLE, DOUBLE, dadd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   670
                        .addUnaryOperator(FLOAT, FLOAT, fadd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   671
                        .addUnaryOperator(LONG, LONG, ladd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   672
                        .addUnaryOperator(INT, INT, iadd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   673
                        .addUnaryOperator(CHAR, CHAR, iadd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   674
                        .addUnaryOperator(SHORT, SHORT, iadd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   675
                        .addUnaryOperator(BYTE, BYTE, iadd),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   676
                new UnaryPrefixPostfixOperator(Tag.POSTDEC)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   677
                        .addUnaryOperator(DOUBLE, DOUBLE, dsub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   678
                        .addUnaryOperator(FLOAT, FLOAT, fsub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   679
                        .addUnaryOperator(LONG, LONG, lsub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   680
                        .addUnaryOperator(INT, INT, isub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   681
                        .addUnaryOperator(CHAR, CHAR, isub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   682
                        .addUnaryOperator(SHORT, SHORT, isub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   683
                        .addUnaryOperator(BYTE, BYTE, isub),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   684
                new UnaryBooleanOperator(Tag.NOT)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   685
                        .addUnaryOperator(BOOLEAN, BOOLEAN, bool_not),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   686
                new UnaryReferenceOperator(Tag.NULLCHK)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   687
                        .addUnaryOperator(OBJECT, OBJECT, nullchk));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   688
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   689
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   690
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   691
     * Initialize all binary operators.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   692
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   693
    private void initBinaryOperators() {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   694
        initOperators(binaryOperators,
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   695
            new BinaryStringOperator(Tag.PLUS)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   696
                    .addBinaryOperator(STRING, OBJECT, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   697
                    .addBinaryOperator(OBJECT, STRING, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   698
                    .addBinaryOperator(STRING, STRING, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   699
                    .addBinaryOperator(STRING, INT, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   700
                    .addBinaryOperator(STRING, LONG, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   701
                    .addBinaryOperator(STRING, FLOAT, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   702
                    .addBinaryOperator(STRING, DOUBLE, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   703
                    .addBinaryOperator(STRING, BOOLEAN, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   704
                    .addBinaryOperator(STRING, BOT, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   705
                    .addBinaryOperator(INT, STRING, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   706
                    .addBinaryOperator(LONG, STRING, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   707
                    .addBinaryOperator(FLOAT, STRING, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   708
                    .addBinaryOperator(DOUBLE, STRING, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   709
                    .addBinaryOperator(BOOLEAN, STRING, STRING, string_add)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   710
                    .addBinaryOperator(BOT, STRING, STRING, string_add),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   711
            new BinaryNumericOperator(Tag.PLUS)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   712
                    .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dadd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   713
                    .addBinaryOperator(FLOAT, FLOAT, FLOAT, fadd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   714
                    .addBinaryOperator(LONG, LONG, LONG, ladd)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   715
                    .addBinaryOperator(INT, INT, INT, iadd),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   716
            new BinaryNumericOperator(Tag.MINUS)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   717
                    .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dsub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   718
                    .addBinaryOperator(FLOAT, FLOAT, FLOAT, fsub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   719
                    .addBinaryOperator(LONG, LONG, LONG, lsub)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   720
                    .addBinaryOperator(INT, INT, INT, isub),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   721
            new BinaryNumericOperator(Tag.MUL)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   722
                    .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dmul)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   723
                    .addBinaryOperator(FLOAT, FLOAT, FLOAT, fmul)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   724
                    .addBinaryOperator(LONG, LONG, LONG, lmul)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   725
                    .addBinaryOperator(INT, INT, INT, imul),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   726
            new BinaryNumericOperator(Tag.DIV)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   727
                    .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, ddiv)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   728
                    .addBinaryOperator(FLOAT, FLOAT, FLOAT, fdiv)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   729
                    .addBinaryOperator(LONG, LONG, LONG, ldiv)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   730
                    .addBinaryOperator(INT, INT, INT, idiv),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   731
            new BinaryNumericOperator(Tag.MOD)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   732
                    .addBinaryOperator(DOUBLE, DOUBLE, DOUBLE, dmod)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   733
                    .addBinaryOperator(FLOAT, FLOAT, FLOAT, fmod)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   734
                    .addBinaryOperator(LONG, LONG, LONG, lmod)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   735
                    .addBinaryOperator(INT, INT, INT, imod),
31114
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents: 29051
diff changeset
   736
            new BinaryBooleanOperator(Tag.BITAND)
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents: 29051
diff changeset
   737
                    .addBinaryOperator(BOOLEAN, BOOLEAN, BOOLEAN, iand),
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   738
            new BinaryNumericOperator(Tag.BITAND, Type::isIntegral)
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   739
                    .addBinaryOperator(LONG, LONG, LONG, land)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   740
                    .addBinaryOperator(INT, INT, INT, iand),
31114
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents: 29051
diff changeset
   741
            new BinaryBooleanOperator(Tag.BITOR)
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents: 29051
diff changeset
   742
                    .addBinaryOperator(BOOLEAN, BOOLEAN, BOOLEAN, ior),
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   743
            new BinaryNumericOperator(Tag.BITOR, Type::isIntegral)
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   744
                    .addBinaryOperator(LONG, LONG, LONG, lor)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   745
                    .addBinaryOperator(INT, INT, INT, ior),
31114
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents: 29051
diff changeset
   746
            new BinaryBooleanOperator(Tag.BITXOR)
d4045e8aaf1e 8082311: NPE when compiling expression with \"^\"
jlahoda
parents: 29051
diff changeset
   747
                    .addBinaryOperator(BOOLEAN, BOOLEAN, BOOLEAN, ixor),
33018
9790ed482dd0 8138840: NPE when compiling bitwise operations with illegal operand types
mcimadamore
parents: 31114
diff changeset
   748
            new BinaryNumericOperator(Tag.BITXOR, Type::isIntegral)
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   749
                    .addBinaryOperator(LONG, LONG, LONG, lxor)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   750
                    .addBinaryOperator(INT, INT, INT, ixor),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   751
            new BinaryShiftOperator(Tag.SL)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   752
                    .addBinaryOperator(INT, INT, INT, ishl)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   753
                    .addBinaryOperator(INT, LONG, INT, ishll)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   754
                    .addBinaryOperator(LONG, INT, LONG, lshl)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   755
                    .addBinaryOperator(LONG, LONG, LONG, lshll),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   756
            new BinaryShiftOperator(Tag.SR)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   757
                    .addBinaryOperator(INT, INT, INT, ishr)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   758
                    .addBinaryOperator(INT, LONG, INT, ishrl)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   759
                    .addBinaryOperator(LONG, INT, LONG, lshr)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   760
                    .addBinaryOperator(LONG, LONG, LONG, lshrl),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   761
            new BinaryShiftOperator(Tag.USR)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   762
                    .addBinaryOperator(INT, INT, INT, iushr)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   763
                    .addBinaryOperator(INT, LONG, INT, iushrl)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   764
                    .addBinaryOperator(LONG, INT, LONG, lushr)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   765
                    .addBinaryOperator(LONG, LONG, LONG, lushrl),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   766
            new BinaryNumericOperator(Tag.LT)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   767
                    .addBinaryOperator(DOUBLE, DOUBLE, BOOLEAN, dcmpg, iflt)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   768
                    .addBinaryOperator(FLOAT, FLOAT, BOOLEAN, fcmpg, iflt)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   769
                    .addBinaryOperator(LONG, LONG, BOOLEAN, lcmp, iflt)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   770
                    .addBinaryOperator(INT, INT, BOOLEAN, if_icmplt),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   771
            new BinaryNumericOperator(Tag.GT)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   772
                    .addBinaryOperator(DOUBLE, DOUBLE, BOOLEAN, dcmpl, ifgt)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   773
                    .addBinaryOperator(FLOAT, FLOAT, BOOLEAN, fcmpl, ifgt)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   774
                    .addBinaryOperator(LONG, LONG, BOOLEAN, lcmp, ifgt)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   775
                    .addBinaryOperator(INT, INT, BOOLEAN, if_icmpgt),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   776
            new BinaryNumericOperator(Tag.LE)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   777
                    .addBinaryOperator(DOUBLE, DOUBLE, BOOLEAN, dcmpg, ifle)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   778
                    .addBinaryOperator(FLOAT, FLOAT, BOOLEAN, fcmpg, ifle)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   779
                    .addBinaryOperator(LONG, LONG, BOOLEAN, lcmp, ifle)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   780
                    .addBinaryOperator(INT, INT, BOOLEAN, if_icmple),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   781
            new BinaryNumericOperator(Tag.GE)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   782
                    .addBinaryOperator(DOUBLE, DOUBLE, BOOLEAN, dcmpl, ifge)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   783
                    .addBinaryOperator(FLOAT, FLOAT, BOOLEAN, fcmpl, ifge)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   784
                    .addBinaryOperator(LONG, LONG, BOOLEAN, lcmp, ifge)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   785
                    .addBinaryOperator(INT, INT, BOOLEAN, if_icmpge),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   786
            new BinaryEqualityOperator(Tag.EQ)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   787
                    .addBinaryOperator(OBJECT, OBJECT, BOOLEAN, if_acmpeq)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   788
                    .addBinaryOperator(BOOLEAN, BOOLEAN, BOOLEAN, if_icmpeq)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   789
                    .addBinaryOperator(DOUBLE, DOUBLE, BOOLEAN, dcmpl, ifeq)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   790
                    .addBinaryOperator(FLOAT, FLOAT, BOOLEAN, fcmpl, ifeq)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   791
                    .addBinaryOperator(LONG, LONG, BOOLEAN, lcmp, ifeq)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   792
                    .addBinaryOperator(INT, INT, BOOLEAN, if_icmpeq),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   793
            new BinaryEqualityOperator(Tag.NE)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   794
                    .addBinaryOperator(OBJECT, OBJECT, BOOLEAN, if_acmpne)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   795
                    .addBinaryOperator(BOOLEAN, BOOLEAN, BOOLEAN, if_icmpne)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   796
                    .addBinaryOperator(DOUBLE, DOUBLE, BOOLEAN, dcmpl, ifne)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   797
                    .addBinaryOperator(FLOAT, FLOAT, BOOLEAN, fcmpl, ifne)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   798
                    .addBinaryOperator(LONG, LONG, BOOLEAN, lcmp, ifne)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   799
                    .addBinaryOperator(INT, INT, BOOLEAN, if_icmpne),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   800
            new BinaryBooleanOperator(Tag.AND)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   801
                    .addBinaryOperator(BOOLEAN, BOOLEAN, BOOLEAN, bool_and),
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   802
            new BinaryBooleanOperator(Tag.OR)
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   803
                    .addBinaryOperator(BOOLEAN, BOOLEAN, BOOLEAN, bool_or));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   804
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   805
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   806
    OperatorSymbol lookupBinaryOp(Predicate<OperatorSymbol> applicabilityTest) {
39806
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
   807
        return binaryOperators.values().stream()
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
   808
                .flatMap(List::stream)
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
   809
                .map(helper -> helper.doLookup(applicabilityTest))
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
   810
                .distinct()
39920
4923274643f2 8161708: javac, consider a different way to handle access code for operators
vromero
parents: 39806
diff changeset
   811
                .filter(sym -> sym != noOpSymbol)
39806
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
   812
                .findFirst().get();
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
   813
    }
d3a13ca6013e 8161383: javac is looking for operator symbols at the wrong place
vromero
parents: 33018
diff changeset
   814
29051
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   815
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   816
     * Complete the initialization of an operator helper by storing it into the corresponding operator map.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   817
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   818
    @SafeVarargs
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   819
    private final <O extends OperatorHelper> void initOperators(Map<Name, List<O>> opsMap, O... ops) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   820
        for (O o : ops) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   821
            Name opName = o.name;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   822
            List<O> helpers = opsMap.getOrDefault(opName, List.nil());
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   823
            opsMap.put(opName, helpers.prepend(o));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   824
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   825
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   826
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   827
    /**
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   828
     * Initialize operator name array.
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   829
     */
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   830
    private void initOperatorNames() {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   831
        setOperatorName(Tag.POS, "+");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   832
        setOperatorName(Tag.NEG, "-");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   833
        setOperatorName(Tag.NOT, "!");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   834
        setOperatorName(Tag.COMPL, "~");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   835
        setOperatorName(Tag.PREINC, "++");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   836
        setOperatorName(Tag.PREDEC, "--");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   837
        setOperatorName(Tag.POSTINC, "++");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   838
        setOperatorName(Tag.POSTDEC, "--");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   839
        setOperatorName(Tag.NULLCHK, "<*nullchk*>");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   840
        setOperatorName(Tag.OR, "||");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   841
        setOperatorName(Tag.AND, "&&");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   842
        setOperatorName(Tag.EQ, "==");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   843
        setOperatorName(Tag.NE, "!=");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   844
        setOperatorName(Tag.LT, "<");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   845
        setOperatorName(Tag.GT, ">");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   846
        setOperatorName(Tag.LE, "<=");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   847
        setOperatorName(Tag.GE, ">=");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   848
        setOperatorName(Tag.BITOR, "|");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   849
        setOperatorName(Tag.BITXOR, "^");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   850
        setOperatorName(Tag.BITAND, "&");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   851
        setOperatorName(Tag.SL, "<<");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   852
        setOperatorName(Tag.SR, ">>");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   853
        setOperatorName(Tag.USR, ">>>");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   854
        setOperatorName(Tag.PLUS, "+");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   855
        setOperatorName(Tag.MINUS, names.hyphen);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   856
        setOperatorName(Tag.MUL, names.asterisk);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   857
        setOperatorName(Tag.DIV, names.slash);
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   858
        setOperatorName(Tag.MOD, "%");
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   859
    }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   860
    //where
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   861
        private void setOperatorName(Tag tag, String name) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   862
            setOperatorName(tag, names.fromString(name));
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   863
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   864
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   865
        private void setOperatorName(Tag tag, Name name) {
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   866
            opname[tag.operatorIndex()] = name;
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   867
        }
7244db2ab176 8071241: Investigate alternate strategy for type-checking operators
mcimadamore
parents:
diff changeset
   868
}