Search in sources :

Example 6 with ClassType

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

the class T6889255 method getExpectedName.

String getExpectedName(VarSymbol v, int i) {
    // synthetic method
    if (((v.owner.owner.flags() & Flags.ENUM) != 0) && v.owner.name.toString().equals("valueOf"))
        return "name";
    // -- no Code attribute for the LocalVariableTable attribute
    if ((v.owner.owner.flags() & Flags.INTERFACE) != 0)
        return "arg" + (i - 1);
    // -- no Code attribute for the LocalVariableTable attribute
    if ((v.owner.flags() & Flags.ABSTRACT) != 0)
        return "arg" + (i - 1);
    // bridge methods use xN
    if ((v.owner.flags() & Flags.BRIDGE) != 0)
        return "x" + (i - 1);
    // The rest of this method assumes the local conventions in the test program
    Type t = v.type;
    String s;
    if (t.tag == TypeTags.CLASS)
        s = ((ClassType) t).tsym.name.toString();
    else
        s = t.toString();
    return String.valueOf(Character.toLowerCase(s.charAt(0))) + i;
}
Also used : ClassType(com.sun.tools.javac.code.Type.ClassType) Type(com.sun.tools.javac.code.Type) ClassType(com.sun.tools.javac.code.Type.ClassType)

Example 7 with ClassType

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

the class Attr method visitTypeUnion.

public void visitTypeUnion(JCTypeUnion tree) {
    ListBuffer<Type> multicatchTypes = ListBuffer.lb();
    // lazy, only if needed
    ListBuffer<Type> all_multicatchTypes = null;
    for (JCExpression typeTree : tree.alternatives) {
        Type ctype = attribType(typeTree, env);
        ctype = chk.checkType(typeTree.pos(), chk.checkClassType(typeTree.pos(), ctype), syms.throwableType);
        if (!ctype.isErroneous()) {
            // unrelated w.r.t. subtyping
            if (chk.intersects(ctype, multicatchTypes.toList())) {
                for (Type t : multicatchTypes) {
                    boolean sub = types.isSubtype(ctype, t);
                    boolean sup = types.isSubtype(t, ctype);
                    if (sub || sup) {
                        // assume 'a' <: 'b'
                        Type a = sub ? ctype : t;
                        Type b = sub ? t : ctype;
                        log.error(typeTree.pos(), "multicatch.types.must.be.disjoint", a, b);
                    }
                }
            }
            multicatchTypes.append(ctype);
            if (all_multicatchTypes != null)
                all_multicatchTypes.append(ctype);
        } else {
            if (all_multicatchTypes == null) {
                all_multicatchTypes = ListBuffer.lb();
                all_multicatchTypes.appendList(multicatchTypes);
            }
            all_multicatchTypes.append(ctype);
        }
    }
    Type t = check(tree, types.lub(multicatchTypes.toList()), TYP, pkind, pt);
    if (t.tag == CLASS) {
        List<Type> alternatives = ((all_multicatchTypes == null) ? multicatchTypes : all_multicatchTypes).toList();
        t = new UnionClassType((ClassType) t, alternatives);
    }
    tree.type = result = t;
}
Also used : UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) 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) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType)

Example 8 with ClassType

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

the class Attr method visitApply.

/**
 * Visitor method for method invocations.
 *  NOTE: The method part of an application will have in its type field
 *        the return type of the method, not the method's type itself!
 */
