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

changeset 110
91eea580fbe9
parent 94
6542933af8f4
child 113
eff38cc97183
equal deleted inserted replaced
107:5a9b808557b6 110:91eea580fbe9
157 157
158 /** Check kind and type of given tree against protokind and prototype. 158 /** Check kind and type of given tree against protokind and prototype.
159 * If check succeeds, store type in tree and return it. 159 * If check succeeds, store type in tree and return it.
160 * If check fails, store errType in tree and return it. 160 * If check fails, store errType in tree and return it.
161 * No checks are performed if the prototype is a method type. 161 * No checks are performed if the prototype is a method type.
162 * Its not necessary in this case since we know that kind and type 162 * It is not necessary in this case since we know that kind and type
163 * are correct. 163 * are correct.
164 * 164 *
165 * @param tree The tree whose kind and type is checked 165 * @param tree The tree whose kind and type is checked
166 * @param owntype The computed type of the tree 166 * @param owntype The computed type of the tree
167 * @param ownkind The computed kind of the tree 167 * @param ownkind The computed kind of the tree
174 owntype = chk.checkType(tree.pos(), owntype, pt); 174 owntype = chk.checkType(tree.pos(), owntype, pt);
175 } else { 175 } else {
176 log.error(tree.pos(), "unexpected.type", 176 log.error(tree.pos(), "unexpected.type",
177 kindNames(pkind), 177 kindNames(pkind),
178 kindName(ownkind)); 178 kindName(ownkind));
179 owntype = syms.errType; 179 owntype = types.createErrorType(owntype);
180 } 180 }
181 } 181 }
182 tree.type = owntype; 182 tree.type = owntype;
183 return owntype; 183 return owntype;
184 } 184 }
522 boolean checkExtensible) { 522 boolean checkExtensible) {
523 if (t.tag == TYPEVAR && !classExpected && !interfaceExpected) { 523 if (t.tag == TYPEVAR && !classExpected && !interfaceExpected) {
524 // check that type variable is already visible 524 // check that type variable is already visible
525 if (t.getUpperBound() == null) { 525 if (t.getUpperBound() == null) {
526 log.error(tree.pos(), "illegal.forward.ref"); 526 log.error(tree.pos(), "illegal.forward.ref");
527 return syms.errType; 527 return types.createErrorType(t);
528 } 528 }
529 } else { 529 } else {
530 t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics); 530 t = chk.checkClassType(tree.pos(), t, checkExtensible|!allowGenerics);
531 } 531 }
532 if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) { 532 if (interfaceExpected && (t.tsym.flags() & INTERFACE) == 0) {
533 log.error(tree.pos(), "intf.expected.here"); 533 log.error(tree.pos(), "intf.expected.here");
534 // return errType is necessary since otherwise there might 534 // return errType is necessary since otherwise there might
535 // be undetected cycles which cause attribution to loop 535 // be undetected cycles which cause attribution to loop
536 return syms.errType; 536 return types.createErrorType(t);
537 } else if (checkExtensible && 537 } else if (checkExtensible &&
538 classExpected && 538 classExpected &&
539 (t.tsym.flags() & INTERFACE) != 0) { 539 (t.tsym.flags() & INTERFACE) != 0) {
540 log.error(tree.pos(), "no.intf.expected.here"); 540 log.error(tree.pos(), "no.intf.expected.here");
541 return syms.errType; 541 return types.createErrorType(t);
542 } 542 }
543 if (checkExtensible && 543 if (checkExtensible &&
544 ((t.tsym.flags() & FINAL) != 0)) { 544 ((t.tsym.flags() & FINAL) != 0)) {
545 log.error(tree.pos(), 545 log.error(tree.pos(),
546 "cant.inherit.from.final", t.tsym); 546 "cant.inherit.from.final", t.tsym);
802 if (elemtype == null) { 802 if (elemtype == null) {
803 // or perhaps expr implements Iterable<T>? 803 // or perhaps expr implements Iterable<T>?
804 Type base = types.asSuper(exprType, syms.iterableType.tsym); 804 Type base = types.asSuper(exprType, syms.iterableType.tsym);
805 if (base == null) { 805 if (base == null) {
806 log.error(tree.expr.pos(), "foreach.not.applicable.to.type"); 806 log.error(tree.expr.pos(), "foreach.not.applicable.to.type");
807 elemtype = syms.errType; 807 elemtype = types.createErrorType(exprType);
808 } else { 808 } else {
809 List<Type> iterableParams = base.allparams(); 809 List<Type> iterableParams = base.allparams();
810 elemtype = iterableParams.isEmpty() 810 elemtype = iterableParams.isEmpty()
811 ? syms.objectType 811 ? syms.objectType
812 : types.upperBound(iterableParams.head); 812 : types.upperBound(iterableParams.head);
1217 // constructor is defined. 1217 // constructor is defined.
1218 Type site = env.enclClass.sym.type; 1218 Type site = env.enclClass.sym.type;
1219 if (methName == names._super) { 1219 if (methName == names._super) {
1220 if (site == syms.objectType) { 1220 if (site == syms.objectType) {
1221 log.error(tree.meth.pos(), "no.superclass", site); 1221 log.error(tree.meth.pos(), "no.superclass", site);
1222 site = syms.errType; 1222 site = types.createErrorType(syms.objectType);
1223 } else { 1223 } else {
1224 site = types.supertype(site); 1224 site = types.supertype(site);
1225 } 1225 }
1226 } 1226 }
1227 1227
1349 MethodType mt = new MethodType(argtypes, null, null, syms.methodClass); 1349 MethodType mt = new MethodType(argtypes, null, null, syms.methodClass);
1350 return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt); 1350 return (typeargtypes == null) ? mt : (Type)new ForAll(typeargtypes, mt);
1351 } 1351 }
1352 1352
1353 public void visitNewClass(JCNewClass tree) { 1353 public void visitNewClass(JCNewClass tree) {
1354 Type owntype = syms.errType; 1354 Type owntype = types.createErrorType(tree.type);
1355 1355
1356 // The local environment of a class creation is 1356 // The local environment of a class creation is
1357 // a new environment nested in the current one. 1357 // a new environment nested in the current one.
1358 Env<AttrContext> localEnv = env.dup(tree, env.info.dup()); 1358 Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
1359 1359
1549 tree.type = arg.type; 1549 tree.type = arg.type;
1550 return tree; 1550 return tree;
1551 } 1551 }
1552 1552
1553 public void visitNewArray(JCNewArray tree) { 1553 public void visitNewArray(JCNewArray tree) {
1554 Type owntype = syms.errType; 1554 Type owntype = types.createErrorType(tree.type);
1555 Type elemtype; 1555 Type elemtype;
1556 if (tree.elemtype != null) { 1556 if (tree.elemtype != null) {
1557 elemtype = attribType(tree.elemtype, env); 1557 elemtype = attribType(tree.elemtype, env);
1558 chk.validate(tree.elemtype); 1558 chk.validate(tree.elemtype);
1559 owntype = elemtype; 1559 owntype = elemtype;
1569 } else { 1569 } else {
1570 if (pt.tag != ERROR) { 1570 if (pt.tag != ERROR) {
1571 log.error(tree.pos(), "illegal.initializer.for.type", 1571 log.error(tree.pos(), "illegal.initializer.for.type",
1572 pt); 1572 pt);
1573 } 1573 }
1574 elemtype = syms.errType; 1574 elemtype = types.createErrorType(pt);
1575 } 1575 }
1576 } 1576 }
1577 if (tree.elems != null) { 1577 if (tree.elems != null) {
1578 attribExprs(tree.elems, env, elemtype); 1578 attribExprs(tree.elems, env, elemtype);
1579 owntype = new ArrayType(elemtype, syms.arrayClass); 1579 owntype = new ArrayType(elemtype, syms.arrayClass);
1629 1629
1630 // Find operator. 1630 // Find operator.
1631 Symbol operator = tree.operator = 1631 Symbol operator = tree.operator =
1632 rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype); 1632 rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype);
1633 1633
1634 Type owntype = syms.errType; 1634 Type owntype = types.createErrorType(tree.type);
1635 if (operator.kind == MTH) { 1635 if (operator.kind == MTH) {
1636 owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) 1636 owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC)
1637 ? tree.arg.type 1637 ? tree.arg.type
1638 : operator.type.getReturnType(); 1638 : operator.type.getReturnType();
1639 int opc = ((OperatorSymbol)operator).opcode; 1639 int opc = ((OperatorSymbol)operator).opcode;
1665 1665
1666 // Find operator. 1666 // Find operator.
1667 Symbol operator = tree.operator = 1667 Symbol operator = tree.operator =
1668 rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right); 1668 rs.resolveBinaryOperator(tree.pos(), tree.getTag(), env, left, right);
1669 1669
1670 Type owntype = syms.errType; 1670 Type owntype = types.createErrorType(tree.type);
1671 if (operator.kind == MTH) { 1671 if (operator.kind == MTH) {
1672 owntype = operator.type.getReturnType(); 1672 owntype = operator.type.getReturnType();
1673 int opc = chk.checkOperator(tree.lhs.pos(), 1673 int opc = chk.checkOperator(tree.lhs.pos(),
1674 (OperatorSymbol)operator, 1674 (OperatorSymbol)operator,
1675 tree.getTag(), 1675 tree.getTag(),
1726 chk.checkCastable(tree.expr.pos(), exprtype, clazztype); 1726 chk.checkCastable(tree.expr.pos(), exprtype, clazztype);
1727 result = check(tree, syms.booleanType, VAL, pkind, pt); 1727 result = check(tree, syms.booleanType, VAL, pkind, pt);
1728 } 1728 }
1729 1729
1730 public void visitIndexed(JCArrayAccess tree) { 1730 public void visitIndexed(JCArrayAccess tree) {
1731 Type owntype = syms.errType; 1731 Type owntype = types.createErrorType(tree.type);
1732 Type atype = attribExpr(tree.indexed, env); 1732 Type atype = attribExpr(tree.indexed, env);
1733 attribExpr(tree.index, env, syms.intType); 1733 attribExpr(tree.index, env, syms.intType);
1734 if (types.isArray(atype)) 1734 if (types.isArray(atype))
1735 owntype = types.elemtype(atype); 1735 owntype = types.elemtype(atype);
1736 else if (atype.tag != ERROR) 1736 else if (atype.tag != ERROR)
1847 Type elt = site; 1847 Type elt = site;
1848 while (elt.tag == ARRAY) 1848 while (elt.tag == ARRAY)
1849 elt = ((ArrayType)elt).elemtype; 1849 elt = ((ArrayType)elt).elemtype;
1850 if (elt.tag == TYPEVAR) { 1850 if (elt.tag == TYPEVAR) {
1851 log.error(tree.pos(), "type.var.cant.be.deref"); 1851 log.error(tree.pos(), "type.var.cant.be.deref");
1852 result = syms.errType; 1852 result = types.createErrorType(tree.type);
1853 return; 1853 return;
1854 } 1854 }
1855 } 1855 }
1856 1856
1857 // If qualifier symbol is a type or `super', assert `selectSuper' 1857 // If qualifier symbol is a type or `super', assert `selectSuper'
2007 } else { 2007 } else {
2008 return sym; 2008 return sym;
2009 } 2009 }
2010 case ERROR: 2010 case ERROR:
2011 // preserve identifier names through errors 2011 // preserve identifier names through errors
2012 return new ErrorType(name, site.tsym).tsym; 2012 return types.createErrorType(name, site.tsym, site).tsym;
2013 default: 2013 default:
2014 // The qualifier expression is of a primitive type -- only 2014 // The qualifier expression is of a primitive type -- only
2015 // .class is allowed for these. 2015 // .class is allowed for these.
2016 if (name == names._class) { 2016 if (name == names._class) {
2017 // In this case, we have already made sure in Select that 2017 // In this case, we have already made sure in Select that
2057 Symbol sym, 2057 Symbol sym,
2058 Env<AttrContext> env, 2058 Env<AttrContext> env,
2059 int pkind, 2059 int pkind,
2060 Type pt, 2060 Type pt,
2061 boolean useVarargs) { 2061 boolean useVarargs) {
2062 if (pt.isErroneous()) return syms.errType; 2062 if (pt.isErroneous()) return types.createErrorType(site);
2063 Type owntype; // The computed type of this identifier occurrence. 2063 Type owntype; // The computed type of this identifier occurrence.
2064 switch (sym.kind) { 2064 switch (sym.kind) {
2065 case TYP: 2065 case TYP:
2066 // For types, the computed type equals the symbol's type, 2066 // For types, the computed type equals the symbol's type,
2067 // except for two situations: 2067 // except for two situations:
2127 if (env.info.tvars.nonEmpty()) { 2127 if (env.info.tvars.nonEmpty()) {
2128 Type owntype1 = new ForAll(env.info.tvars, owntype); 2128 Type owntype1 = new ForAll(env.info.tvars, owntype);
2129 for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail) 2129 for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail)
2130 if (!owntype.contains(l.head)) { 2130 if (!owntype.contains(l.head)) {
2131 log.error(tree.pos(), "undetermined.type", owntype1); 2131 log.error(tree.pos(), "undetermined.type", owntype1);
2132 owntype1 = syms.errType; 2132 owntype1 = types.createErrorType(owntype1);
2133 } 2133 }
2134 owntype = owntype1; 2134 owntype = owntype1;
2135 } 2135 }
2136 2136
2137 // If the variable is a constant, record constant value in 2137 // If the variable is a constant, record constant value in
2330 if (!pt.isErroneous()) 2330 if (!pt.isErroneous())
2331 log.error(env.tree.pos(), 2331 log.error(env.tree.pos(),
2332 "internal.error.cant.instantiate", 2332 "internal.error.cant.instantiate",
2333 sym, site, 2333 sym, site,
2334 Type.toString(pt.getParameterTypes())); 2334 Type.toString(pt.getParameterTypes()));
2335 owntype = syms.errType; 2335 owntype = types.createErrorType(site);
2336 } else { 2336 } else {
2337 // System.out.println("call : " + env.tree); 2337 // System.out.println("call : " + env.tree);
2338 // System.out.println("method : " + owntype); 2338 // System.out.println("method : " + owntype);
2339 // System.out.println("actuals: " + argtypes); 2339 // System.out.println("actuals: " + argtypes);
2340 List<Type> formals = owntype.getParameterTypes(); 2340 List<Type> formals = owntype.getParameterTypes();
2452 /** Visitor method for parameterized types. 2452 /** Visitor method for parameterized types.
2453 * Bound checking is left until later, since types are attributed 2453 * Bound checking is left until later, since types are attributed
2454 * before supertype structure is completely known 2454 * before supertype structure is completely known
2455 */ 2455 */
2456 public void visitTypeApply(JCTypeApply tree) { 2456 public void visitTypeApply(JCTypeApply tree) {
2457 Type owntype = syms.errType; 2457 Type owntype = types.createErrorType(tree.type);
2458 2458
2459 // Attribute functor part of application and make sure it's a class. 2459 // Attribute functor part of application and make sure it's a class.
2460 Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env)); 2460 Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env));
2461 2461
2462 // Attribute type parameters 2462 // Attribute type parameters
2496 log.error(tree.pos(), "wrong.number.type.args", 2496 log.error(tree.pos(), "wrong.number.type.args",
2497 Integer.toString(formals.length())); 2497 Integer.toString(formals.length()));
2498 } else { 2498 } else {
2499 log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym); 2499 log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym);
2500 } 2500 }
2501 owntype = syms.errType; 2501 owntype = types.createErrorType(tree.type);
2502 } 2502 }
2503 } 2503 }
2504 result = check(tree, owntype, TYP, pkind, pt); 2504 result = check(tree, owntype, TYP, pkind, pt);
2505 } 2505 }
2506 2506

mercurial