1 /*
  2  * Copyright (c) 1999, 2022, 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  */
 25 
 26 package com.sun.tools.javac.comp;
 27 
 28 
 29 import com.sun.tools.javac.code.*;
 30 import com.sun.tools.javac.code.Attribute.TypeCompound;
 31 import com.sun.tools.javac.code.Source.Feature;
 32 import com.sun.tools.javac.code.Symbol.*;
 33 import com.sun.tools.javac.tree.*;
 34 import com.sun.tools.javac.tree.JCTree.*;
 35 import com.sun.tools.javac.tree.JCTree.JCMemberReference.ReferenceKind;
 36 import com.sun.tools.javac.util.*;
 37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
 38 import com.sun.tools.javac.util.List;
 39 
 40 import static com.sun.tools.javac.code.Flags.*;
 41 import static com.sun.tools.javac.code.Kinds.Kind.*;
 42 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
 43 import static com.sun.tools.javac.code.TypeTag.CLASS;
 44 import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
 45 import static com.sun.tools.javac.code.TypeTag.VOID;
 46 import static com.sun.tools.javac.comp.CompileStates.CompileState;
 47 import com.sun.tools.javac.tree.JCTree.JCBreak;
 48 
 49 /** This pass translates Generic Java to conventional Java.
 50  *
 51  *  <p><b>This is NOT part of any supported API.
 52  *  If you write code that depends on this, you do so at your own risk.
 53  *  This code and its internal interfaces are subject to change or
 54  *  deletion without notice.</b>
 55  */
 56 public class TransTypes extends TreeTranslator {
 57     /** The context key for the TransTypes phase. */
 58     protected static final Context.Key<TransTypes> transTypesKey = new Context.Key<>();
 59 
 60     /** Get the instance for this context. */
 61     public static TransTypes instance(Context context) {
 62         TransTypes instance = context.get(transTypesKey);
 63         if (instance == null)
 64             instance = new TransTypes(context);
 65         return instance;
 66     }
 67 
 68     private Names names;
 69     private Log log;
 70     private Symtab syms;
 71     private TreeMaker make;
 72     private Enter enter;
 73     private Types types;
 74     private Annotate annotate;
 75     private Attr attr;
 76     private final Resolve resolve;
 77     private final CompileStates compileStates;
 78 
 79     @SuppressWarnings("this-escape")
 80     protected TransTypes(Context context) {
 81         context.put(transTypesKey, this);
 82         compileStates = CompileStates.instance(context);
 83         names = Names.instance(context);
 84         log = Log.instance(context);
 85         syms = Symtab.instance(context);
 86         enter = Enter.instance(context);
 87         types = Types.instance(context);
 88         make = TreeMaker.instance(context);
 89         resolve = Resolve.instance(context);
 90         annotate = Annotate.instance(context);
 91         attr = Attr.instance(context);
 92     }
 93 
 94     /** Construct an attributed tree for a cast of expression to target type,
 95      *  unless it already has precisely that type.
 96      *  @param tree    The expression tree.
 97      *  @param target  The target type.
 98      */
 99     JCExpression cast(JCExpression tree, Type target) {
100         int oldpos = make.pos;
101         make.at(tree.pos);
102         if (!types.isSameType(tree.type, target)) {
103             if (!resolve.isAccessible(env, target.tsym))
104                 resolve.logAccessErrorInternal(env, tree, target);
105             tree = make.TypeCast(make.Type(target), tree).setType(target);
106         }
107         make.pos = oldpos;
108         return tree;
109     }
110 
111     /** Construct an attributed tree to coerce an expression to some erased
112      *  target type, unless the expression is already assignable to that type.
113      *  If target type is a constant type, use its base type instead.
114      *  @param tree    The expression tree.
115      *  @param target  The target type.
116      */
117     public JCExpression coerce(Env<AttrContext> env, JCExpression tree, Type target) {
118         Env<AttrContext> prevEnv = this.env;
119         try {
120             this.env = env;
121             return coerce(tree, target);
122         }
123         finally {
124             this.env = prevEnv;
125         }
126     }
127     JCExpression coerce(JCExpression tree, Type target) {
128         Type btarget = target.baseType();
129         if (tree.type.isPrimitive() == target.isPrimitive()) {
130             return types.isAssignable(tree.type, btarget, types.noWarnings)
131                 ? tree
132                 : cast(tree, btarget);
133         }
134         return tree;
135     }
136 
137     /** Given an erased reference type, assume this type as the tree's type.
138      *  Then, coerce to some given target type unless target type is null.
139      *  This operation is used in situations like the following:
140      *
141      *  <pre>{@code
142      *  class Cell<A> { A value; }
143      *  ...
144      *  Cell<Integer> cell;
145      *  Integer x = cell.value;
146      *  }</pre>
147      *
148      *  Since the erasure of Cell.value is Object, but the type
149      *  of cell.value in the assignment is Integer, we need to
150      *  adjust the original type of cell.value to Object, and insert
151      *  a cast to Integer. That is, the last assignment becomes:
152      *
153      *  <pre>{@code
154      *  Integer x = (Integer)cell.value;
155      *  }</pre>
156      *
157      *  @param tree       The expression tree whose type might need adjustment.
158      *  @param erasedType The expression's type after erasure.
159      *  @param target     The target type, which is usually the erasure of the
160      *                    expression's original type.
161      */
162     JCExpression retype(JCExpression tree, Type erasedType, Type target) {
163 //      System.err.println("retype " + tree + " to " + erasedType);//DEBUG
164         if (!erasedType.isPrimitive()) {
165             if (target != null && target.isPrimitive()) {
166                 target = erasure(tree.type);
167             }
168             tree.type = erasedType;
169             if (target != null) {
170                 return coerce(tree, target);
171             }
172         }
173         return tree;
174     }
175 
176     /** Translate method argument list, casting each argument
177      *  to its corresponding type in a list of target types.
178      *  @param _args            The method argument list.
179      *  @param parameters       The list of target types.
180      *  @param varargsElement   The erasure of the varargs element type,
181      *  or null if translating a non-varargs invocation
182      */
183     <T extends JCTree> List<T> translateArgs(List<T> _args,
184                                            List<Type> parameters,
185                                            Type varargsElement) {
186         if (parameters.isEmpty()) return _args;
187         List<T> args = _args;
188         while (parameters.tail.nonEmpty()) {
189             args.head = translate(args.head, parameters.head);
190             args = args.tail;
191             parameters = parameters.tail;
192         }
193         Type parameter = parameters.head;
194         Assert.check(varargsElement != null || args.length() == 1);
195         if (varargsElement != null) {
196             while (args.nonEmpty()) {
197                 args.head = translate(args.head, varargsElement);
198                 args = args.tail;
199             }
200         } else {
201             args.head = translate(args.head, parameter);
202         }
203         return _args;
204     }
205 
206     public <T extends JCTree> List<T> translateArgs(List<T> _args,
207                                            List<Type> parameters,
208                                            Type varargsElement,
209                                            Env<AttrContext> localEnv) {
210         Env<AttrContext> prevEnv = env;
211         try {
212             env = localEnv;
213             return translateArgs(_args, parameters, varargsElement);
214         }
215         finally {
216             env = prevEnv;
217         }
218     }
219 
220     /** Add a bridge definition and enter corresponding method symbol in
221      *  local scope of origin.
222      *
223      *  @param pos     The source code position to be used for the definition.
224      *  @param meth    The method for which a bridge needs to be added
225      *  @param impl    That method's implementation (possibly the method itself)
226      *  @param origin  The class to which the bridge will be added
227      *  @param bridges The list buffer to which the bridge will be added
228      */
229     void addBridge(DiagnosticPosition pos,
230                    MethodSymbol meth,
231                    MethodSymbol impl,
232                    ClassSymbol origin,
233                    ListBuffer<JCTree> bridges) {
234         make.at(pos);
235         Type implTypeErasure = erasure(impl.type);
236 
237         // Create a bridge method symbol and a bridge definition without a body.
238         Type bridgeType = meth.erasure(types);
239         long flags = impl.flags() & AccessFlags | SYNTHETIC | BRIDGE |
240                 (origin.isInterface() ? DEFAULT : 0);
241         MethodSymbol bridge = new MethodSymbol(flags,
242                                                meth.name,
243                                                bridgeType,
244                                                origin);
245         /* once JDK-6996415 is solved it should be checked if this approach can
246          * be applied to method addOverrideBridgesIfNeeded
247          */
248         bridge.params = createBridgeParams(impl, bridge, bridgeType);
249         bridge.setAttributes(impl);
250 
251         JCMethodDecl md = make.MethodDef(bridge, null);
252 
253         // The bridge calls this.impl(..), if we have an implementation
254         // in the current class, super.impl(...) otherwise.
255         JCExpression receiver = (impl.owner == origin)
256             ? make.This(origin.erasure(types))
257             : make.Super(types.supertype(origin.type).tsym.erasure(types), origin);
258 
259         // The type returned from the original method.
260         Type calltype = implTypeErasure.getReturnType();
261 
262         // Construct a call of  this.impl(params), or super.impl(params),
263         // casting params and possibly results as needed.
264         JCExpression call =
265             make.Apply(
266                        null,
267                        make.Select(receiver, impl).setType(calltype),
268                        translateArgs(make.Idents(md.params), implTypeErasure.getParameterTypes(), null))
269             .setType(calltype);
270         JCStatement stat = (implTypeErasure.getReturnType().hasTag(VOID))
271             ? make.Exec(call)
272             : make.Return(coerce(call, bridgeType.getReturnType()));
273         md.body = make.Block(0, List.of(stat));
274 
275         // Add bridge to `bridges' buffer
276         bridges.append(md);
277 
278         // Add bridge to scope of enclosing class and keep track of the bridge span.
279         origin.members().enter(bridge);
280     }
281 
282     private List<VarSymbol> createBridgeParams(MethodSymbol impl, MethodSymbol bridge,
283             Type bridgeType) {
284         List<VarSymbol> bridgeParams = null;
285         if (impl.params != null) {
286             bridgeParams = List.nil();
287             List<VarSymbol> implParams = impl.params;
288             Type.MethodType mType = (Type.MethodType)bridgeType;
289             List<Type> argTypes = mType.argtypes;
290             while (implParams.nonEmpty() && argTypes.nonEmpty()) {
291                 VarSymbol param = new VarSymbol(implParams.head.flags() | SYNTHETIC | PARAMETER,
292                         implParams.head.name, argTypes.head, bridge);
293                 param.setAttributes(implParams.head);
294                 bridgeParams = bridgeParams.append(param);
295                 implParams = implParams.tail;
296                 argTypes = argTypes.tail;
297             }
298         }
299         return bridgeParams;
300     }
301 
302     /** Add bridge if given symbol is a non-private, non-static member
303      *  of the given class, which is either defined in the class or non-final
304      *  inherited, and one of the two following conditions holds:
305      *  1. The method's type changes in the given class, as compared to the
306      *     class where the symbol was defined, (in this case
307      *     we have extended a parameterized class with non-trivial parameters).
308      *  2. The method has an implementation with a different erased return type.
309      *     (in this case we have used co-variant returns).
310      *  If a bridge already exists in some other class, no new bridge is added.
311      *  Instead, it is checked that the bridge symbol overrides the method symbol.
312      *  (Spec ???).
313      *  todo: what about bridges for privates???
314      *
315      *  @param pos     The source code position to be used for the definition.
316      *  @param sym     The symbol for which a bridge might have to be added.
317      *  @param origin  The class in which the bridge would go.
318      *  @param bridges The list buffer to which the bridge would be added.
319      */
320     void addBridgeIfNeeded(DiagnosticPosition pos,
321                            Symbol sym,
322                            ClassSymbol origin,
323                            ListBuffer<JCTree> bridges) {
324         if (sym.kind == MTH &&
325                 sym.name != names.init &&
326                 (sym.flags() & (PRIVATE | STATIC)) == 0 &&
327                 (sym.flags() & SYNTHETIC) != SYNTHETIC &&
328                 sym.isMemberOf(origin, types)) {
329             MethodSymbol meth = (MethodSymbol)sym;
330             MethodSymbol bridge = meth.binaryImplementation(origin, types);
331             MethodSymbol impl = meth.implementation(origin, types, true);
332             if (bridge == null ||
333                 bridge == meth ||
334                 (impl != null && !bridge.owner.isSubClass(impl.owner, types))) {
335                 // No bridge was added yet.
336                 if (impl != null && bridge != impl && isBridgeNeeded(meth, impl, origin.type)) {
337                     addBridge(pos, meth, impl, origin, bridges);
338                 } else if (impl == meth
339                            && impl.owner != origin
340                            && (impl.flags() & FINAL) == 0
341                            && (meth.flags() & (ABSTRACT|PUBLIC)) == PUBLIC
342                            && (origin.flags() & PUBLIC) > (impl.owner.flags() & PUBLIC)) {
343                     // this is to work around a horrible but permanent
344                     // reflection design error.
345                     addBridge(pos, meth, impl, origin, bridges);
346                 }
347             }
348         }
349     }
350     // where
351 
352         /**
353          * @param method The symbol for which a bridge might have to be added
354          * @param impl The implementation of method
355          * @param dest The type in which the bridge would go
356          */
357         private boolean isBridgeNeeded(MethodSymbol method,
358                                        MethodSymbol impl,
359                                        Type dest) {
360             if (impl != method) {
361                 // If either method or impl have different erasures as
362                 // members of dest, a bridge is needed.
363                 Type method_erasure = method.erasure(types);
364                 if (!isSameMemberWhenErased(dest, method, method_erasure))
365                     return true;
366                 Type impl_erasure = impl.erasure(types);
367                 if (!isSameMemberWhenErased(dest, impl, impl_erasure))
368                     return true;
369 
370                 /* Bottom line: A bridge is needed if the erasure of the implementation
371                    is different from that of the method that it overrides.
372                 */
373                 return !types.isSameType(impl_erasure, method_erasure);
374             } else {
375                // method and impl are the same...
376                 if ((method.flags() & ABSTRACT) != 0) {
377                     // ...and abstract so a bridge is not needed.
378                     // Concrete subclasses will bridge as needed.
379                     return false;
380                 }
381 
382                 // The erasure of the return type is always the same
383                 // for the same symbol.  Reducing the three tests in
384                 // the other branch to just one:
385                 return !isSameMemberWhenErased(dest, method, method.erasure(types));
386             }
387         }
388         /**
389          * Lookup the method as a member of the type.  Compare the
390          * erasures.
391          * @param type the class where to look for the method
392          * @param method the method to look for in class
393          * @param erasure the erasure of method
394          */
395         private boolean isSameMemberWhenErased(Type type,
396                                                MethodSymbol method,
397                                                Type erasure) {
398             return types.isSameType(erasure(types.memberType(type, method)),
399                                     erasure);
400         }
401 
402     void addBridges(DiagnosticPosition pos,
403                     TypeSymbol i,
404                     ClassSymbol origin,
405                     ListBuffer<JCTree> bridges) {
406         for (Symbol sym : i.members().getSymbols(NON_RECURSIVE))
407             addBridgeIfNeeded(pos, sym, origin, bridges);
408         for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail)
409             addBridges(pos, l.head.tsym, origin, bridges);
410     }
411 
412     /** Add all necessary bridges to some class appending them to list buffer.
413      *  @param pos     The source code position to be used for the bridges.
414      *  @param origin  The class in which the bridges go.
415      *  @param bridges The list buffer to which the bridges are added.
416      */
417     void addBridges(DiagnosticPosition pos, ClassSymbol origin, ListBuffer<JCTree> bridges) {
418         Type st = types.supertype(origin.type);
419         while (st.hasTag(CLASS)) {
420 //          if (isSpecialization(st))
421             addBridges(pos, st.tsym, origin, bridges);
422             st = types.supertype(st);
423         }
424         for (List<Type> l = types.interfaces(origin.type); l.nonEmpty(); l = l.tail)
425 //          if (isSpecialization(l.head))
426             addBridges(pos, l.head.tsym, origin, bridges);
427     }
428 
429 /* ************************************************************************
430  * Visitor methods
431  *************************************************************************/
432 
433     /** Visitor argument: proto-type.
434      */
435     private Type pt;
436 
437     /** Visitor method: perform a type translation on tree.
438      */
439     public <T extends JCTree> T translate(T tree, Type pt) {
440         Type prevPt = this.pt;
441         try {
442             this.pt = pt;
443             return translate(tree);
444         } finally {
445             this.pt = prevPt;
446         }
447     }
448 
449     /** Visitor method: perform a type translation on list of trees.
450      */
451     public <T extends JCTree> List<T> translate(List<T> trees, Type pt) {
452         Type prevPt = this.pt;
453         List<T> res;
454         try {
455             this.pt = pt;
456             res = translate(trees);
457         } finally {
458             this.pt = prevPt;
459         }
460         return res;
461     }
462 
463     public void visitClassDef(JCClassDecl tree) {
464         translateClass(tree.sym);
465         result = tree;
466     }
467 
468     Type returnType = null;
469     public void visitMethodDef(JCMethodDecl tree) {
470         Type prevRetType = returnType;
471         try {
472             returnType = erasure(tree.type).getReturnType();
473             tree.restype = translate(tree.restype, null);
474             tree.typarams = List.nil();
475             tree.params = translateVarDefs(tree.params);
476             tree.recvparam = translate(tree.recvparam, null);
477             tree.thrown = translate(tree.thrown, null);
478             tree.body = translate(tree.body, tree.sym.erasure(types).getReturnType());
479             tree.type = erasure(tree.type);
480             result = tree;
481         } finally {
482             returnType = prevRetType;
483         }
484     }
485 
486     public void visitVarDef(JCVariableDecl tree) {
487         tree.vartype = translate(tree.vartype, null);
488         tree.init = translate(tree.init, tree.sym.erasure(types));
489         tree.type = erasure(tree.type);
490         result = tree;
491     }
492 
493     public void visitDoLoop(JCDoWhileLoop tree) {
494         tree.body = translate(tree.body);
495         tree.cond = translate(tree.cond, syms.booleanType);
496         result = tree;
497     }
498 
499     public void visitWhileLoop(JCWhileLoop tree) {
500         tree.cond = translate(tree.cond, syms.booleanType);
501         tree.body = translate(tree.body);
502         result = tree;
503     }
504 
505     public void visitForLoop(JCForLoop tree) {
506         tree.init = translate(tree.init, null);
507         if (tree.cond != null)
508             tree.cond = translate(tree.cond, syms.booleanType);
509         tree.step = translate(tree.step, null);
510         tree.body = translate(tree.body);
511         result = tree;
512     }
513 
514     public void visitForeachLoop(JCEnhancedForLoop tree) {
515         tree.var = translate(tree.var, null);
516         Type iterableType = tree.expr.type;
517         tree.expr = translate(tree.expr, erasure(tree.expr.type));
518         if (types.elemtype(tree.expr.type) == null)
519             tree.expr.type = iterableType; // preserve type for Lower
520         tree.body = translate(tree.body);
521         result = tree;
522     }
523 
524     public void visitLambda(JCLambda tree) {
525         Type prevRetType = returnType;
526         try {
527             returnType = erasure(tree.getDescriptorType(types)).getReturnType();
528             tree.params = translate(tree.params);
529             tree.body = translate(tree.body, tree.body.type == null || returnType.hasTag(VOID) ? null : returnType);
530             if (!tree.type.isIntersection()) {
531                 tree.type = erasure(tree.type);
532             } else {
533                 tree.type = types.erasure(types.findDescriptorSymbol(tree.type.tsym).owner.type);
534             }
535             result = tree;
536         }
537         finally {
538             returnType = prevRetType;
539         }
540     }
541 
542     public void visitSwitch(JCSwitch tree) {
543         Type selsuper = types.supertype(tree.selector.type);
544         boolean enumSwitch = selsuper != null &&
545             selsuper.tsym == syms.enumSym;
546         Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
547         tree.selector = translate(tree.selector, target);
548         tree.cases = translateCases(tree.cases);
549         result = tree;
550     }
551 
552     public void visitCase(JCCase tree) {
553         tree.labels = translate(tree.labels, null);
554         tree.guard = translate(tree.guard, syms.booleanType);
555         tree.stats = translate(tree.stats);
556         result = tree;
557     }
558 
559     @Override
560     public void visitAnyPattern(JCAnyPattern tree) {
561         result = tree;
562     }
563 
564     public void visitBindingPattern(JCBindingPattern tree) {
565         tree.var = translate(tree.var, null);
566         result = tree;
567     }
568 
569     @Override
570     public void visitConstantCaseLabel(JCConstantCaseLabel tree) {
571         tree.expr = translate(tree.expr, null);
572         result = tree;
573     }
574 
575     @Override
576     public void visitPatternCaseLabel(JCPatternCaseLabel tree) {
577         tree.pat = translate(tree.pat, null);
578         result = tree;
579     }
580 
581     public void visitSwitchExpression(JCSwitchExpression tree) {
582         Type selsuper = types.supertype(tree.selector.type);
583         boolean enumSwitch = selsuper != null &&
584             selsuper.tsym == syms.enumSym;
585         Type target = enumSwitch ? erasure(tree.selector.type) : syms.intType;
586         tree.selector = translate(tree.selector, target);
587         tree.cases = translate(tree.cases, tree.type);
588         tree.type = erasure(tree.type);
589         result = retype(tree, tree.type, pt);
590     }
591 
592     public void visitRecordPattern(JCRecordPattern tree) {
593         tree.fullComponentTypes = tree.record.getRecordComponents()
594                                              .map(rc -> types.memberType(tree.type, rc));
595         tree.deconstructor = translate(tree.deconstructor, null);
596         tree.nested = translate(tree.nested, null);
597         result = tree;
598     }
599 
600     public void visitSynchronized(JCSynchronized tree) {
601         tree.lock = translate(tree.lock, erasure(tree.lock.type));
602         tree.body = translate(tree.body);
603         result = tree;
604     }
605 
606     public void visitTry(JCTry tree) {
607         tree.resources = translate(tree.resources, syms.autoCloseableType);
608         tree.body = translate(tree.body);
609         tree.catchers = translateCatchers(tree.catchers);
610         tree.finalizer = translate(tree.finalizer);
611         result = tree;
612     }
613 
614     public void visitConditional(JCConditional tree) {
615         tree.cond = translate(tree.cond, syms.booleanType);
616         tree.truepart = translate(tree.truepart, erasure(tree.type));
617         tree.falsepart = translate(tree.falsepart, erasure(tree.type));
618         tree.type = erasure(tree.type);
619         result = retype(tree, tree.type, pt);
620     }
621 
622    public void visitIf(JCIf tree) {
623         tree.cond = translate(tree.cond, syms.booleanType);
624         tree.thenpart = translate(tree.thenpart);
625         tree.elsepart = translate(tree.elsepart);
626         result = tree;
627     }
628 
629     public void visitExec(JCExpressionStatement tree) {
630         tree.expr = translate(tree.expr, null);
631         result = tree;
632     }
633 
634     public void visitReturn(JCReturn tree) {
635         if (!returnType.hasTag(VOID))
636             tree.expr = translate(tree.expr, returnType);
637         result = tree;
638     }
639 
640     @Override
641     public void visitBreak(JCBreak tree) {
642         result = tree;
643     }
644 
645     @Override
646     public void visitYield(JCYield tree) {
647         tree.value = translate(tree.value, erasure(tree.value.type));
648         tree.value.type = erasure(tree.value.type);
649         tree.value = retype(tree.value, tree.value.type, pt);
650         result = tree;
651     }
652 
653     public void visitThrow(JCThrow tree) {
654         tree.expr = translate(tree.expr, erasure(tree.expr.type));
655         result = tree;
656     }
657 
658     public void visitAssert(JCAssert tree) {
659         tree.cond = translate(tree.cond, syms.booleanType);
660         if (tree.detail != null)
661             tree.detail = translate(tree.detail, erasure(tree.detail.type));
662         result = tree;
663     }
664 
665     public void visitApply(JCMethodInvocation tree) {
666         tree.meth = translate(tree.meth, null);
667         Symbol meth = TreeInfo.symbol(tree.meth);
668         Type mt = meth.erasure(types);
669         boolean useInstantiatedPtArgs = !types.isSignaturePolymorphic((MethodSymbol)meth.baseSymbol());
670         List<Type> argtypes = useInstantiatedPtArgs ?
671                 tree.meth.type.getParameterTypes() :
672                 mt.getParameterTypes();
673         if (meth.name == names.init && meth.owner == syms.enumSym)
674             argtypes = argtypes.tail.tail;
675         if (tree.varargsElement != null)
676             tree.varargsElement = types.erasure(tree.varargsElement);
677         else
678             if (tree.args.length() != argtypes.length()) {
679                 Assert.error(String.format("Incorrect number of arguments; expected %d, found %d",
680                         tree.args.length(), argtypes.length()));
681             }
682         tree.args = translateArgs(tree.args, argtypes, tree.varargsElement);
683 
684         tree.type = types.erasure(tree.type);
685         // Insert casts of method invocation results as needed.
686         result = retype(tree, mt.getReturnType(), pt);
687     }
688 
689     public void visitNewClass(JCNewClass tree) {
690         if (tree.encl != null) {
691             if (tree.def == null) {
692                 tree.encl = translate(tree.encl, erasure(tree.encl.type));
693             } else {
694                 tree.args = tree.args.prepend(attr.makeNullCheck(tree.encl));
695                 tree.encl = null;
696             }
697         }
698 
699         Type erasedConstructorType = tree.constructorType != null ?
700                 erasure(tree.constructorType) :
701                 null;
702 
703         List<Type> argtypes = erasedConstructorType != null ?
704                 erasedConstructorType.getParameterTypes() :
705                 tree.constructor.erasure(types).getParameterTypes();
706 
707         tree.clazz = translate(tree.clazz, null);
708         if (tree.varargsElement != null)
709             tree.varargsElement = types.erasure(tree.varargsElement);
710         tree.args = translateArgs(
711             tree.args, argtypes, tree.varargsElement);
712         tree.def = translate(tree.def, null);
713         if (erasedConstructorType != null)
714             tree.constructorType = erasedConstructorType;
715         tree.type = erasure(tree.type);
716         result = tree;
717     }
718 
719     public void visitNewArray(JCNewArray tree) {
720         tree.elemtype = translate(tree.elemtype, null);
721         translate(tree.dims, syms.intType);
722         if (tree.type != null) {
723             tree.elems = translate(tree.elems, erasure(types.elemtype(tree.type)));
724             tree.type = erasure(tree.type);
725         } else {
726             tree.elems = translate(tree.elems, null);
727         }
728 
729         result = tree;
730     }
731 
732     public void visitParens(JCParens tree) {
733         tree.expr = translate(tree.expr, pt);
734         tree.type = erasure(tree.expr.type);
735         result = tree;
736     }
737 
738     public void visitAssign(JCAssign tree) {
739         tree.lhs = translate(tree.lhs, null);
740         tree.rhs = translate(tree.rhs, erasure(tree.lhs.type));
741         tree.type = erasure(tree.lhs.type);
742         result = retype(tree, tree.type, pt);
743     }
744 
745     public void visitAssignop(JCAssignOp tree) {
746         tree.lhs = translate(tree.lhs, null);
747         tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
748         tree.type = erasure(tree.type);
749         result = tree;
750     }
751 
752     public void visitUnary(JCUnary tree) {
753         tree.arg = translate(tree.arg, (tree.getTag() == Tag.NULLCHK)
754             ? tree.type
755             : tree.operator.type.getParameterTypes().head);
756         result = tree;
757     }
758 
759     public void visitBinary(JCBinary tree) {
760         tree.lhs = translate(tree.lhs, tree.operator.type.getParameterTypes().head);
761         tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
762         result = tree;
763     }
764 
765     public void visitAnnotatedType(JCAnnotatedType tree) {
766         // For now, we need to keep the annotations in the tree because of the current
767         // MultiCatch implementation wrt type annotations
768         List<TypeCompound> mirrors = annotate.fromAnnotations(tree.annotations);
769         tree.underlyingType = translate(tree.underlyingType);
770         tree.type = tree.underlyingType.type.annotatedType(mirrors);
771         result = tree;
772     }
773 
774     public void visitTypeCast(JCTypeCast tree) {
775         tree.clazz = translate(tree.clazz, null);
776         Type originalTarget = tree.type;
777         tree.type = erasure(tree.type);
778         JCExpression newExpression = translate(tree.expr, tree.type);
779         if (newExpression != tree.expr) {
780             JCTypeCast typeCast = newExpression.hasTag(Tag.TYPECAST)
781                 ? (JCTypeCast) newExpression
782                 : null;
783             tree.expr = typeCast != null && types.isSameType(typeCast.type, tree.type)
784                 ? typeCast.expr
785                 : newExpression;
786         }
787         if (originalTarget.isIntersection()) {
788             Type.IntersectionClassType ict = (Type.IntersectionClassType)originalTarget;
789             for (Type c : ict.getExplicitComponents()) {
790                 Type ec = erasure(c);
791                 if (!types.isSameType(ec, tree.type) && (!types.isSameType(ec, pt))) {
792                     tree.expr = coerce(tree.expr, ec);
793                 }
794             }
795         }
796         result = retype(tree, tree.type, pt);
797     }
798 
799     public void visitTypeTest(JCInstanceOf tree) {
800         tree.expr = translate(tree.expr, null);
801         tree.pattern = translate(tree.pattern, null);
802         result = tree;
803     }
804 
805     public void visitIndexed(JCArrayAccess tree) {
806         tree.indexed = translate(tree.indexed, erasure(tree.indexed.type));
807         tree.index = translate(tree.index, syms.intType);
808 
809         // Insert casts of indexed expressions as needed.
810         result = retype(tree, types.elemtype(tree.indexed.type), pt);
811     }
812 
813     // There ought to be nothing to rewrite here;
814     // we don't generate code.
815     public void visitAnnotation(JCAnnotation tree) {
816         result = tree;
817     }
818 
819     public void visitIdent(JCIdent tree) {
820         Type et = tree.sym.erasure(types);
821 
822         // Map type variables to their bounds.
823         if (tree.sym.kind == TYP && tree.sym.type.hasTag(TYPEVAR)) {
824             result = make.at(tree.pos).Type(et);
825         } else
826         // Map constants expressions to themselves.
827         if (tree.type.constValue() != null) {
828             result = tree;
829         }
830         // Insert casts of variable uses as needed.
831         else if (tree.sym.kind == VAR) {
832             result = retype(tree, et, pt);
833         }
834         else {
835             tree.type = erasure(tree.type);
836             result = tree;
837         }
838     }
839 
840     public void visitStringTemplate(JCStringTemplate tree) {
841         tree.processor = translate(tree.processor, erasure(tree.processor.type));
842         tree.expressions = tree.expressions.stream()
843                 .map(e -> translate(e, erasure(e.type))).collect(List.collector());
844         tree.type = erasure(tree.type);
845         result = tree;
846     }
847 
848     public void visitSelect(JCFieldAccess tree) {
849         Type t = types.skipTypeVars(tree.selected.type, false);
850         if (t.isCompound()) {
851             tree.selected = coerce(
852                 translate(tree.selected, erasure(tree.selected.type)),
853                 erasure(tree.sym.owner.type));
854         } else
855             tree.selected = translate(tree.selected, erasure(t));
856 
857         // Map constants expressions to themselves.
858         if (tree.type.constValue() != null) {
859             result = tree;
860         }
861         // Insert casts of variable uses as needed.
862         else if (tree.sym.kind == VAR) {
863             result = retype(tree, tree.sym.erasure(types), pt);
864         }
865         else {
866             tree.type = erasure(tree.type);
867             result = tree;
868         }
869     }
870 
871     public void visitReference(JCMemberReference tree) {
872         Type t = types.skipTypeVars(tree.expr.type, false);
873         Type receiverTarget = t.isCompound() ? erasure(tree.sym.owner.type) : erasure(t);
874         if (tree.kind == ReferenceKind.UNBOUND) {
875             tree.expr = make.Type(receiverTarget);
876         } else {
877             tree.expr = translate(tree.expr, receiverTarget);
878         }
879         if (!tree.type.isIntersection()) {
880             tree.type = erasure(tree.type);
881         } else {
882             tree.type = types.erasure(types.findDescriptorSymbol(tree.type.tsym).owner.type);
883         }
884         if (tree.varargsElement != null)
885             tree.varargsElement = erasure(tree.varargsElement);
886         result = tree;
887     }
888 
889     public void visitTypeArray(JCArrayTypeTree tree) {
890         tree.elemtype = translate(tree.elemtype, null);
891         tree.type = erasure(tree.type);
892         result = tree;
893     }
894 
895     /** Visitor method for parameterized types.
896      */
897     public void visitTypeApply(JCTypeApply tree) {
898         JCTree clazz = translate(tree.clazz, null);
899         result = clazz;
900     }
901 
902     public void visitTypeIntersection(JCTypeIntersection tree) {
903         tree.bounds = translate(tree.bounds, null);
904         tree.type = erasure(tree.type);
905         result = tree;
906     }
907 
908 /**************************************************************************
909  * utility methods
910  *************************************************************************/
911 
912     private Type erasure(Type t) {
913         return types.erasure(t);
914     }
915 
916 /**************************************************************************
917  * main method
918  *************************************************************************/
919 
920     private Env<AttrContext> env;
921 
922     private static final String statePreviousToFlowAssertMsg =
923             "The current compile state [%s] of class %s is previous to FLOW";
924 
925     void translateClass(ClassSymbol c) {
926         Type st = types.supertype(c.type);
927         // process superclass before derived
928         if (st.hasTag(CLASS)) {
929             translateClass((ClassSymbol)st.tsym);
930         }
931 
932         Env<AttrContext> myEnv = enter.getEnv(c);
933         if (myEnv == null || (c.flags_field & TYPE_TRANSLATED) != 0) {
934             return;
935         }
936         c.flags_field |= TYPE_TRANSLATED;
937 
938         /*  The two assertions below are set for early detection of any attempt
939          *  to translate a class that:
940          *
941          *  1) has no compile state being it the most outer class.
942          *     We accept this condition for inner classes.
943          *
944          *  2) has a compile state which is previous to Flow state.
945          */
946         boolean envHasCompState = compileStates.get(myEnv) != null;
947         if (!envHasCompState && c.outermostClass() == c) {
948             Assert.error("No info for outermost class: " + myEnv.enclClass.sym);
949         }
950 
951         if (envHasCompState &&
952                 CompileState.FLOW.isAfter(compileStates.get(myEnv))) {
953             Assert.error(String.format(statePreviousToFlowAssertMsg,
954                     compileStates.get(myEnv), myEnv.enclClass.sym));
955         }
956 
957         Env<AttrContext> oldEnv = env;
958         try {
959             env = myEnv;
960             // class has not been translated yet
961 
962             TreeMaker savedMake = make;
963             Type savedPt = pt;
964             make = make.forToplevel(env.toplevel);
965             pt = null;
966             try {
967                 JCClassDecl tree = (JCClassDecl) env.tree;
968                 tree.typarams = List.nil();
969                 super.visitClassDef(tree);
970                 make.at(tree.pos);
971                 ListBuffer<JCTree> bridges = new ListBuffer<>();
972                 addBridges(tree.pos(), c, bridges);
973                 tree.defs = bridges.toList().prependList(tree.defs);
974                 tree.type = erasure(tree.type);
975             } finally {
976                 make = savedMake;
977                 pt = savedPt;
978             }
979         } finally {
980             env = oldEnv;
981         }
982     }
983 
984     /** Translate a toplevel class definition.
985      *  @param cdef    The definition to be translated.
986      */
987     public JCTree translateTopLevelClass(JCTree cdef, TreeMaker make) {
988         // note that this method does NOT support recursion.
989         this.make = make;
990         pt = null;
991         return translate(cdef, null);
992     }
993 }