public void visitApply(JCMethodInvocation tree) {
    // The local environment of a method application is
    // a new environment nested in the current one.
    Env<AttrContext> localEnv = env.dup(tree, env.info.dup());
    // The types of the actual method arguments.
    List<Type> argtypes;
    // The types of the actual method type arguments.
    List<Type> typeargtypes = null;
    Name methName = TreeInfo.name(tree.meth);
    boolean isConstructorCall = methName == names._this || methName == names._super;
    if (isConstructorCall) {
        // Check that this is the first statement in a constructor.
        if (checkFirstConstructorStat(tree, env)) {
            // Record the fact
            // that this is a constructor call (using isSelfCall).
            localEnv.info.isSelfCall = true;
            // Attribute arguments, yielding list of argument types.
            argtypes = attribArgs(tree.args, localEnv);
            typeargtypes = attribTypes(tree.typeargs, localEnv);
            // Variable `site' points to the class in which the called
            // constructor is defined.
            Type site = env.enclClass.sym.type;
            if (methName == names._super) {
                if (site == syms.objectType) {
                    log.error(tree.meth.pos(), "no.superclass", site);
                    site = types.createErrorType(syms.objectType);
                } else {
                    site = types.supertype(site);
                }
            }
            if (site.tag == CLASS) {
                Type encl = site.getEnclosingType();
                while (encl != null && encl.tag == TYPEVAR) encl = encl.getUpperBound();
                if (encl.tag == CLASS) {
                    if (tree.meth.getTag() == JCTree.SELECT) {
                        JCTree qualifier = ((JCFieldAccess) tree.meth).selected;
                        // We are seeing a prefixed call, of the form
                        // <expr>.super(...).
                        // Check that the prefix expression conforms
                        // to the outer instance type of the class.
                        chk.checkRefType(qualifier.pos(), attribExpr(qualifier, localEnv, encl));
                    } else if (methName == names._super) {
                        // qualifier omitted; check for existence
                        // of an appropriate implicit qualifier.
                        rs.resolveImplicitThis(tree.meth.pos(), localEnv, site, true);
                    }
                } else if (tree.meth.getTag() == JCTree.SELECT) {
                    log.error(tree.meth.pos(), "illegal.qual.not.icls", site.tsym);
                }
                // prefix the implicit String and int parameters
                if (site.tsym == syms.enumSym && allowEnums)
                    argtypes = argtypes.prepend(syms.intType).prepend(syms.stringType);
                // Resolve the called constructor under the assumption
                // that we are referring to a superclass instance of the
                // current instance (JLS ???).
                boolean selectSuperPrev = localEnv.info.selectSuper;
                localEnv.info.selectSuper = true;
                localEnv.info.varArgs = false;
                Symbol sym = rs.resolveConstructor(tree.meth.pos(), localEnv, site, argtypes, typeargtypes);
                localEnv.info.selectSuper = selectSuperPrev;
                // Set method symbol to resolved constructor...
                TreeInfo.setSymbol(tree.meth, sym);
                // ...and check that it is legal in the current context.
                // (this will also set the tree's type)
                Type mpt = newMethTemplate(argtypes, typeargtypes);
                checkId(tree.meth, site, sym, localEnv, MTH, mpt, tree.varargsElement != null);
            }
        // Otherwise, `site' is an error type and we do nothing
        }
        result = tree.type = syms.voidType;
    } else {
        // Otherwise, we are seeing a regular method call.
        // Attribute the arguments, yielding list of argument types, ...
        argtypes = attribArgs(tree.args, localEnv);
        typeargtypes = attribAnyTypes(tree.typeargs, localEnv);
        // ... and attribute the method using as a prototype a methodtype
        // whose formal argument types is exactly the list of actual
        // arguments (this will also set the method symbol).
        Type mpt = newMethTemplate(argtypes, typeargtypes);
        localEnv.info.varArgs = false;
        Type mtype = attribExpr(tree.meth, localEnv, mpt);
        if (localEnv.info.varArgs)
            Assert.check(mtype.isErroneous() || tree.varargsElement != null);
        // Compute the result type.
        Type restype = mtype.getReturnType();
        if (restype.tag == WILDCARD)
            throw new AssertionError(mtype);
        // the same as static type of the array being cloned
        if (tree.meth.getTag() == JCTree.SELECT && allowCovariantReturns && methName == names.clone && types.isArray(((JCFieldAccess) tree.meth).selected.type))
            restype = ((JCFieldAccess) tree.meth).selected.type;
        // as a special case, x.getClass() has type Class<? extends |X|>
        if (allowGenerics && methName == names.getClass && tree.args.isEmpty()) {
            Type qualifier = (tree.meth.getTag() == JCTree.SELECT) ? ((JCFieldAccess) tree.meth).selected.type : env.enclClass.sym.type;
            restype = new ClassType(restype.getEnclosingType(), List.<Type>of(new WildcardType(types.erasure(qualifier), BoundKind.EXTENDS, syms.boundClass)), restype.tsym);
        }
        chk.checkRefTypes(tree.typeargs, typeargtypes);
        // Check that value of resulting type is admissible in the
        // current context.  Also, capture the return type
        result = check(tree, capture(restype), VAL, pkind, pt);
    }
    chk.validate(tree.typeargs, localEnv);
}
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) WildcardType(com.sun.tools.javac.code.Type.WildcardType) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) 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) JCTree(com.sun.tools.javac.tree.JCTree) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Kinds.kindName(com.sun.tools.javac.code.Kinds.kindName) Name(com.sun.tools.javac.util.Name)

