langtools/src/share/classes/com/sun/tools/javac/comp/TransTypes.java
author jjg
Tue, 16 Sep 2008 18:35:18 -0700
changeset 1260 a772ba9ba43d
parent 735 372aa565a221
child 1356 9f40f16e543b
permissions -rw-r--r--
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes Reviewed-by: darcy, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 507
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.comp;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import static com.sun.tools.javac.code.Flags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import static com.sun.tools.javac.code.Kinds.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
/** This pass translates Generic Java to conventional Java.
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
public class TransTypes extends TreeTranslator {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    /** The context key for the TransTypes phase. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    protected static final Context.Key<TransTypes> transTypesKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        new Context.Key<TransTypes>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /** Get the instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public static TransTypes instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        TransTypes instance = context.get(transTypesKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            instance = new TransTypes(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 735
diff changeset
    62
    private Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    private Symtab syms;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    private TreeMaker make;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    private Enter enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    private boolean allowEnums;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private Types types;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    private final Resolve resolve;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * Flag to indicate whether or not to generate bridge methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * For pre-Tiger source there is no need for bridge methods, so it
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * can be skipped to get better performance for -source 1.4 etc.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    private final boolean addBridges;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    protected TransTypes(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        context.put(transTypesKey, this);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 735
diff changeset
    80
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        log = Log.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        enter = Enter.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        overridden = new HashMap<MethodSymbol,MethodSymbol>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        Source source = Source.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        allowEnums = source.allowEnums();
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        addBridges = source.addBridges();
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        make = TreeMaker.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        resolve = Resolve.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /** A hashtable mapping bridge methods to the methods they override after
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     *  type erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    Map<MethodSymbol,MethodSymbol> overridden;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    /** Construct an attributed tree for a cast of expression to target type,
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     *  unless it already has precisely that type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     *  @param tree    The expression tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     *  @param target  The target type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    JCExpression cast(JCExpression tree, Type target) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        int oldpos = make.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        make.at(tree.pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        if (!types.isSameType(tree.type, target)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            if (!resolve.isAccessible(env, target.tsym))
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                resolve.logAccessError(env, tree, target);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            tree = make.TypeCast(make.Type(target), tree).setType(target);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        make.pos = oldpos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    /** Construct an attributed tree to coerce an expression to some erased
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
     *  target type, unless the expression is already assignable to that type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     *  If target type is a constant type, use its base type instead.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *  @param tree    The expression tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     *  @param target  The target type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    JCExpression coerce(JCExpression tree, Type target) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        Type btarget = target.baseType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if (tree.type.isPrimitive() == target.isPrimitive()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            return types.isAssignable(tree.type, btarget, Warner.noWarnings)
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                ? tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                : cast(tree, btarget);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    /** Given an erased reference type, assume this type as the tree's type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     *  Then, coerce to some given target type unless target type is null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     *  This operation is used in situations like the following:
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     *  class Cell<A> { A value; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     *  ...
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     *  Cell<Integer> cell;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     *  Integer x = cell.value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     *  Since the erasure of Cell.value is Object, but the type
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     *  of cell.value in the assignment is Integer, we need to
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     *  adjust the original type of cell.value to Object, and insert
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     *  a cast to Integer. That is, the last assignment becomes:
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
     *  Integer x = (Integer)cell.value;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     *  @param tree       The expression tree whose type might need adjustment.
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     *  @param erasedType The expression's type after erasure.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  @param target     The target type, which is usually the erasure of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     *                    expression's original type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    JCExpression retype(JCExpression tree, Type erasedType, Type target) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
//      System.err.println("retype " + tree + " to " + erasedType);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        if (erasedType.tag > lastBaseTag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
            if (target != null && target.isPrimitive())
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                target = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
            tree.type = erasedType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
            if (target != null) return coerce(tree, target);
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    /** Translate method argument list, casting each argument
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     *  to its corresponding type in a list of target types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     *  @param _args            The method argument list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     *  @param parameters       The list of target types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     *  @param varargsElement   The erasure of the varargs element type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     *  or null if translating a non-varargs invocation
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    <T extends JCTree> List<T> translateArgs(List<T> _args,
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                                           List<Type> parameters,
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                                           Type varargsElement) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        if (parameters.isEmpty()) return _args;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        List<T> args = _args;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        while (parameters.tail.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            args.head = translate(args.head, parameters.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            args = args.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            parameters = parameters.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        Type parameter = parameters.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        assert varargsElement != null || args.length() == 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        if (varargsElement != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            while (args.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                args.head = translate(args.head, varargsElement);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
                args = args.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
            args.head = translate(args.head, parameter);
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        return _args;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    /** Add a bridge definition and enter corresponding method symbol in
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     *  local scope of origin.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     *  @param pos     The source code position to be used for the definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     *  @param meth    The method for which a bridge needs to be added
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     *  @param impl    That method's implementation (possibly the method itself)
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     *  @param origin  The class to which the bridge will be added
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     *  @param hypothetical
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     *                 True if the bridge method is not strictly necessary in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     *                 binary, but is represented in the symbol table to detect
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     *                 erasure clashes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     *  @param bridges The list buffer to which the bridge will be added
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    void addBridge(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                   MethodSymbol meth,
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                   MethodSymbol impl,
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                   ClassSymbol origin,
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                   boolean hypothetical,
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                   ListBuffer<JCTree> bridges) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        make.at(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        Type origType = types.memberType(origin.type, meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        Type origErasure = erasure(origType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        // Create a bridge method symbol and a bridge definition without a body.
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        Type bridgeType = meth.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        if (hypothetical) flags |= HYPOTHETICAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        MethodSymbol bridge = new MethodSymbol(flags,
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                                               meth.name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                                               bridgeType,
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                                               origin);
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        if (!hypothetical) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
            JCMethodDecl md = make.MethodDef(bridge, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            // The bridge calls this.impl(..), if we have an implementation
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            // in the current class, super.impl(...) otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            JCExpression receiver = (impl.owner == origin)
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                ? make.This(origin.erasure(types))
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                : make.Super(types.supertype(origin.type).tsym.erasure(types), origin);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            // The type returned from the original method.
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            Type calltype = erasure(impl.type.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            // Construct a call of  this.impl(params), or super.impl(params),
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            // casting params and possibly results as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            JCExpression call =
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                make.Apply(
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                           null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                           make.Select(receiver, impl).setType(calltype),
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                           translateArgs(make.Idents(md.params), origErasure.getParameterTypes(), null))
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                .setType(calltype);
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            JCStatement stat = (origErasure.getReturnType().tag == VOID)
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                ? make.Exec(call)
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                : make.Return(coerce(call, bridgeType.getReturnType()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            md.body = make.Block(0, List.of(stat));
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            // Add bridge to `bridges' buffer
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            bridges.append(md);
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        // Add bridge to scope of enclosing class and `overridden' table.
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        origin.members().enter(bridge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        overridden.put(bridge, meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    /** Add bridge if given symbol is a non-private, non-static member
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
     *  of the given class, which is either defined in the class or non-final
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     *  inherited, and one of the two following conditions holds:
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     *  1. The method's type changes in the given class, as compared to the
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     *     class where the symbol was defined, (in this case
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     *     we have extended a parameterized class with non-trivial parameters).
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     *  2. The method has an implementation with a different erased return type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     *     (in this case we have used co-variant returns).
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     *  If a bridge already exists in some other class, no new bridge is added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     *  Instead, it is checked that the bridge symbol overrides the method symbol.
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     *  (Spec ???).
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     *  todo: what about bridges for privates???
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     *  @param pos     The source code position to be used for the definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     *  @param sym     The symbol for which a bridge might have to be added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     *  @param origin  The class in which the bridge would go.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     *  @param bridges The list buffer to which the bridge would be added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    void addBridgeIfNeeded(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                           Symbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                           ClassSymbol origin,
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                           ListBuffer<JCTree> bridges) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        if (sym.kind == MTH &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            sym.name != names.init &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            (sym.flags() & (PRIVATE | SYNTHETIC | STATIC)) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            sym.isMemberOf(origin, types))
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
            MethodSymbol meth = (MethodSymbol)sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
            MethodSymbol bridge = meth.binaryImplementation(origin, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
            MethodSymbol impl = meth.implementation(origin, types, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            if (bridge == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                bridge == meth ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                (impl != null && !bridge.owner.isSubClass(impl.owner, types))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                // No bridge was added yet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                if (impl != null && isBridgeNeeded(meth, impl, origin.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                    addBridge(pos, meth, impl, origin, bridge==impl, bridges);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                } else if (impl == meth
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                           && impl.owner != origin
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                           && (impl.flags() & FINAL) == 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                           && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                           && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                    // this is to work around a horrible but permanent
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                    // reflection design error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                    addBridge(pos, meth, impl, origin, false, bridges);
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            } else if ((bridge.flags() & SYNTHETIC) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
                MethodSymbol other = overridden.get(bridge);
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
                if (other != null && other != meth) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
                    if (impl == null || !impl.overrides(other, origin, types, true)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                        // Bridge for other symbol pair was added
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                        log.error(pos, "name.clash.same.erasure.no.override",
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
                                  other, other.location(origin.type, types),
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
                                  meth,  meth.location(origin.type, types));
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            } else if (!bridge.overrides(meth, origin, types, true)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                // Accidental binary override without source override.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                if (bridge.owner == origin ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
                    types.asSuper(bridge.owner.type, meth.owner) == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                    // Don't diagnose the problem if it would already
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                    // have been reported in the superclass
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
                    log.error(pos, "name.clash.same.erasure.no.override",
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                              bridge, bridge.location(origin.type, types),
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                              meth,  meth.location(origin.type, types));
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
         * @param method The symbol for which a bridge might have to be added
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
         * @param impl The implementation of method
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
         * @param dest The type in which the bridge would go
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        private boolean isBridgeNeeded(MethodSymbol method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                                       MethodSymbol impl,
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                                       Type dest) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            if (impl != method) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                // If either method or impl have different erasures as
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                // members of dest, a bridge is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                Type method_erasure = method.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                if (!isSameMemberWhenErased(dest, method, method_erasure))
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                Type impl_erasure = impl.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                if (!isSameMemberWhenErased(dest, impl, impl_erasure))
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                // If the erasure of the return type is different, a
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                // bridge is needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                return !types.isSameType(impl_erasure.getReturnType(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                                         method_erasure.getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
               // method and impl are the same...
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                if ((method.flags() & ABSTRACT) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                    // ...and abstract so a bridge is not needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                    // Concrete subclasses will bridge as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                // The erasure of the return type is always the same
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                // for the same symbol.  Reducing the three tests in
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                // the other branch to just one:
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                return !isSameMemberWhenErased(dest, method, method.erasure(types));
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
         * Lookup the method as a member of the type.  Compare the
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
         * erasures.
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
         * @param type the class where to look for the method
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
         * @param method the method to look for in class
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
         * @param erasure the erasure of method
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        private boolean isSameMemberWhenErased(Type type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                                               MethodSymbol method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                                               Type erasure) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
            return types.isSameType(erasure(types.memberType(type, method)),
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                                    erasure);
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    void addBridges(DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                    TypeSymbol i,
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                    ClassSymbol origin,
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                    ListBuffer<JCTree> bridges) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
        for (Scope.Entry e = i.members().elems; e != null; e = e.sibling)
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
            addBridgeIfNeeded(pos, e.sym, origin, bridges);
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            addBridges(pos, l.head.tsym, origin, bridges);
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    /** Add all necessary bridges to some class appending them to list buffer.
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
     *  @param pos     The source code position to be used for the bridges.
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
     *  @param origin  The class in which the bridges go.
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
     *  @param bridges The list buffer to which the bridges are added.
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
    void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
        Type st = types.supertype(origin.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        while (st.tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
//          if (isSpecialization(st))
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            addBridges(pos, st.tsym, origin, bridges);
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            st = types.supertype(st);
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
        for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
//          if (isSpecialization(l.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            addBridges(pos, l.head.tsym, origin, bridges);
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
/* ************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
 * Visitor methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
    /** Visitor argument: proto-type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
    private Type pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
    /** Visitor method: perform a type translation on tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
    public <T extends JCTree> T translate(T tree, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        Type prevPt = this.pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
            this.pt = pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            return translate(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            this.pt = prevPt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    /** Visitor method: perform a type translation on list of trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
    public <T extends JCTree> List<T> translate(List<T> trees, Type pt) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        Type prevPt = this.pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        List<T> res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            this.pt = pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            res = translate(trees);
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
            this.pt = prevPt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        translateClass(tree.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    JCMethodDecl currentMethod = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
    public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        JCMethodDecl previousMethod = currentMethod;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            currentMethod = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
            tree.restype = translate(tree.restype, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
            tree.typarams = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            tree.params = translateVarDefs(tree.params);
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
            tree.thrown = translate(tree.thrown, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
            tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
            tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
            result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
            currentMethod = previousMethod;
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        // Check that we do not introduce a name clash by erasing types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
             e.sym != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
             e = e.next()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
            if (e.sym != tree.sym &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
                types.isSameType(erasure(e.sym.type), tree.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                log.error(tree.pos(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
                          "name.clash.same.erasure", tree.sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
                          e.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
                return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
        tree.vartype = translate(tree.vartype, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        tree.init = translate(tree.init, tree.sym.erasure(types));
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    public void visitDoLoop(JCDoWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
        tree.body = translate(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
        tree.cond = translate(tree.cond, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
    public void visitWhileLoop(JCWhileLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        tree.cond = translate(tree.cond, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
        tree.body = translate(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
    public void visitForLoop(JCForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
        tree.init = translate(tree.init, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
        if (tree.cond != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            tree.cond = translate(tree.cond, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        tree.step = translate(tree.step, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        tree.body = translate(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    public void visitForeachLoop(JCEnhancedForLoop tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
        tree.var = translate(tree.var, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        Type iterableType = tree.expr.type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
        tree.expr = translate(tree.expr, erasure(tree.expr.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        if (types.elemtype(tree.expr.type) == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            tree.expr.type = iterableType; // preserve type for Lower
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        tree.body = translate(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    public void visitSwitch(JCSwitch tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        Type selsuper = types.supertype(tree.selector.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        boolean enumSwitch = selsuper != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
            selsuper.tsym == syms.enumSym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
        Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        tree.selector = translate(tree.selector, target);
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        tree.cases = translateCases(tree.cases);
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
    public void visitCase(JCCase tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        tree.pat = translate(tree.pat, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
        tree.stats = translate(tree.stats);
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
    public void visitSynchronized(JCSynchronized tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        tree.lock = translate(tree.lock, erasure(tree.lock.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        tree.body = translate(tree.body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
    public void visitConditional(JCConditional tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
        tree.cond = translate(tree.cond, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        tree.truepart = translate(tree.truepart, erasure(tree.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        tree.falsepart = translate(tree.falsepart, erasure(tree.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
   public void visitIf(JCIf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
        tree.cond = translate(tree.cond, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
        tree.thenpart = translate(tree.thenpart);
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
        tree.elsepart = translate(tree.elsepart);
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
    public void visitExec(JCExpressionStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
        tree.expr = translate(tree.expr, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
    public void visitReturn(JCReturn tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
        tree.expr = translate(tree.expr, currentMethod.sym.erasure(types).getReturnType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
    public void visitThrow(JCThrow tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        tree.expr = translate(tree.expr, erasure(tree.expr.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
    public void visitAssert(JCAssert tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
        tree.cond = translate(tree.cond, syms.booleanType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        if (tree.detail != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            tree.detail = translate(tree.detail, erasure(tree.detail.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
    public void visitApply(JCMethodInvocation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        tree.meth = translate(tree.meth, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
        Symbol meth = TreeInfo.symbol(tree.meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
        Type mt = meth.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
        List<Type> argtypes = mt.getParameterTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        if (allowEnums &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
            meth.name==names.init &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
            meth.owner == syms.enumSym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            argtypes = argtypes.tail.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
        if (tree.varargsElement != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
            tree.varargsElement = types.erasure(tree.varargsElement);
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
            assert tree.args.length() == argtypes.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
        tree.args = translateArgs(tree.args, argtypes, tree.varargsElement);
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        // Insert casts of method invocation results as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        result = retype(tree, mt.getReturnType(), pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
    public void visitNewClass(JCNewClass tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        if (tree.encl != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            tree.encl = translate(tree.encl, erasure(tree.encl.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
        tree.clazz = translate(tree.clazz, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        if (tree.varargsElement != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            tree.varargsElement = types.erasure(tree.varargsElement);
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
        tree.args = translateArgs(
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
            tree.args, tree.constructor.erasure(types).getParameterTypes(), tree.varargsElement);
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        tree.def = translate(tree.def, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
    public void visitNewArray(JCNewArray tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
        tree.elemtype = translate(tree.elemtype, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        translate(tree.dims, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
        tree.elems = translate(tree.elems,
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                               (tree.type == null) ? null
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
                               : erasure(types.elemtype(tree.type)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
    public void visitParens(JCParens tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
        tree.expr = translate(tree.expr, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
    public void visitAssign(JCAssign tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
        tree.lhs = translate(tree.lhs, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
        tree.rhs = translate(tree.rhs, erasure(tree.lhs.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
    public void visitAssignop(JCAssignOp tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
        tree.lhs = translate(tree.lhs, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        tree.rhs = translate(tree.rhs, erasure(tree.rhs.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
    public void visitUnary(JCUnary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
        tree.arg = translate(tree.arg, tree.operator.type.getParameterTypes().head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    public void visitBinary(JCBinary tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
        tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
    public void visitTypeCast(JCTypeCast tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        tree.clazz = translate(tree.clazz, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
        tree.expr = translate(tree.expr, tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
    public void visitTypeTest(JCInstanceOf tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
        tree.expr = translate(tree.expr, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
        tree.clazz = translate(tree.clazz, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
    public void visitIndexed(JCArrayAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
        tree.indexed = translate(tree.indexed, erasure(tree.indexed.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        tree.index = translate(tree.index, syms.intType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
        // Insert casts of indexed expressions as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
        result = retype(tree, types.elemtype(tree.indexed.type), pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
    // There ought to be nothing to rewrite here;
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
    // we don't generate code.
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
    public void visitAnnotation(JCAnnotation tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
    public void visitIdent(JCIdent tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
        Type et = tree.sym.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
        // Map type variables to their bounds.
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
        if (tree.sym.kind == TYP && tree.sym.type.tag == TYPEVAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
            result = make.at(tree.pos).Type(et);
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        // Map constants expressions to themselves.
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        if (tree.type.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
            result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
        // Insert casts of variable uses as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        else if (tree.sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
            result = retype(tree, et, pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
            tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
    public void visitSelect(JCFieldAccess tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
        Type t = tree.selected.type;
507
bdd92cc35ac4 6531075: Missing synthetic casts when accessing fields/methods of intersection types including type variables
mcimadamore
parents: 10
diff changeset
   693
        while (t.tag == TYPEVAR)
bdd92cc35ac4 6531075: Missing synthetic casts when accessing fields/methods of intersection types including type variables
mcimadamore
parents: 10
diff changeset
   694
            t = t.getUpperBound();
bdd92cc35ac4 6531075: Missing synthetic casts when accessing fields/methods of intersection types including type variables
mcimadamore
parents: 10
diff changeset
   695
        if (t.isCompound()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
            if ((tree.sym.flags() & IPROXY) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
                tree.sym = ((MethodSymbol)tree.sym).
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
                    implemented((TypeSymbol)tree.sym.owner, types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            tree.selected = cast(
507
bdd92cc35ac4 6531075: Missing synthetic casts when accessing fields/methods of intersection types including type variables
mcimadamore
parents: 10
diff changeset
   701
                translate(tree.selected, erasure(tree.selected.type)),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
                erasure(tree.sym.owner.type));
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
            tree.selected = translate(tree.selected, erasure(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        // Map constants expressions to themselves.
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        if (tree.type.constValue() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
            result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
        // Insert casts of variable uses as needed.
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        else if (tree.sym.kind == VAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
            result = retype(tree, tree.sym.erasure(types), pt);
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
            result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
    public void visitTypeArray(JCArrayTypeTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
        tree.elemtype = translate(tree.elemtype, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
        tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
        result = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
    /** Visitor method for parameterized types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
    public void visitTypeApply(JCTypeApply tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
        // Delete all type parameters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
        result = translate(tree.clazz, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
/**************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
 * utility methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
    private Type erasure(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
        return types.erasure(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
/**************************************************************************
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
 * main method
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
 *************************************************************************/
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
    private Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
    void translateClass(ClassSymbol c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
        Type st = types.supertype(c.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
        // process superclass before derived
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
        if (st.tag == CLASS)
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
            translateClass((ClassSymbol)st.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
        Env<AttrContext> myEnv = enter.typeEnvs.remove(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        if (myEnv == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
        Env<AttrContext> oldEnv = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
            env = myEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            // class has not been translated yet
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            TreeMaker savedMake = make;
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
            Type savedPt = pt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
            make = make.forToplevel(env.toplevel);
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            pt = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
                JCClassDecl tree = (JCClassDecl) env.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
                tree.typarams = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
                super.visitClassDef(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
                make.at(tree.pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
                if (addBridges) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
                    ListBuffer<JCTree> bridges = new ListBuffer<JCTree>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
                    if ((tree.sym.flags() & INTERFACE) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
                        addBridges(tree.pos(), tree.sym, bridges);
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
                    tree.defs = bridges.toList().prependList(tree.defs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
                tree.type = erasure(tree.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
            } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
                make = savedMake;
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
                pt = savedPt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
            env = oldEnv;
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
    /** Translate a toplevel class definition.
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
     *  @param cdef    The definition to be translated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
    public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
        // note that this method does NOT support recursion.
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        this.make = make;
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
        pt = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
        return translate(cdef, null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
}