Search in sources :

Example 31 with ClassType

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

the class Attr method attribDiamond.

Type attribDiamond(Env<AttrContext> env, JCNewClass tree, Type clazztype, Pair<Scope, Scope> mapping, List<Type> argtypes, List<Type> typeargtypes) {
    if (clazztype.isErroneous() || clazztype.isInterface() || mapping == erroneousMapping) {
        // mapping, return the (possibly erroneous) type unchanged
        return clazztype;
    }
    // dup attribution environment and augment the set of inference variables
    Env<AttrContext> localEnv = env.dup(tree);
    localEnv.info.tvars = clazztype.tsym.type.getTypeArguments();
    // if the type of the instance creation expression is a class type
    // apply method resolution inference (JLS 15.12.2.7). The return type
    // of the resolved constructor will be a partially instantiated type
    ((ClassSymbol) clazztype.tsym).members_field = mapping.snd;
    Symbol constructor;
    try {
        constructor = rs.resolveDiamond(tree.pos(), localEnv, clazztype.tsym.type, argtypes, typeargtypes);
    } finally {
        ((ClassSymbol) clazztype.tsym).members_field = mapping.fst;
    }
    if (constructor.kind == MTH) {
        ClassType ct = new ClassType(clazztype.getEnclosingType(), clazztype.tsym.type.getTypeArguments(), clazztype.tsym);
        clazztype = checkMethod(ct, constructor, localEnv, tree.args, argtypes, typeargtypes, localEnv.info.varArgs).getReturnType();
    } else {
        clazztype = syms.errType;
    }
    if (clazztype.tag == FORALL && !pt.isErroneous()) {
        // bounds (JLS 15.12.2.8).
        try {
            clazztype = infer.instantiateExpr((ForAll) clazztype, pt.tag == NONE ? syms.objectType : pt, Warner.noWarnings);
        } catch (Infer.InferenceException ex) {
            // an error occurred while inferring uninstantiated type-variables
            log.error(tree.clazz.pos(), "cant.apply.diamond.1", diags.fragment("diamond", clazztype.tsym), ex.diagnostic);
        }
    }
    return chk.checkClassType(tree.clazz.pos(), clazztype, true);
}
Also used : 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) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ForAll(com.sun.tools.javac.code.Type.ForAll)

Example 32 with ClassType

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

the class Attr method checkId.

/**
 * Determine type of identifier or select expression and check that
 *  (1) the referenced symbol is not deprecated
 *  (2) the symbol's type is safe (@see checkSafe)
 *  (3) if symbol is a variable, check that its type and kind are
 *      compatible with the prototype and protokind.
 *  (4) if symbol is an instance field of a raw type,
 *      which is being assigned to, issue an unchecked warning if its
 *      type changes under erasure.
 *  (5) if symbol is an instance method of a raw type, issue an
 *      unchecked warning if its argument types change under erasure.
 *  If checks succeed:
 *    If symbol is a constant, return its constant type
 *    else if symbol is a method, return its result type
 *    otherwise return its type.
 *  Otherwise return errType.
 *
 *  @param tree       The syntax tree representing the identifier
 *  @param site       If this is a select, the type of the selected
 *                    expression, otherwise the type of the current class.
 *  @param sym        The symbol representing the identifier.
 *  @param env        The current environment.
 *  @param pkind      The set of expected kinds.
 *  @param pt         The expected type.
 */
