Search in sources :

Example 81 with Type

use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.

the class Types method resultSubtype.

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Return-Type-Substitutable">
/**
     * Does t have a result that is a subtype of the result type of s,
     * suitable for covariant returns?  It is assumed that both types
     * are (possibly polymorphic) method types.  Monomorphic method
     * types are handled in the obvious way.  Polymorphic method types
     * require renaming all type variables of one to corresponding
     * type variables in the other, where correspondence is by
     * position in the type parameter list. */
public boolean resultSubtype(Type t, Type s, Warner warner) {
    List<Type> tvars = t.getTypeArguments();
    List<Type> svars = s.getTypeArguments();
    Type tres = t.getReturnType();
    Type sres = subst(s.getReturnType(), svars, tvars);
    return covariantReturnType(tres, sres, warner);
}
Also used : Type(com.sun.tools.javac.code.Type)

Example 82 with Type

use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.

the class Attr method visitSelect.

public void visitSelect(JCFieldAccess tree) {
    // Determine the expected kind of the qualifier expression.
    int skind = 0;
    if (tree.name == names._this || tree.name == names._super || tree.name == names._class) {
        skind = TYP;
    } else {
        if ((pkind & PCK) != 0)
            skind = skind | PCK;
        if ((pkind & TYP) != 0)
            skind = skind | TYP | PCK;
        if ((pkind & (VAL | MTH)) != 0)
            skind = skind | VAL | TYP;
    }
    // Attribute the qualifier expression, and determine its symbol (if any).
    Type site = attribTree(tree.selected, env, skind, Infer.anyPoly);
    if ((pkind & (PCK | TYP)) == 0)
        // Capture field access
        site = capture(site);
    // don't allow T.class T[].class, etc
    if (skind == TYP) {
        Type elt = site;
        while (elt.tag == ARRAY) elt = ((ArrayType) elt).elemtype;
        if (elt.tag == TYPEVAR) {
            log.error(tree.pos(), "type.var.cant.be.deref");
            result = types.createErrorType(tree.type);
            return;
        }
    }
    // If qualifier symbol is a type or `super', assert `selectSuper'
    // for the selection. This is relevant for determining whether
    // protected symbols are accessible.
    Symbol sitesym = TreeInfo.symbol(tree.selected);
    boolean selectSuperPrev = env.info.selectSuper;
    env.info.selectSuper = sitesym != null && sitesym.name == names._super;
    // they can be added later (in Attr.checkId and Infer.instantiateMethod).
    if (tree.selected.type.tag == FORALL) {
        ForAll pstype = (ForAll) tree.selected.type;
        env.info.tvars = pstype.tvars;
        site = tree.selected.type = pstype.qtype;
    }
    // Determine the symbol represented by the selection.
    env.info.varArgs = false;
    Symbol sym = selectSym(tree, sitesym, site, env, pt, pkind);
    if (sym.exists() && !isType(sym) && (pkind & (PCK | TYP)) != 0) {
        site = capture(site);
        sym = selectSym(tree, sitesym, site, env, pt, pkind);
    }
    boolean varArgs = env.info.varArgs;
    tree.sym = sym;
    if (site.tag == TYPEVAR && !isType(sym) && sym.kind != ERR) {
        while (site.tag == TYPEVAR) site = site.getUpperBound();
        site = capture(site);
    }
    // If that symbol is a variable, ...
    if (sym.kind == VAR) {
        VarSymbol v = (VarSymbol) sym;
        // ..., evaluate its initializer, if it has one, and check for
        // illegal forward reference.
        checkInit(tree, env, v, true);
        // that the variable is assignable in the current environment.
        if (pkind == VAR)
            checkAssignable(tree.pos(), v, tree.selected, env);
    }
    if (sitesym != null && sitesym.kind == VAR && ((VarSymbol) sitesym).isResourceVariable() && sym.kind == MTH && sym.name.equals(names.close) && sym.overrides(syms.autoCloseableClose, sitesym.type.tsym, types, true) && env.info.lint.isEnabled(LintCategory.TRY)) {
        log.warning(LintCategory.TRY, tree, "try.explicit.close.call");
    }
    // Disallow selecting a type from an expression
    if (isType(sym) && (sitesym == null || (sitesym.kind & (TYP | PCK)) == 0)) {
        tree.type = check(tree.selected, pt, sitesym == null ? VAL : sitesym.kind, TYP | PCK, pt);
    }
    if (isType(sitesym)) {
        if (sym.name == names._this) {
            // C.this' does not appear in a call to a super(...)
            if (env.info.isSelfCall && site.tsym == env.enclClass.sym) {
                chk.earlyRefError(tree.pos(), sym);
            }
        } else {
            // Check if type-qualified fields or methods are static (JLS)
            if ((sym.flags() & STATIC) == 0 && sym.name != names._super && (sym.kind == VAR || sym.kind == MTH)) {
                rs.access(rs.new StaticError(sym), tree.pos(), site, sym.name, true);
            }
        }
    } else if (sym.kind != ERR && (sym.flags() & STATIC) != 0 && sym.name != names._class) {
        // If the qualified item is not a type and the selected item is static, report
        // a warning. Make allowance for the class of an array type e.g. Object[].class)
        chk.warnStatic(tree, "static.not.qualified.by.type", Kinds.kindName(sym.kind), sym.owner);
    }
    // If we are selecting an instance member via a `super', ...
    if (env.info.selectSuper && (sym.flags() & STATIC) == 0) {
        // Check that super-qualified symbols are not abstract (JLS)
        rs.checkNonAbstract(tree.pos(), sym);
        if (site.isRaw()) {
            // Determine argument types for site.
            Type site1 = types.asSuper(env.enclClass.sym.type, site.tsym);
            if (site1 != null)
                site = site1;
        }
    }
    // Ceylon: error if we try to select an interface static in < 1.8
    if (sym != null && sym.isStatic() && (sym.kind & Kinds.MTH) != 0 && sitesym != null && sitesym.isInterface()) {
        chk.checkStaticInterfaceMethodCall(tree);
    }
    env.info.selectSuper = selectSuperPrev;
    result = checkId(tree, site, sym, env, pkind, pt, varArgs);
    env.info.tvars = List.nil();
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) OperatorSymbol(com.sun.tools.javac.code.Symbol.OperatorSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ForAll(com.sun.tools.javac.code.Type.ForAll) Lint(com.sun.tools.javac.code.Lint)

Example 83 with Type

use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.

the class Attr method checkAutoCloseable.

void checkAutoCloseable(DiagnosticPosition pos, Env<AttrContext> env, Type resource) {
    if (!resource.isErroneous() && types.asSuper(resource, syms.autoCloseableType.tsym) != null) {
        Symbol close = syms.noSymbol;
        boolean prevDeferDiags = log.deferDiagnostics;
        Queue<JCDiagnostic> prevDeferredDiags = log.deferredDiagnostics;
        try {
            log.deferDiagnostics = true;
            log.deferredDiagnostics = ListBuffer.lb();
            close = rs.resolveQualifiedMethod(pos, env, resource, names.close, List.<Type>nil(), List.<Type>nil());
        } finally {
            log.deferDiagnostics = prevDeferDiags;
            log.deferredDiagnostics = prevDeferredDiags;
        }
        if (close.kind == MTH && close.overrides(syms.autoCloseableClose, resource.tsym, types, true) && chk.isHandled(syms.interruptedExceptionType, types.memberType(resource, close).getThrownTypes()) && env.info.lint.isEnabled(LintCategory.TRY)) {
            log.warning(LintCategory.TRY, pos, "try.resource.throws.interrupted.exc", resource);
        }
    }
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) JCDiagnostic(com.sun.tools.javac.util.JCDiagnostic) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) Symbol(com.sun.tools.javac.code.Symbol) PackageSymbol(com.sun.tools.javac.code.Symbol.PackageSymbol) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) OperatorSymbol(com.sun.tools.javac.code.Symbol.OperatorSymbol)