Example 9 with ClassType

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

the class Attr method getSyntheticScopeMapping.

/**
 * Creates a synthetic scope containing fake generic constructors.
 *  Assuming that the original scope contains a constructor of the kind:
 *  Foo(X x, Y y), where X,Y are class type-variables declared in Foo,
 *  the synthetic scope is added a generic constructor of the kind:
 *  <X,Y>Foo<X,Y>(X x, Y y). This is crucial in order to enable diamond
 *  inference. The inferred return type of the synthetic constructor IS
 *  the inferred type for the diamond operator.
 */
private Pair<Scope, Scope> getSyntheticScopeMapping(Type ctype) {
    if (ctype.tag != CLASS) {
        return erroneousMapping;
    }
    Pair<Scope, Scope> mapping = new Pair<Scope, Scope>(ctype.tsym.members(), new Scope(ctype.tsym));
    // declared, and insert it into the new scope.
    for (Scope.Entry e = mapping.fst.lookup(names.init); e.scope != null; e = e.next()) {
        Type synthRestype = new ClassType(ctype.getEnclosingType(), ctype.tsym.type.getTypeArguments(), ctype.tsym);
        MethodSymbol synhConstr = new MethodSymbol(e.sym.flags(), names.init, types.createMethodTypeWithReturn(e.sym.type, synthRestype), e.sym.owner);
        mapping.snd.enter(synhConstr);
    }
    return mapping;
}
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) Scope(com.sun.tools.javac.code.Scope) DelegatedScope(com.sun.tools.javac.code.Scope.DelegatedScope) DynamicMethodSymbol(com.sun.tools.javac.code.Symbol.DynamicMethodSymbol) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Pair(com.sun.tools.javac.util.Pair)

Example 10 with ClassType

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

the class Attr method visitTypeApply.

/**
 * Visitor method for parameterized types.
 *  Bound checking is left until later, since types are attributed
 *  before supertype structure is completely known
 */
public void visitTypeApply(JCTypeApply tree) {
    Type owntype = types.createErrorType(tree.type);
    // Attribute functor part of application and make sure it's a class.
    Type clazztype = chk.checkClassType(tree.clazz.pos(), attribType(tree.clazz, env));
    // Attribute type parameters
    List<Type> actuals = attribTypes(tree.arguments, env);
    if (clazztype.tag == CLASS) {
        List<Type> formals = clazztype.tsym.type.getTypeArguments();
        if (actuals.length() == formals.length() || actuals.length() == 0) {
            List<Type> a = actuals;
            List<Type> f = formals;
            while (a.nonEmpty()) {
                a.head = a.head.withTypeVar(f.head);
                a = a.tail;
                f = f.tail;
            }
            // Compute the proper generic outer
            Type clazzOuter = clazztype.getEnclosingType();
            if (clazzOuter.tag == CLASS) {
                Type site;
                JCExpression clazz = TreeInfo.typeIn(tree.clazz);
                if (clazz.getTag() == JCTree.IDENT) {
                    site = env.enclClass.sym.type;
                } else if (clazz.getTag() == JCTree.SELECT) {
                    site = ((JCFieldAccess) clazz).selected.type;
                } else
                    throw new AssertionError("" + tree);
                if (clazzOuter.tag == CLASS && site != clazzOuter) {
                    if (site.tag == CLASS)
                        site = types.asOuterSuper(site, clazzOuter.tsym);
                    if (site == null)
                        site = types.erasure(clazzOuter);
                    clazzOuter = site;
                }
            }
            owntype = new ClassType(clazzOuter, actuals, clazztype.tsym);
        } else {
            if (formals.length() != 0) {
                log.error(tree.pos(), "wrong.number.type.args", Integer.toString(formals.length()));
            } else {
                log.error(tree.pos(), "type.doesnt.take.params", clazztype.tsym);
            }
            owntype = types.createErrorType(tree.type);
        }
    }
    result = check(tree, owntype, TYP, pkind, pt);
}
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) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ClassType(com.sun.tools.javac.code.Type.ClassType) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType)

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