langtools/src/share/classes/com/sun/tools/javac/jvm/Gen.java
changeset 14721 071e3587f212
parent 14359 d4099818ab70
child 14949 45f43822bbde
equal deleted inserted replaced
14720:c24c61d0d9a6 14721:071e3587f212
    69     private final Target target;
    69     private final Target target;
    70     private final Type stringBufferType;
    70     private final Type stringBufferType;
    71     private final Map<Type,Symbol> stringBufferAppend;
    71     private final Map<Type,Symbol> stringBufferAppend;
    72     private Name accessDollar;
    72     private Name accessDollar;
    73     private final Types types;
    73     private final Types types;
       
    74     private final Lower lower;
    74 
    75 
    75     /** Switch: GJ mode?
    76     /** Switch: GJ mode?
    76      */
    77      */
    77     private final boolean allowGenerics;
    78     private final boolean allowGenerics;
    78 
    79 
   110             ? syms.stringBuilderType
   111             ? syms.stringBuilderType
   111             : syms.stringBufferType;
   112             : syms.stringBufferType;
   112         stringBufferAppend = new HashMap<Type,Symbol>();
   113         stringBufferAppend = new HashMap<Type,Symbol>();
   113         accessDollar = names.
   114         accessDollar = names.
   114             fromString("access" + target.syntheticNameChar());
   115             fromString("access" + target.syntheticNameChar());
       
   116         lower = Lower.instance(context);
   115 
   117 
   116         Options options = Options.instance(context);
   118         Options options = Options.instance(context);
   117         lineDebugInfo =
   119         lineDebugInfo =
   118             options.isUnset(G_CUSTOM) ||
   120             options.isUnset(G_CUSTOM) ||
   119             options.isSet(G_CUSTOM, "lines");
   121             options.isSet(G_CUSTOM, "lines");
   814             if (markBranches) result.tree = _tree;
   816             if (markBranches) result.tree = _tree;
   815             return result;
   817             return result;
   816         }
   818         }
   817     }
   819     }
   818 
   820 
       
   821     /** Visitor class for expressions which might be constant expressions.
       
   822      *  This class is a subset of TreeScanner. Intended to visit trees pruned by
       
   823      *  Lower as long as constant expressions looking for references to any
       
   824      *  ClassSymbol. Any such reference will be added to the constant pool so
       
   825      *  automated tools can detect class dependencies better.
       
   826      */
       
   827     class ClassReferenceVisitor extends JCTree.Visitor {
       
   828 
       
   829         @Override
       
   830         public void visitTree(JCTree tree) {}
       
   831 
       
   832         @Override
       
   833         public void visitBinary(JCBinary tree) {
       
   834             tree.lhs.accept(this);
       
   835             tree.rhs.accept(this);
       
   836         }
       
   837 
       
   838         @Override
       
   839         public void visitSelect(JCFieldAccess tree) {
       
   840             if (tree.selected.type.hasTag(CLASS)) {
       
   841                 makeRef(tree.selected.pos(), tree.selected.type);
       
   842             }
       
   843         }
       
   844 
       
   845         @Override
       
   846         public void visitIdent(JCIdent tree) {
       
   847             if (tree.sym.owner instanceof ClassSymbol) {
       
   848                 pool.put(tree.sym.owner);
       
   849             }
       
   850         }
       
   851 
       
   852         @Override
       
   853         public void visitConditional(JCConditional tree) {
       
   854             tree.cond.accept(this);
       
   855             tree.truepart.accept(this);
       
   856             tree.falsepart.accept(this);
       
   857         }
       
   858 
       
   859         @Override
       
   860         public void visitUnary(JCUnary tree) {
       
   861             tree.arg.accept(this);
       
   862         }
       
   863 
       
   864         @Override
       
   865         public void visitParens(JCParens tree) {
       
   866             tree.expr.accept(this);
       
   867         }
       
   868 
       
   869         @Override
       
   870         public void visitTypeCast(JCTypeCast tree) {
       
   871             tree.expr.accept(this);
       
   872         }
       
   873     }
       
   874 
       
   875     private ClassReferenceVisitor classReferenceVisitor = new ClassReferenceVisitor();
       
   876 
   819     /** Visitor method: generate code for an expression, catching and reporting
   877     /** Visitor method: generate code for an expression, catching and reporting
   820      *  any completion failures.
   878      *  any completion failures.
   821      *  @param tree    The expression to be visited.
   879      *  @param tree    The expression to be visited.
   822      *  @param pt      The expression's expected type (proto-type).
   880      *  @param pt      The expression's expected type (proto-type).
   823      */
   881      */
   824     public Item genExpr(JCTree tree, Type pt) {
   882     public Item genExpr(JCTree tree, Type pt) {
   825         Type prevPt = this.pt;
   883         Type prevPt = this.pt;
   826         try {
   884         try {
   827             if (tree.type.constValue() != null) {
   885             if (tree.type.constValue() != null) {
   828                 // Short circuit any expressions which are constants
   886                 // Short circuit any expressions which are constants
       
   887                 tree.accept(classReferenceVisitor);
   829                 checkStringConstant(tree.pos(), tree.type.constValue());
   888                 checkStringConstant(tree.pos(), tree.type.constValue());
   830                 result = items.makeImmediateItem(tree.type, tree.type.constValue());
   889                 result = items.makeImmediateItem(tree.type, tree.type.constValue());
   831             } else {
   890             } else {
   832                 this.pt = pt;
   891                 this.pt = pt;
   833                 tree.accept(this);
   892                 tree.accept(this);
  2203         genStats(tree.defs, env);
  2262         genStats(tree.defs, env);
  2204         result = genExpr(tree.expr, tree.expr.type).load();
  2263         result = genExpr(tree.expr, tree.expr.type).load();
  2205         code.endScopes(limit);
  2264         code.endScopes(limit);
  2206     }
  2265     }
  2207 
  2266 
       
  2267     private void generateReferencesToPrunedTree(ClassSymbol classSymbol, Pool pool) {
       
  2268         List<JCTree> prunedInfo = lower.prunedTree.get(classSymbol);
       
  2269         if (prunedInfo != null) {
       
  2270             for (JCTree prunedTree: prunedInfo) {
       
  2271                 prunedTree.accept(classReferenceVisitor);
       
  2272             }
       
  2273         }
       
  2274     }
       
  2275 
  2208 /* ************************************************************************
  2276 /* ************************************************************************
  2209  * main method
  2277  * main method
  2210  *************************************************************************/
  2278  *************************************************************************/
  2211 
  2279 
  2212     /** Generate code for a class definition.
  2280     /** Generate code for a class definition.
  2230                 )
  2298                 )
  2231                 implementInterfaceMethods(c);
  2299                 implementInterfaceMethods(c);
  2232             cdef.defs = normalizeDefs(cdef.defs, c);
  2300             cdef.defs = normalizeDefs(cdef.defs, c);
  2233             c.pool = pool;
  2301             c.pool = pool;
  2234             pool.reset();
  2302             pool.reset();
       
  2303             generateReferencesToPrunedTree(c, pool);
  2235             Env<GenContext> localEnv =
  2304             Env<GenContext> localEnv =
  2236                 new Env<GenContext>(cdef, new GenContext());
  2305                 new Env<GenContext>(cdef, new GenContext());
  2237             localEnv.toplevel = env.toplevel;
  2306             localEnv.toplevel = env.toplevel;
  2238             localEnv.enclClass = cdef;
  2307             localEnv.enclClass = cdef;
  2239             for (List<JCTree> l = cdef.defs; l.nonEmpty(); l = l.tail) {
  2308             for (List<JCTree> l = cdef.defs; l.nonEmpty(); l = l.tail) {