Example 84 with Type

use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.

the class Attr method attribClass.

/** Attribute class definition associated with given class symbol.
     *  @param c   The class symbol whose definition will be attributed.
     */
void attribClass(ClassSymbol c) throws CompletionFailure {
    if (c.type.tag == ERROR)
        return;
    // Check for cycles in the inheritance graph, which can arise from
    // ill-formed class files.
    chk.checkNonCyclic(null, c.type);
    Type st = types.supertype(c.type);
    if ((c.flags_field & Flags.COMPOUND) == 0) {
        // First, attribute superclass.
        if (st.tag == CLASS)
            attribClass((ClassSymbol) st.tsym);
        // Next attribute owner, if it is a class.
        if (c.owner.kind == TYP && c.owner.type.tag == CLASS)
            attribClass((ClassSymbol) c.owner);
    }
    // UNATTRIBUTED.
    if ((c.flags_field & UNATTRIBUTED) != 0) {
        c.flags_field &= ~UNATTRIBUTED;
        // Get environment current at the point of class definition.
        Env<AttrContext> env = enter.typeEnvs.get(c);
        // The info.lint field in the envs stored in enter.typeEnvs is deliberately uninitialized,
        // because the annotations were not available at the time the env was created. Therefore,
        // we look up the environment chain for the first enclosing environment for which the
        // lint value is set. Typically, this is the parent env, but might be further if there
        // are any envs created as a result of TypeParameter nodes.
        Env<AttrContext> lintEnv = env;
        while (lintEnv.info.lint == null) lintEnv = lintEnv.next;
        // Having found the enclosing lint value, we can initialize the lint value for this class
        env.info.lint = lintEnv.info.lint.augment(c.attributes_field, c.flags());
        Lint prevLint = chk.setLint(env.info.lint);
        JavaFileObject prev = log.useSource(c.sourcefile);
        try {
            // java.lang.Enum may not be subclassed by a non-enum
            if (st.tsym == syms.enumSym && ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0))
                log.error(env.tree.pos(), "enum.no.subclassing");
            // Enums may not be extended by source-level classes
            if (st.tsym != null && ((st.tsym.flags_field & Flags.ENUM) != 0) && ((c.flags_field & (Flags.ENUM | Flags.COMPOUND)) == 0) && !target.compilerBootstrap(c)) {
                log.error(env.tree.pos(), "enum.types.not.extensible");
            }
            attribClassBody(env, c);
            chk.checkDeprecatedAnnotation(env.tree.pos(), c);
        } finally {
            log.useSource(prev);
            chk.setLint(prevLint);
        }
    }
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) Lint(com.sun.tools.javac.code.Lint)

