langtools/src/share/classes/com/sun/tools/javac/comp/Attr.java
changeset 5320 e2aaa958b02d
parent 5002 12a9e8562200
child 5321 c8efe769cb3b
equal deleted inserted replaced
5319:63dc7f367a37 5320:e2aaa958b02d
   205         }
   205         }
   206         tree.type = owntype;
   206         tree.type = owntype;
   207         return owntype;
   207         return owntype;
   208     }
   208     }
   209 
   209 
   210     Type checkReturn(JCTree tree, Type owntype, int ownkind, int pkind, Type pt) {
       
   211         if (owntype.tag != ERROR && pt.tag != METHOD && pt.tag != FORALL) {
       
   212             if ((ownkind & ~pkind) == 0) {
       
   213                 owntype = chk.checkReturnType(tree.pos(), owntype, pt);
       
   214             } else {
       
   215                 log.error(tree.pos(), "unexpected.type",
       
   216                           kindNames(pkind),
       
   217                           kindName(ownkind));
       
   218                 owntype = types.createErrorType(owntype);
       
   219             }
       
   220         }
       
   221         tree.type = owntype;
       
   222         return owntype;
       
   223     }
       
   224 
       
   225     /** Is given blank final variable assignable, i.e. in a scope where it
   210     /** Is given blank final variable assignable, i.e. in a scope where it
   226      *  may be assigned to even though it is final?
   211      *  may be assigned to even though it is final?
   227      *  @param v      The blank final variable.
   212      *  @param v      The blank final variable.
   228      *  @param env    The current environment.
   213      *  @param env    The current environment.
   229      */
   214      */
   438     }
   423     }
   439 
   424 
   440     /** Derived visitor method: attribute a type tree.
   425     /** Derived visitor method: attribute a type tree.
   441      */
   426      */
   442     Type attribType(JCTree tree, Env<AttrContext> env) {
   427     Type attribType(JCTree tree, Env<AttrContext> env) {
   443         Type result = attribType(tree, env, Type.noType);
   428         Type result = attribTree(tree, env, TYP, Type.noType);
   444         return result;
       
   445     }
       
   446 
       
   447     /** Derived visitor method: attribute a type tree.
       
   448      */
       
   449     Type attribType(JCTree tree, Env<AttrContext> env, Type pt) {
       
   450         Type result = attribTree(tree, env, TYP, pt);
       
   451         return result;
   429         return result;
   452     }
   430     }
   453 
   431 
   454     /** Derived visitor method: attribute a statement or definition tree.
   432     /** Derived visitor method: attribute a statement or definition tree.
   455      */
   433      */
  1398                 chk.checkRefTypes(tree.typeargs, typeargtypes);
  1376                 chk.checkRefTypes(tree.typeargs, typeargtypes);
  1399             }
  1377             }
  1400 
  1378 
  1401             // Check that value of resulting type is admissible in the
  1379             // Check that value of resulting type is admissible in the
  1402             // current context.  Also, capture the return type
  1380             // current context.  Also, capture the return type
  1403             result = checkReturn(tree, capture(restype), VAL, pkind, pt);
  1381             result = check(tree, capture(restype), VAL, pkind, pt);
  1404         }
  1382         }
  1405         chk.validate(tree.typeargs, localEnv);
  1383         chk.validate(tree.typeargs, localEnv);
  1406     }
  1384     }
  1407     //where
  1385     //where
  1408         /** Check that given application node appears as first statement
  1386         /** Check that given application node appears as first statement
  1473 //          System.out.println(clazz + " generated.");//DEBUG
  1451 //          System.out.println(clazz + " generated.");//DEBUG
  1474         }
  1452         }
  1475 
  1453 
  1476         // Attribute clazz expression and store
  1454         // Attribute clazz expression and store
  1477         // symbol + type back into the attributed tree.
  1455         // symbol + type back into the attributed tree.
  1478         Type clazztype = attribType(clazz, env);
  1456         Type clazztype = chk.checkClassType(
       
  1457             tree.clazz.pos(), attribType(clazz, env), true);
  1479         chk.validate(clazz, localEnv);
  1458         chk.validate(clazz, localEnv);
  1480         clazztype = chk.checkNewClassType(clazz.pos(), clazztype, true, pt);
       
  1481         if (tree.encl != null) {
  1459         if (tree.encl != null) {
  1482             // We have to work in this case to store
  1460             // We have to work in this case to store
  1483             // symbol + type back into the attributed tree.
  1461             // symbol + type back into the attributed tree.
  1484             tree.clazz.type = clazztype;
  1462             tree.clazz.type = clazztype;
  1485             TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1));
  1463             TreeInfo.setSymbol(clazzid, TreeInfo.symbol(clazzid1));
  1580                 //         e.<typeargs1>super(args)
  1558                 //         e.<typeargs1>super(args)
  1581                 //       }
  1559                 //       }
  1582                 //       ...
  1560                 //       ...
  1583                 //     }
  1561                 //     }
  1584                 if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC;
  1562                 if (Resolve.isStatic(env)) cdef.mods.flags |= STATIC;
  1585                 clazz = TreeInfo.isDiamond(tree) ?
  1563 
  1586                     make.Type(clazztype)
       
  1587                     : clazz;
       
  1588                 if (clazztype.tsym.isInterface()) {
  1564                 if (clazztype.tsym.isInterface()) {
  1589                     cdef.implementing = List.of(clazz);
  1565                     cdef.implementing = List.of(clazz);
  1590                 } else {
  1566                 } else {
  1591                     cdef.extending = clazz;
  1567                     cdef.extending = clazz;
  1592                 }
  1568                 }
  2569         List<Type> actuals = attribTypes(tree.arguments, env);
  2545         List<Type> actuals = attribTypes(tree.arguments, env);
  2570 
  2546 
  2571         if (clazztype.tag == CLASS) {
  2547         if (clazztype.tag == CLASS) {
  2572             List<Type> formals = clazztype.tsym.type.getTypeArguments();
  2548             List<Type> formals = clazztype.tsym.type.getTypeArguments();
  2573 
  2549 
  2574             if (actuals.length() == formals.length() || actuals.isEmpty()) {
  2550             if (actuals.length() == formals.length()) {
  2575                 List<Type> a = actuals;
  2551                 List<Type> a = actuals;
  2576                 List<Type> f = formals;
  2552                 List<Type> f = formals;
  2577                 while (a.nonEmpty()) {
  2553                 while (a.nonEmpty()) {
  2578                     a.head = a.head.withTypeVar(f.head);
  2554                     a.head = a.head.withTypeVar(f.head);
  2579                     a = a.tail;
  2555                     a = a.tail;
  2595                         if (site == null)
  2571                         if (site == null)
  2596                             site = types.erasure(clazzOuter);
  2572                             site = types.erasure(clazzOuter);
  2597                         clazzOuter = site;
  2573                         clazzOuter = site;
  2598                     }
  2574                     }
  2599                 }
  2575                 }
  2600                 if (actuals.nonEmpty()) {
  2576                 owntype = new ClassType(clazzOuter, actuals, clazztype.tsym);
  2601                     owntype = new ClassType(clazzOuter, actuals, clazztype.tsym);
       
  2602                 }
       
  2603                 else if (TreeInfo.isDiamond(tree)) {
       
  2604                     //a type apply with no explicit type arguments - diamond operator
       
  2605                     //the result type is a forall F where F's tvars are the type-variables
       
  2606                     //that will be inferred when F is checked against the expected type
       
  2607                     List<Type> ftvars = clazztype.tsym.type.getTypeArguments();
       
  2608                     List<Type> new_tvars = types.newInstances(ftvars);
       
  2609                     clazztype = new ClassType(clazzOuter, new_tvars, clazztype.tsym);
       
  2610                     owntype = new ForAll(new_tvars, clazztype);
       
  2611                 }
       
  2612             } else {
  2577             } else {
  2613                 if (formals.length() != 0) {
  2578                 if (formals.length() != 0) {
  2614                     log.error(tree.pos(), "wrong.number.type.args",
  2579                     log.error(tree.pos(), "wrong.number.type.args",
  2615                               Integer.toString(formals.length()));
  2580                               Integer.toString(formals.length()));
  2616                 } else {
  2581                 } else {