src/share/classes/com/sun/tools/javac/comp/TransTypes.java

Fri, 05 Oct 2012 14:35:24 +0100

author
mcimadamore
date
Fri, 05 Oct 2012 14:35:24 +0100
changeset 1348
573ceb23beeb
parent 1305
9d47f4850714
child 1352
d4b3cb1ece84
permissions
-rw-r--r--

7177385: Add attribution support for lambda expressions
Summary: Add support for function descriptor lookup, functional interface inference and lambda expression type-checking
Reviewed-by: jjg, dlsmith

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

mercurial