Type checkId(JCTree tree, Type site, Symbol sym, Env<AttrContext> env, int pkind, Type pt, boolean useVarargs) {
    if (pt.isErroneous())
        return types.createErrorType(site);
    // The computed type of this identifier occurrence.
    Type owntype;
    switch(sym.kind) {
        case TYP:
            // For types, the computed type equals the symbol's type,
            // except for two situations:
            owntype = sym.type;
            if (owntype.tag == CLASS) {
                Type ownOuter = owntype.getEnclosingType();
                // We recover generic outer type later in visitTypeApply.
                if (owntype.tsym.type.getTypeArguments().nonEmpty()) {
                    owntype = types.erasure(owntype);
                } else // Tree<Point>.Visitor.
                if (ownOuter.tag == CLASS && site != ownOuter) {
                    Type normOuter = site;
                    if (normOuter.tag == CLASS)
                        normOuter = types.asEnclosingSuper(site, ownOuter.tsym);
                    if (// perhaps from an import
                    normOuter == null)
                        normOuter = types.erasure(ownOuter);
                    if (normOuter != ownOuter)
                        owntype = new ClassType(normOuter, List.<Type>nil(), owntype.tsym);
                }
            }
            break;
        case VAR:
            VarSymbol v = (VarSymbol) sym;
            // its type changes under erasure.
            if (allowGenerics && pkind == VAR && v.owner.kind == TYP && (v.flags() & STATIC) == 0 && (site.tag == CLASS || site.tag == TYPEVAR)) {
                Type s = types.asOuterSuper(site, v.owner);
                if (s != null && s.isRaw() && !types.isSameType(v.type, v.erasure(types))) {
                    chk.warnUnchecked(tree.pos(), "unchecked.assign.to.var", v, s);
                }
            }
            // The computed type of a variable is the type of the
            // variable symbol, taken as a member of the site type.
            owntype = (sym.owner.kind == TYP && sym.name != names._this && sym.name != names._super) ? types.memberType(site, sym) : sym.type;
            if (env.info.tvars.nonEmpty()) {
                Type owntype1 = new ForAll(env.info.tvars, owntype);
                for (List<Type> l = env.info.tvars; l.nonEmpty(); l = l.tail) if (!owntype.contains(l.head)) {
                    log.error(tree.pos(), "undetermined.type", owntype1);
                    owntype1 = types.createErrorType(owntype1);
                }
                owntype = owntype1;
            }
            // computed type.
            if (v.getConstValue() != null && isStaticReference(tree))
                owntype = owntype.constType(v.getConstValue());
            if (pkind == VAL) {
                // capture "names as expressions"
                owntype = capture(owntype);
            }
            break;
        case MTH:
            {
                JCMethodInvocation app = (JCMethodInvocation) env.tree;
                owntype = checkMethod(site, sym, env, app.args, pt.getParameterTypes(), pt.getTypeArguments(), env.info.varArgs);
                break;
            }
        case PCK:
        case ERR:
            owntype = sym.type;
            break;
        default:
            throw new AssertionError("unexpected kind: " + sym.kind + " in tree " + tree);
    }
    if (sym.name != names.init) {
        chk.checkDeprecated(tree.pos(), env.info.scope.owner, sym);
        chk.checkSunAPI(tree.pos(), sym);
    }
    // kind are compatible with the prototype and protokind.
    return check(tree, owntype, sym.kind, pkind, pt);
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) 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) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ForAll(com.sun.tools.javac.code.Type.ForAll)

Example 33 with ClassType

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

the class ParameterizedTypeImpl method parameterizedTypeToString.

static String parameterizedTypeToString(DocEnv env, ClassType cl, boolean full) {
    if (env.legacyDoclet) {
        return TypeMaker.getTypeName(cl, full);
    }
    StringBuilder s = new StringBuilder();
    if (cl.getEnclosingType().tag != CLASS) {
        // if not an inner class...
        s.append(TypeMaker.getTypeName(cl, full));
    } else {
        ClassType encl = (ClassType) cl.getEnclosingType();
        s.append(parameterizedTypeToString(env, encl, full)).append('.').append(cl.tsym.name.toString());
    }
    s.append(TypeMaker.typeArgumentsString(env, cl, full));
    return s.toString();
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType)

Example 34 with ClassType

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

the class TypeMaker method typeArgumentsString.

/**
 * Return the actual type arguments of a parameterized type as an
 * angle-bracketed string.  Class name are qualified if "full" is true.
 * Return "" if there are no type arguments or we're hiding generics.
 */
static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) {
    if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) {
        return "";
    }
    StringBuilder s = new StringBuilder();
    for (Type t : cl.getTypeArguments()) {
        s.append(s.length() == 0 ? "<" : ", ");
        s.append(getTypeString(env, t, full));
    }
    s.append(">");
    return s.toString();
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) Type(com.sun.tools.javac.code.Type)

Aggregations

ClassType (com.sun.tools.javac.code.Type.ClassType)34 Type (com.sun.tools.javac.code.Type)21 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)10 ArrayType (com.sun.tools.javac.code.Type.ArrayType)10 Symbol (com.sun.tools.javac.code.Symbol)9 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)9 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)9 WildcardType (com.sun.tools.javac.code.Type.WildcardType)9 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)8 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)7 MethodType (com.sun.tools.javac.code.Type.MethodType)7 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)7 ArrayList (java.util.ArrayList)5 ClassTree (com.sun.source.tree.ClassTree)4 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)4 Violation (com.google.errorprone.bugpatterns.threadsafety.ThreadSafety.Violation)3 MethodTree (com.sun.source.tree.MethodTree)3 NewClassTree (com.sun.source.tree.NewClassTree)3 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)3 AttrContext (com.sun.tools.javac.comp.AttrContext)3