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

Print this page




 376         for (int i=1; ; i++) {
 377             Name flatname = names.
 378                 fromString("" + c.owner.enclClass().flatname +
 379                            syntheticNameChar + i +
 380                            c.name);
 381             if (compiled.get(flatname) == null) return flatname;
 382         }
 383     }
 384 
 385 /* *************************************************************************
 386  * Type Checking
 387  **************************************************************************/
 388 
 389     /** Check that a given type is assignable to a given proto-type.
 390      *  If it is, return the type, otherwise return errType.
 391      *  @param pos        Position to be used for error reporting.
 392      *  @param found      The type that was found.
 393      *  @param req        The type that was required.
 394      */
 395     Type checkType(DiagnosticPosition pos, Type found, Type req) {




 396         if (req.tag == ERROR)
 397             return req;
 398         if (found.tag == FORALL)
 399             return instantiatePoly(pos, (ForAll)found, req, convertWarner(pos, found, req));
 400         if (req.tag == NONE)
 401             return found;
 402         if (types.isAssignable(found, req, convertWarner(pos, found, req)))
 403             return found;
 404         if (found.tag <= DOUBLE && req.tag <= DOUBLE)
 405             return typeError(pos, diags.fragment("possible.loss.of.precision"), found, req);
 406         if (found.isSuperBound()) {
 407             log.error(pos, "assignment.from.super-bound", found);
 408             return types.createErrorType(found);
 409         }
 410         if (req.isExtendsBound()) {
 411             log.error(pos, "assignment.to.extends-bound", req);
 412             return types.createErrorType(found);
 413         }
 414         return typeError(pos, diags.fragment("incompatible.types"), found, req);
 415     }
 416 
 417     /** Instantiate polymorphic type to some prototype, unless
 418      *  prototype is `anyPoly' in which case polymorphic type
 419      *  is returned unchanged.
 420      */
 421     Type instantiatePoly(DiagnosticPosition pos, ForAll t, Type pt, Warner warn) throws Infer.NoInstanceException {
 422         if (pt == Infer.anyPoly && complexInference) {
 423             return t;
 424         } else if (pt == Infer.anyPoly || pt.tag == NONE) {
 425             Type newpt = t.qtype.tag <= VOID ? t.qtype : syms.objectType;
 426             return instantiatePoly(pos, t, newpt, warn);
 427         } else if (pt.tag == ERROR) {
 428             return pt;
 429         } else {
 430             try {
 431                 return infer.instantiateExpr(t, pt, warn);
 432             } catch (Infer.NoInstanceException ex) {
 433                 if (ex.isAmbiguous) {
 434                     JCDiagnostic d = ex.getDiagnostic();




 376         for (int i=1; ; i++) {
 377             Name flatname = names.
 378                 fromString("" + c.owner.enclClass().flatname +
 379                            syntheticNameChar + i +
 380                            c.name);
 381             if (compiled.get(flatname) == null) return flatname;
 382         }
 383     }
 384 
 385 /* *************************************************************************
 386  * Type Checking
 387  **************************************************************************/
 388 
 389     /** Check that a given type is assignable to a given proto-type.
 390      *  If it is, return the type, otherwise return errType.
 391      *  @param pos        Position to be used for error reporting.
 392      *  @param found      The type that was found.
 393      *  @param req        The type that was required.
 394      */
 395     Type checkType(DiagnosticPosition pos, Type found, Type req) {
 396         return checkType(pos, found, req, "incompatible.types");
 397     }
 398 
 399     Type checkType(DiagnosticPosition pos, Type found, Type req, String errKey) {
 400         if (req.tag == ERROR)
 401             return req;
 402         if (found.tag == FORALL)
 403             return instantiatePoly(pos, (ForAll)found, req, convertWarner(pos, found, req));
 404         if (req.tag == NONE)
 405             return found;
 406         if (types.isAssignable(found, req, convertWarner(pos, found, req)))
 407             return found;
 408         if (found.tag <= DOUBLE && req.tag <= DOUBLE)
 409             return typeError(pos, diags.fragment("possible.loss.of.precision"), found, req);
 410         if (found.isSuperBound()) {
 411             log.error(pos, "assignment.from.super-bound", found);
 412             return types.createErrorType(found);
 413         }
 414         if (req.isExtendsBound()) {
 415             log.error(pos, "assignment.to.extends-bound", req);
 416             return types.createErrorType(found);
 417         }
 418         return typeError(pos, diags.fragment(errKey), found, req);
 419     }
 420 
 421     /** Instantiate polymorphic type to some prototype, unless
 422      *  prototype is `anyPoly' in which case polymorphic type
 423      *  is returned unchanged.
 424      */
 425     Type instantiatePoly(DiagnosticPosition pos, ForAll t, Type pt, Warner warn) throws Infer.NoInstanceException {
 426         if (pt == Infer.anyPoly && complexInference) {
 427             return t;
 428         } else if (pt == Infer.anyPoly || pt.tag == NONE) {
 429             Type newpt = t.qtype.tag <= VOID ? t.qtype : syms.objectType;
 430             return instantiatePoly(pos, t, newpt, warn);
 431         } else if (pt.tag == ERROR) {
 432             return pt;
 433         } else {
 434             try {
 435                 return infer.instantiateExpr(t, pt, warn);
 436             } catch (Infer.NoInstanceException ex) {
 437                 if (ex.isAmbiguous) {
 438                     JCDiagnostic d = ex.getDiagnostic();