langtools/src/share/classes/com/sun/tools/javac/comp/Lower.java
changeset 8032 e1aa25ccdabb
parent 8031 d5fe2c1cecfc
child 8242 3873b4aaf4a8
equal deleted inserted replaced
8031:d5fe2c1cecfc 8032:e1aa25ccdabb
  1055                 make.at(tree.pos);
  1055                 make.at(tree.pos);
  1056                 return makeLit(sym.type, cv);
  1056                 return makeLit(sym.type, cv);
  1057             }
  1057             }
  1058             // Otherwise replace the variable by its proxy.
  1058             // Otherwise replace the variable by its proxy.
  1059             sym = proxies.lookup(proxyName(sym.name)).sym;
  1059             sym = proxies.lookup(proxyName(sym.name)).sym;
  1060             assert sym != null && (sym.flags_field & FINAL) != 0;
  1060             Assert.check(sym != null && (sym.flags_field & FINAL) != 0);
  1061             tree = make.at(tree.pos).Ident(sym);
  1061             tree = make.at(tree.pos).Ident(sym);
  1062         }
  1062         }
  1063         JCExpression base = (tree.getTag() == JCTree.SELECT) ? ((JCFieldAccess) tree).selected : null;
  1063         JCExpression base = (tree.getTag() == JCTree.SELECT) ? ((JCFieldAccess) tree).selected : null;
  1064         switch (sym.kind) {
  1064         switch (sym.kind) {
  1065         case TYP:
  1065         case TYP:
  1206     /** Add all required access methods for a private symbol to enclosing class.
  1206     /** Add all required access methods for a private symbol to enclosing class.
  1207      *  @param sym       The symbol.
  1207      *  @param sym       The symbol.
  1208      */
  1208      */
  1209     void makeAccessible(Symbol sym) {
  1209     void makeAccessible(Symbol sym) {
  1210         JCClassDecl cdef = classDef(sym.owner.enclClass());
  1210         JCClassDecl cdef = classDef(sym.owner.enclClass());
  1211         assert cdef != null : "class def not found: " + sym + " in " + sym.owner;
  1211         if (cdef == null) Assert.error("class def not found: " + sym + " in " + sym.owner);
  1212         if (sym.name == names.init) {
  1212         if (sym.name == names.init) {
  1213             cdef.defs = cdef.defs.prepend(
  1213             cdef.defs = cdef.defs.prepend(
  1214                 accessConstructorDef(cdef.pos, sym, accessConstrs.get(sym)));
  1214                 accessConstructorDef(cdef.pos, sym, accessConstrs.get(sym)));
  1215         } else {
  1215         } else {
  1216             MethodSymbol[] accessors = accessSyms.get(sym);
  1216             MethodSymbol[] accessors = accessSyms.get(sym);
  1456         if (resource instanceof JCVariableDecl) {
  1456         if (resource instanceof JCVariableDecl) {
  1457             JCVariableDecl var = (JCVariableDecl) resource;
  1457             JCVariableDecl var = (JCVariableDecl) resource;
  1458             expr = make.Ident(var.sym).setType(resource.type);
  1458             expr = make.Ident(var.sym).setType(resource.type);
  1459             stats.add(var);
  1459             stats.add(var);
  1460         } else {
  1460         } else {
  1461             assert resource instanceof JCExpression;
  1461             Assert.check(resource instanceof JCExpression);
  1462             VarSymbol syntheticTwrVar =
  1462             VarSymbol syntheticTwrVar =
  1463             new VarSymbol(SYNTHETIC | FINAL,
  1463             new VarSymbol(SYNTHETIC | FINAL,
  1464                           makeSyntheticName(names.fromString("twrVar" +
  1464                           makeSyntheticName(names.fromString("twrVar" +
  1465                                            depth), twrVars),
  1465                                            depth), twrVars),
  1466                           (resource.type.tag == TypeTags.BOT) ?
  1466                           (resource.type.tag == TypeTags.BOT) ?
  1550      */
  1550      */
  1551     JCExpression makeOuterThis(DiagnosticPosition pos, TypeSymbol c) {
  1551     JCExpression makeOuterThis(DiagnosticPosition pos, TypeSymbol c) {
  1552         List<VarSymbol> ots = outerThisStack;
  1552         List<VarSymbol> ots = outerThisStack;
  1553         if (ots.isEmpty()) {
  1553         if (ots.isEmpty()) {
  1554             log.error(pos, "no.encl.instance.of.type.in.scope", c);
  1554             log.error(pos, "no.encl.instance.of.type.in.scope", c);
  1555             assert false;
  1555             Assert.error();
  1556             return makeNull();
  1556             return makeNull();
  1557         }
  1557         }
  1558         VarSymbol ot = ots.head;
  1558         VarSymbol ot = ots.head;
  1559         JCExpression tree = access(make.at(pos).Ident(ot));
  1559         JCExpression tree = access(make.at(pos).Ident(ot));
  1560         TypeSymbol otc = ot.type.tsym;
  1560         TypeSymbol otc = ot.type.tsym;
  1563                 ots = ots.tail;
  1563                 ots = ots.tail;
  1564                 if (ots.isEmpty()) {
  1564                 if (ots.isEmpty()) {
  1565                     log.error(pos,
  1565                     log.error(pos,
  1566                               "no.encl.instance.of.type.in.scope",
  1566                               "no.encl.instance.of.type.in.scope",
  1567                               c);
  1567                               c);
  1568                     assert false; // should have been caught in Attr
  1568                     Assert.error(); // should have been caught in Attr
  1569                     return tree;
  1569                     return tree;
  1570                 }
  1570                 }
  1571                 ot = ots.head;
  1571                 ot = ots.head;
  1572             } while (ot.owner != otc);
  1572             } while (ot.owner != otc);
  1573             if (otc.owner.kind != PCK && !otc.hasOuterInstance()) {
  1573             if (otc.owner.kind != PCK && !otc.hasOuterInstance()) {
  1574                 chk.earlyRefError(pos, c);
  1574                 chk.earlyRefError(pos, c);
  1575                 assert false; // should have been caught in Attr
  1575                 Assert.error(); // should have been caught in Attr
  1576                 return makeNull();
  1576                 return makeNull();
  1577             }
  1577             }
  1578             tree = access(make.at(pos).Select(tree, ot));
  1578             tree = access(make.at(pos).Select(tree, ot));
  1579             otc = ot.type.tsym;
  1579             otc = ot.type.tsym;
  1580         }
  1580         }
  1608     JCExpression makeOwnerThisN(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) {
  1608     JCExpression makeOwnerThisN(DiagnosticPosition pos, Symbol sym, boolean preciseMatch) {
  1609         Symbol c = sym.owner;
  1609         Symbol c = sym.owner;
  1610         List<VarSymbol> ots = outerThisStack;
  1610         List<VarSymbol> ots = outerThisStack;
  1611         if (ots.isEmpty()) {
  1611         if (ots.isEmpty()) {
  1612             log.error(pos, "no.encl.instance.of.type.in.scope", c);
  1612             log.error(pos, "no.encl.instance.of.type.in.scope", c);
  1613             assert false;
  1613             Assert.error();
  1614             return makeNull();
  1614             return makeNull();
  1615         }
  1615         }
  1616         VarSymbol ot = ots.head;
  1616         VarSymbol ot = ots.head;
  1617         JCExpression tree = access(make.at(pos).Ident(ot));
  1617         JCExpression tree = access(make.at(pos).Ident(ot));
  1618         TypeSymbol otc = ot.type.tsym;
  1618         TypeSymbol otc = ot.type.tsym;
  1621                 ots = ots.tail;
  1621                 ots = ots.tail;
  1622                 if (ots.isEmpty()) {
  1622                 if (ots.isEmpty()) {
  1623                     log.error(pos,
  1623                     log.error(pos,
  1624                         "no.encl.instance.of.type.in.scope",
  1624                         "no.encl.instance.of.type.in.scope",
  1625                         c);
  1625                         c);
  1626                     assert false;
  1626                     Assert.error();
  1627                     return tree;
  1627                     return tree;
  1628                 }
  1628                 }
  1629                 ot = ots.head;
  1629                 ot = ots.head;
  1630             } while (ot.owner != otc);
  1630             } while (ot.owner != otc);
  1631             tree = access(make.at(pos).Select(tree, ot));
  1631             tree = access(make.at(pos).Select(tree, ot));
  1638      *  name is the name of a free variable.
  1638      *  name is the name of a free variable.
  1639      */
  1639      */
  1640     JCStatement initField(int pos, Name name) {
  1640     JCStatement initField(int pos, Name name) {
  1641         Scope.Entry e = proxies.lookup(name);
  1641         Scope.Entry e = proxies.lookup(name);
  1642         Symbol rhs = e.sym;
  1642         Symbol rhs = e.sym;
  1643         assert rhs.owner.kind == MTH;
  1643         Assert.check(rhs.owner.kind == MTH);
  1644         Symbol lhs = e.next().sym;
  1644         Symbol lhs = e.next().sym;
  1645         assert rhs.owner.owner == lhs.owner;
  1645         Assert.check(rhs.owner.owner == lhs.owner);
  1646         make.at(pos);
  1646         make.at(pos);
  1647         return
  1647         return
  1648             make.Exec(
  1648             make.Exec(
  1649                 make.Assign(
  1649                 make.Assign(
  1650                     make.Select(make.This(lhs.owner.erasure(types)), lhs),
  1650                     make.Select(make.This(lhs.owner.erasure(types)), lhs),
  1653 
  1653 
  1654     /** Return tree simulating the assignment <this.this$n = this$n>.
  1654     /** Return tree simulating the assignment <this.this$n = this$n>.
  1655      */
  1655      */
  1656     JCStatement initOuterThis(int pos) {
  1656     JCStatement initOuterThis(int pos) {
  1657         VarSymbol rhs = outerThisStack.head;
  1657         VarSymbol rhs = outerThisStack.head;
  1658         assert rhs.owner.kind == MTH;
  1658         Assert.check(rhs.owner.kind == MTH);
  1659         VarSymbol lhs = outerThisStack.tail.head;
  1659         VarSymbol lhs = outerThisStack.tail.head;
  1660         assert rhs.owner.owner == lhs.owner;
  1660         Assert.check(rhs.owner.owner == lhs.owner);
  1661         make.at(pos);
  1661         make.at(pos);
  1662         return
  1662         return
  1663             make.Exec(
  1663             make.Exec(
  1664                 make.Assign(
  1664                 make.Assign(
  1665                     make.Select(make.This(lhs.owner.erasure(types)), lhs),
  1665                     make.Select(make.This(lhs.owner.erasure(types)), lhs),
  1854         return make.Block(0, List.of(tryCatch));
  1854         return make.Block(0, List.of(tryCatch));
  1855     }
  1855     }
  1856     // where
  1856     // where
  1857         /** Create an attributed tree of the form left.name(). */
  1857         /** Create an attributed tree of the form left.name(). */
  1858         private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) {
  1858         private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) {
  1859             assert left.type != null;
  1859             Assert.checkNonNull(left.type);
  1860             Symbol funcsym = lookupMethod(make_pos, name, left.type,
  1860             Symbol funcsym = lookupMethod(make_pos, name, left.type,
  1861                                           TreeInfo.types(args));
  1861                                           TreeInfo.types(args));
  1862             return make.App(make.Select(left, funcsym), args);
  1862             return make.App(make.Select(left, funcsym), args);
  1863         }
  1863         }
  1864 
  1864 
  2397          */
  2397          */
  2398         MethodSymbol valueOfSym = lookupMethod(tree.pos(),
  2398         MethodSymbol valueOfSym = lookupMethod(tree.pos(),
  2399                          names.valueOf,
  2399                          names.valueOf,
  2400                          tree.sym.type,
  2400                          tree.sym.type,
  2401                          List.of(syms.stringType));
  2401                          List.of(syms.stringType));
  2402         assert (valueOfSym.flags() & STATIC) != 0;
  2402         Assert.check((valueOfSym.flags() & STATIC) != 0);
  2403         VarSymbol nameArgSym = valueOfSym.params.head;
  2403         VarSymbol nameArgSym = valueOfSym.params.head;
  2404         JCIdent nameVal = make.Ident(nameArgSym);
  2404         JCIdent nameVal = make.Ident(nameArgSym);
  2405         JCStatement enum_ValueOf =
  2405         JCStatement enum_ValueOf =
  2406             make.Return(make.TypeCast(tree.sym.type,
  2406             make.Return(make.TypeCast(tree.sym.type,
  2407                                       makeCall(make.Ident(syms.enumSym),
  2407                                       makeCall(make.Ident(syms.enumSym),
  3414                 JCExpression expression = oneCase.getExpression();
  3414                 JCExpression expression = oneCase.getExpression();
  3415 
  3415 
  3416                 if (expression != null) { // expression for a "default" case is null
  3416                 if (expression != null) { // expression for a "default" case is null
  3417                     String labelExpr = (String) expression.type.constValue();
  3417                     String labelExpr = (String) expression.type.constValue();
  3418                     Integer mapping = caseLabelToPosition.put(labelExpr, casePosition);
  3418                     Integer mapping = caseLabelToPosition.put(labelExpr, casePosition);
  3419                     assert mapping == null;
  3419                     Assert.checkNull(mapping);
  3420                     int hashCode = labelExpr.hashCode();
  3420                     int hashCode = labelExpr.hashCode();
  3421 
  3421 
  3422                     Set<String> stringSet = hashToString.get(hashCode);
  3422                     Set<String> stringSet = hashToString.get(hashCode);
  3423                     if (stringSet == null) {
  3423                     if (stringSet == null) {
  3424                         stringSet = new LinkedHashSet<String>(1, 1.0f);
  3424                         stringSet = new LinkedHashSet<String>(1, 1.0f);
  3425                         stringSet.add(labelExpr);
  3425                         stringSet.add(labelExpr);
  3426                         hashToString.put(hashCode, stringSet);
  3426                         hashToString.put(hashCode, stringSet);
  3427                     } else {
  3427                     } else {
  3428                         boolean added = stringSet.add(labelExpr);
  3428                         boolean added = stringSet.add(labelExpr);
  3429                         assert added;
  3429                         Assert.check(added);
  3430                     }
  3430                     }
  3431                 }
  3431                 }
  3432                 casePosition++;
  3432                 casePosition++;
  3433             }
  3433             }
  3434 
  3434 
  3476             JCSwitch switch1 = make.Switch(hashCodeCall,
  3476             JCSwitch switch1 = make.Switch(hashCodeCall,
  3477                                         caseBuffer.toList());
  3477                                         caseBuffer.toList());
  3478             for(Map.Entry<Integer, Set<String>> entry : hashToString.entrySet()) {
  3478             for(Map.Entry<Integer, Set<String>> entry : hashToString.entrySet()) {
  3479                 int hashCode = entry.getKey();
  3479                 int hashCode = entry.getKey();
  3480                 Set<String> stringsWithHashCode = entry.getValue();
  3480                 Set<String> stringsWithHashCode = entry.getValue();
  3481                 assert stringsWithHashCode.size() >= 1;
  3481                 Assert.check(stringsWithHashCode.size() >= 1);
  3482 
  3482 
  3483                 JCStatement elsepart = null;
  3483                 JCStatement elsepart = null;
  3484                 for(String caseLabel : stringsWithHashCode ) {
  3484                 for(String caseLabel : stringsWithHashCode ) {
  3485                     JCMethodInvocation stringEqualsCall = makeCall(make.Ident(dollar_s),
  3485                     JCMethodInvocation stringEqualsCall = makeCall(make.Ident(dollar_s),
  3486                                                                    names.equals,
  3486                                                                    names.equals,
  3690         Symbol ordinalSym = lookupMethod(cdef.pos(),
  3690         Symbol ordinalSym = lookupMethod(cdef.pos(),
  3691                                          names.ordinal,
  3691                                          names.ordinal,
  3692                                          cdef.type,
  3692                                          cdef.type,
  3693                                          List.<Type>nil());
  3693                                          List.<Type>nil());
  3694 
  3694 
  3695         assert(ordinalSym != null);
  3695         Assert.check(ordinalSym instanceof MethodSymbol);
  3696         assert(ordinalSym instanceof MethodSymbol);
       
  3697 
  3696 
  3698         JCStatement ret = make.Return(make.Ident(ordinalSymbol));
  3697         JCStatement ret = make.Return(make.Ident(ordinalSymbol));
  3699         cdef.defs = cdef.defs.append(make.MethodDef((MethodSymbol)ordinalSym,
  3698         cdef.defs = cdef.defs.append(make.MethodDef((MethodSymbol)ordinalSym,
  3700                                                     make.Block(0L, List.of(ret))));
  3699                                                     make.Block(0L, List.of(ret))));
  3701 
  3700 
  3707         Symbol nameSym = lookupMethod(cdef.pos(),
  3706         Symbol nameSym = lookupMethod(cdef.pos(),
  3708                                    names._name,
  3707                                    names._name,
  3709                                    cdef.type,
  3708                                    cdef.type,
  3710                                    List.<Type>nil());
  3709                                    List.<Type>nil());
  3711 
  3710 
  3712         assert(nameSym != null);
  3711         Assert.check(nameSym instanceof MethodSymbol);
  3713         assert(nameSym instanceof MethodSymbol);
       
  3714 
  3712 
  3715         JCStatement ret = make.Return(make.Ident(nameSymbol));
  3713         JCStatement ret = make.Return(make.Ident(nameSymbol));
  3716 
  3714 
  3717         cdef.defs = cdef.defs.append(make.MethodDef((MethodSymbol)nameSym,
  3715         cdef.defs = cdef.defs.append(make.MethodDef((MethodSymbol)nameSym,
  3718                                                     make.Block(0L, List.of(ret))));
  3716                                                     make.Block(0L, List.of(ret))));
  3759         Symbol compareToSym = lookupMethod(cdef.pos(),
  3757         Symbol compareToSym = lookupMethod(cdef.pos(),
  3760                                    names.compareTo,
  3758                                    names.compareTo,
  3761                                    cdef.type,
  3759                                    cdef.type,
  3762                                    List.of(cdef.sym.type));
  3760                                    List.of(cdef.sym.type));
  3763 
  3761 
  3764         assert(compareToSym != null);
  3762         Assert.check(compareToSym instanceof MethodSymbol);
  3765         assert(compareToSym instanceof MethodSymbol);
       
  3766 
  3763 
  3767         JCMethodDecl compareToDecl = (JCMethodDecl) TreeInfo.declarationFor(compareToSym, cdef);
  3764         JCMethodDecl compareToDecl = (JCMethodDecl) TreeInfo.declarationFor(compareToSym, cdef);
  3768 
  3765 
  3769         ListBuffer<JCStatement> blockStatements = new ListBuffer<JCStatement>();
  3766         ListBuffer<JCStatement> blockStatements = new ListBuffer<JCStatement>();
  3770 
  3767