langtools/src/share/classes/com/sun/tools/javac/comp/Resolve.java
changeset 939 38e24969c7e9
parent 735 372aa565a221
child 1040 c0f5acfd9d15
equal deleted inserted replaced
938:13aae74ca013 939:38e24969c7e9
  1440             log.error(pos, "cant.ref.before.ctor.called", "this");
  1440             log.error(pos, "cant.ref.before.ctor.called", "this");
  1441         return thisType;
  1441         return thisType;
  1442     }
  1442     }
  1443 
  1443 
  1444 /* ***************************************************************************
  1444 /* ***************************************************************************
  1445  *  Methods related to kinds
       
  1446  ****************************************************************************/
       
  1447 
       
  1448     /** A localized string describing a given kind.
       
  1449      */
       
  1450     static JCDiagnostic kindName(int kind) {
       
  1451         switch (kind) {
       
  1452         case PCK: return JCDiagnostic.fragment("kindname.package");
       
  1453         case TYP: return JCDiagnostic.fragment("kindname.class");
       
  1454         case VAR: return JCDiagnostic.fragment("kindname.variable");
       
  1455         case VAL: return JCDiagnostic.fragment("kindname.value");
       
  1456         case MTH: return JCDiagnostic.fragment("kindname.method");
       
  1457         default : return JCDiagnostic.fragment("kindname",
       
  1458                                                Integer.toString(kind)); //debug
       
  1459         }
       
  1460     }
       
  1461 
       
  1462     static JCDiagnostic kindName(Symbol sym) {
       
  1463         switch (sym.getKind()) {
       
  1464         case PACKAGE:
       
  1465             return JCDiagnostic.fragment("kindname.package");
       
  1466 
       
  1467         case ENUM:
       
  1468         case ANNOTATION_TYPE:
       
  1469         case INTERFACE:
       
  1470         case CLASS:
       
  1471             return JCDiagnostic.fragment("kindname.class");
       
  1472 
       
  1473         case TYPE_PARAMETER:
       
  1474             return JCDiagnostic.fragment("kindname.type.variable");
       
  1475 
       
  1476         case ENUM_CONSTANT:
       
  1477         case FIELD:
       
  1478         case PARAMETER:
       
  1479         case LOCAL_VARIABLE:
       
  1480         case EXCEPTION_PARAMETER:
       
  1481             return JCDiagnostic.fragment("kindname.variable");
       
  1482 
       
  1483         case METHOD:
       
  1484         case CONSTRUCTOR:
       
  1485         case STATIC_INIT:
       
  1486         case INSTANCE_INIT:
       
  1487             return JCDiagnostic.fragment("kindname.method");
       
  1488 
       
  1489         default:
       
  1490             if (sym.kind == VAL)
       
  1491                 // I don't think this can happen but it can't harm
       
  1492                 // playing it safe --ahe
       
  1493                 return JCDiagnostic.fragment("kindname.value");
       
  1494             else
       
  1495                 return JCDiagnostic.fragment("kindname", sym.getKind()); // debug
       
  1496         }
       
  1497     }
       
  1498 
       
  1499     /** A localized string describing a given set of kinds.
       
  1500      */
       
  1501     static JCDiagnostic kindNames(int kind) {
       
  1502         StringBuffer key = new StringBuffer();
       
  1503         key.append("kindname");
       
  1504         if ((kind & VAL) != 0)
       
  1505             key.append(((kind & VAL) == VAR) ? ".variable" : ".value");
       
  1506         if ((kind & MTH) != 0) key.append(".method");
       
  1507         if ((kind & TYP) != 0) key.append(".class");
       
  1508         if ((kind & PCK) != 0) key.append(".package");
       
  1509         return JCDiagnostic.fragment(key.toString(), kind);
       
  1510     }
       
  1511 
       
  1512     /** A localized string describing the kind -- either class or interface --
       
  1513      *  of a given type.
       
  1514      */
       
  1515     static JCDiagnostic typeKindName(Type t) {
       
  1516         if (t.tag == TYPEVAR ||
       
  1517             t.tag == CLASS && (t.tsym.flags() & COMPOUND) != 0)
       
  1518             return JCDiagnostic.fragment("kindname.type.variable.bound");
       
  1519         else if (t.tag == PACKAGE)
       
  1520             return JCDiagnostic.fragment("kindname.package");
       
  1521         else if ((t.tsym.flags_field & ANNOTATION) != 0)
       
  1522             return JCDiagnostic.fragment("kindname.annotation");
       
  1523         else if ((t.tsym.flags_field & INTERFACE) != 0)
       
  1524             return JCDiagnostic.fragment("kindname.interface");
       
  1525         else
       
  1526             return JCDiagnostic.fragment("kindname.class");
       
  1527     }
       
  1528 
       
  1529     /** A localized string describing the kind of a missing symbol, given an
       
  1530      *  error kind.
       
  1531      */
       
  1532     static JCDiagnostic absentKindName(int kind) {
       
  1533         switch (kind) {
       
  1534         case ABSENT_VAR:
       
  1535             return JCDiagnostic.fragment("kindname.variable");
       
  1536         case WRONG_MTHS: case WRONG_MTH: case ABSENT_MTH:
       
  1537             return JCDiagnostic.fragment("kindname.method");
       
  1538         case ABSENT_TYP:
       
  1539             return JCDiagnostic.fragment("kindname.class");
       
  1540         default:
       
  1541             return JCDiagnostic.fragment("kindname", kind);
       
  1542         }
       
  1543     }
       
  1544 
       
  1545 /* ***************************************************************************
       
  1546  *  ResolveError classes, indicating error situations when accessing symbols
  1445  *  ResolveError classes, indicating error situations when accessing symbols
  1547  ****************************************************************************/
  1446  ****************************************************************************/
  1548 
  1447 
  1549     public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
  1448     public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
  1550         AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
  1449         AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
  1631          *  @param typeargtypes  The invocation's type arguments,
  1530          *  @param typeargtypes  The invocation's type arguments,
  1632          *                   if we looked for a method.
  1531          *                   if we looked for a method.
  1633          */
  1532          */
  1634         void report(Log log, DiagnosticPosition pos, Type site, Name name,
  1533         void report(Log log, DiagnosticPosition pos, Type site, Name name,
  1635                     List<Type> argtypes, List<Type> typeargtypes) {
  1534                     List<Type> argtypes, List<Type> typeargtypes) {
       
  1535             if (argtypes == null)
       
  1536                 argtypes = List.nil();
       
  1537             if (typeargtypes == null)
       
  1538                 typeargtypes = List.nil();
  1636             if (name != name.table.error) {
  1539             if (name != name.table.error) {
  1637                 JCDiagnostic kindname = absentKindName(kind);
  1540                 KindName kindname = absentKind(kind);
  1638                 String idname = name.toString();
  1541                 Name idname = name;
  1639                 String args = "";
       
  1640                 String typeargs = "";
       
  1641                 if (kind >= WRONG_MTHS && kind <= ABSENT_MTH) {
  1542                 if (kind >= WRONG_MTHS && kind <= ABSENT_MTH) {
  1642                     if (isOperator(name)) {
  1543                     if (isOperator(name)) {
  1643                         log.error(pos, "operator.cant.be.applied",
  1544                         log.error(pos, "operator.cant.be.applied",
  1644                                   name, Type.toString(argtypes));
  1545                                   name, argtypes);
  1645                         return;
  1546                         return;
  1646                     }
  1547                     }
  1647                     if (name == name.table.init) {
  1548                     if (name == name.table.init) {
  1648                         kindname = JCDiagnostic.fragment("kindname.constructor");
  1549                         kindname = KindName.CONSTRUCTOR;
  1649                         idname = site.tsym.name.toString();
  1550                         idname = site.tsym.name;
  1650                     }
  1551                     }
  1651                     args = "(" + Type.toString(argtypes) + ")";
       
  1652                     if (typeargtypes != null && typeargtypes.nonEmpty())
       
  1653                         typeargs = "<" + Type.toString(typeargtypes) + ">";
       
  1654                 }
  1552                 }
  1655                 if (kind == WRONG_MTH) {
  1553                 if (kind == WRONG_MTH) {
       
  1554                     Symbol ws = wrongSym.asMemberOf(site, types);
  1656                     log.error(pos,
  1555                     log.error(pos,
  1657                               "cant.apply.symbol" + (explanation != null ? ".1" : ""),
  1556                               "cant.apply.symbol" + (explanation != null ? ".1" : ""),
  1658                               wrongSym.asMemberOf(site, types),
  1557                               kindname,
  1659                               wrongSym.location(site, types),
  1558                               ws.name == names.init ? ws.owner.name : ws.name,
  1660                               typeargs,
  1559                               ws.type.getParameterTypes(),
  1661                               Type.toString(argtypes),
  1560                               argtypes,
       
  1561                               kindName(ws.owner),
       
  1562                               ws.owner.type,
  1662                               explanation);
  1563                               explanation);
  1663                 } else if (site.tsym.name.len != 0) {
  1564                 } else if (site.tsym.name.len != 0) {
  1664                     if (site.tsym.kind == PCK && !site.tsym.exists())
  1565                     if (site.tsym.kind == PCK && !site.tsym.exists())
  1665                         log.error(pos, "doesnt.exist", site.tsym);
  1566                         log.error(pos, "doesnt.exist", site.tsym);
  1666                     else
  1567                     else {
  1667                         log.error(pos, "cant.resolve.location",
  1568                         String errKey = getErrorKey("cant.resolve.location",
  1668                                   kindname, idname, args, typeargs,
  1569                                                     argtypes, typeargtypes,
  1669                                   typeKindName(site), site);
  1570                                                     kindname);
       
  1571                         log.error(pos, errKey, kindname, idname, //symbol kindname, name
       
  1572                                   typeargtypes, argtypes, //type parameters and arguments (if any)
       
  1573                                   typeKindName(site), site); //location kindname, type
       
  1574                     }
  1670                 } else {
  1575                 } else {
  1671                     log.error(pos, "cant.resolve", kindname, idname, args, typeargs);
  1576                     String errKey = getErrorKey("cant.resolve",
       
  1577                                                 argtypes, typeargtypes,
       
  1578                                                 kindname);
       
  1579                     log.error(pos, errKey, kindname, idname, //symbol kindname, name
       
  1580                               typeargtypes, argtypes); //type parameters and arguments (if any)
  1672                 }
  1581                 }
  1673             }
  1582             }
  1674         }
  1583         }
  1675 //where
  1584         //where
  1676             /** A name designates an operator if it consists
  1585         String getErrorKey(String key, List<Type> argtypes, List<Type> typeargtypes, KindName kindname) {
  1677              *  of a non-empty sequence of operator symbols +-~!/*%&|^<>=
  1586             String suffix = "";
  1678              */
  1587             switch (kindname) {
  1679             boolean isOperator(Name name) {
  1588                 case METHOD:
  1680                 int i = 0;
  1589                 case CONSTRUCTOR: {
  1681                 while (i < name.len &&
  1590                     suffix += ".args";
  1682                        "+-~!*/%&|^<>=".indexOf(name.byteAt(i)) >= 0) i++;
  1591                     suffix += typeargtypes.nonEmpty() ? ".params" : "";
  1683                 return i > 0 && i == name.len;
  1592                 }
  1684             }
  1593             }
       
  1594             return key + suffix;
       
  1595         }
       
  1596 
       
  1597         /** A name designates an operator if it consists
       
  1598          *  of a non-empty sequence of operator symbols +-~!/*%&|^<>=
       
  1599          */
       
  1600         boolean isOperator(Name name) {
       
  1601             int i = 0;
       
  1602             while (i < name.len &&
       
  1603                    "+-~!*/%&|^<>=".indexOf(name.byteAt(i)) >= 0) i++;
       
  1604             return i > 0 && i == name.len;
       
  1605         }
  1685     }
  1606     }
  1686 
  1607 
  1687     /** Resolve error class indicating that a symbol is not accessible.
  1608     /** Resolve error class indicating that a symbol is not accessible.
  1688      */
  1609      */
  1689     class AccessError extends ResolveError {
  1610     class AccessError extends ResolveError {
  1724                         && !isAccessible(env, this.site)))
  1645                         && !isAccessible(env, this.site)))
  1725                     log.error(pos, "not.def.access.class.intf.cant.access",
  1646                     log.error(pos, "not.def.access.class.intf.cant.access",
  1726                         sym, sym.location());
  1647                         sym, sym.location());
  1727                 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0)
  1648                 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0)
  1728                     log.error(pos, "report.access", sym,
  1649                     log.error(pos, "report.access", sym,
  1729                               TreeInfo.flagNames(sym.flags() & (PRIVATE | PROTECTED)),
  1650                               asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
  1730                               sym.location());
  1651                               sym.location());
  1731                 else
  1652                 else
  1732                     log.error(pos, "not.def.public.cant.access",
  1653                     log.error(pos, "not.def.public.cant.access",
  1733                               sym, sym.location());
  1654                               sym, sym.location());
  1734             }
  1655             }
  1757                     DiagnosticPosition pos,
  1678                     DiagnosticPosition pos,
  1758                     Type site,
  1679                     Type site,
  1759                     Name name,
  1680                     Name name,
  1760                     List<Type> argtypes,
  1681                     List<Type> argtypes,
  1761                     List<Type> typeargtypes) {
  1682                     List<Type> typeargtypes) {
  1762             String symstr = ((sym.kind == TYP && sym.type.tag == CLASS)
  1683             Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
  1763                 ? types.erasure(sym.type)
  1684                 ? types.erasure(sym.type).tsym
  1764                 : sym).toString();
  1685                 : sym);
  1765             log.error(pos, "non-static.cant.be.ref",
  1686             log.error(pos, "non-static.cant.be.ref",
  1766                       kindName(sym), symstr);
  1687                       kindName(sym), errSym);
  1767         }
  1688         }
  1768     }
  1689     }
  1769 
  1690 
  1770     /** Resolve error class indicating an ambiguous reference.
  1691     /** Resolve error class indicating an ambiguous reference.
  1771      */
  1692      */