Example 85 with Type

use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.

the class Attr method visitTypeArray.

public void visitTypeArray(JCArrayTypeTree tree) {
    Type etype = attribType(tree.elemtype, env);
    Type type = new ArrayType(etype, syms.arrayClass);
    result = check(tree, type, TYP, pkind, pt);
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) ClassType(com.sun.tools.javac.code.Type.ClassType) MethodType(com.sun.tools.javac.code.Type.MethodType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) ArrayType(com.sun.tools.javac.code.Type.ArrayType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType)

Aggregations

Type (com.sun.tools.javac.code.Type)254 Symbol (com.sun.tools.javac.code.Symbol)64 ClassType (com.sun.tools.javac.code.Type.ClassType)55 ArrayType (com.sun.tools.javac.code.Type.ArrayType)47 MethodType (com.sun.tools.javac.code.Type.MethodType)42 WildcardType (com.sun.tools.javac.code.Type.WildcardType)42 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)36 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)33 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)32 JCTree (com.sun.tools.javac.tree.JCTree)27 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)24 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)23 Types (com.sun.tools.javac.code.Types)22 ExpressionTree (com.sun.source.tree.ExpressionTree)21 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)17 ArrayList (java.util.ArrayList)17 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)16 Tree (com.sun.source.tree.Tree)14 Name (com.sun.tools.javac.util.Name)14 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)13