Search in sources :

Example 56 with Type

use of com.sun.tools.javac.code.Type in project lombok by rzwitserloot.

the class JavacResolution method typeToJCTree.

private static JCExpression typeToJCTree(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
    int dims = 0;
    Type type0 = type;
    while (type0 instanceof ArrayType) {
        dims++;
        type0 = ((ArrayType) type0).elemtype;
    }
    JCExpression result = typeToJCTree0(type0, ast, allowCompound, allowVoid);
    while (dims > 0) {
        result = ast.getTreeMaker().TypeArray(result);
        dims--;
    }
    return result;
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) ClassType(com.sun.tools.javac.code.Type.ClassType) CapturedType(com.sun.tools.javac.code.Type.CapturedType) ArrayType(com.sun.tools.javac.code.Type.ArrayType) WildcardType(com.sun.tools.javac.code.Type.WildcardType) Type(com.sun.tools.javac.code.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression)

Example 57 with Type

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

the class Attr method attribLazyConstantValue.

/**
     * Attribute a "lazy constant value".
     *  @param env         The env for the const value
     *  @param initializer The initializer for the const value
     *  @param type        The expected type, or null
     *  @see VarSymbol#setlazyConstValue
     */
public Object attribLazyConstantValue(Env<AttrContext> env, JCTree.JCExpression initializer, Type type) {
    // in case no lint value has been set up for this env, scan up
    // env stack looking for smallest enclosing env for which it is set.
    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(env.info.enclVar.attributes_field, env.info.enclVar.flags());
    Lint prevLint = chk.setLint(env.info.lint);
    JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
    try {
        Type itype = attribExpr(initializer, env, type);
        if (itype.constValue() != null)
            return coerce(itype, type).constValue();
        else
            return null;
    } finally {
        env.info.lint = prevLint;
        log.useSource(prevSource);
    }
}
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) Lint(com.sun.tools.javac.code.Lint)

Example 58 with Type

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

the class Attr method visitUnary.

public void visitUnary(JCUnary tree) {
    // Attribute arguments.
    Type argtype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) ? attribTree(tree.arg, env, VAR, Type.noType) : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env));
    // Find operator.
    Symbol operator = tree.operator = rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype);
    Type owntype = types.createErrorType(tree.type);
    if (operator.kind == MTH && !argtype.isErroneous()) {
        owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) ? tree.arg.type : operator.type.getReturnType();
        int opc = ((OperatorSymbol) operator).opcode;
        // If the argument is constant, fold it.
        if (argtype.constValue() != null) {
            Type ctype = cfolder.fold1(opc, argtype);
            if (ctype != null) {
                owntype = cfolder.coerce(ctype, owntype);
                // operands are constant identifiers.
                if (tree.arg.type.tsym == syms.stringType.tsym) {
                    tree.arg.type = syms.stringType;
                }
            }
        }
    }
    result = check(tree, owntype, VAL, 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) OperatorSymbol(com.sun.tools.javac.code.Symbol.OperatorSymbol) 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) Lint(com.sun.tools.javac.code.Lint)

Example 59 with Type

use of com.sun.tools.javac.code.Type 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 60 with Type

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

the class Attr method visitSwitch.

public void visitSwitch(JCSwitch tree) {
    Type seltype = attribExpr(tree.selector, env);
    Env<AttrContext> switchEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
    boolean enumSwitch = allowEnums && (seltype.tsym.flags() & Flags.ENUM) != 0;
    boolean stringSwitch = false;
    if (types.isSameType(seltype, syms.stringType)) {
        if (allowStringsInSwitch) {
            stringSwitch = true;
        } else {
            log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName);
        }
    }
    if (!enumSwitch && !stringSwitch)
        seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
    // Attribute all cases and
    // check that there are no duplicate case labels or default clauses.
    // The set of case labels.
    Set<Object> labels = new HashSet<Object>();
    // Is there a default label?
    boolean hasDefault = false;
    for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
        JCCase c = l.head;
        Env<AttrContext> caseEnv = switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
        if (c.pat != null) {
            if (enumSwitch) {
                Symbol sym = enumConstant(c.pat, seltype);
                if (sym == null) {
                    log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum");
                } else if (!labels.add(sym)) {
                    log.error(c.pos(), "duplicate.case.label");
                }
            } else {
                Type pattype = attribExpr(c.pat, switchEnv, seltype);
                if (pattype.tag != ERROR) {
                    if (pattype.constValue() == null) {
                        log.error(c.pat.pos(), (stringSwitch ? "string.const.req" : "const.expr.req"));
                    } else if (labels.contains(pattype.constValue())) {
                        log.error(c.pos(), "duplicate.case.label");
                    } else {
                        labels.add(pattype.constValue());
                    }
                }
            }
        } else if (hasDefault) {
            log.error(c.pos(), "duplicate.default.label");
        } else {
            hasDefault = true;
        }
        attribStats(c.stats, caseEnv);
        caseEnv.info.scope.leave();
        addVars(c.stats, switchEnv.info.scope);
    }
    switchEnv.info.scope.leave();
    result = null;
}
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) 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) JavaFileObject(javax.tools.JavaFileObject) HashSet(java.util.HashSet) JCCase(com.sun.tools.javac.tree.JCTree.JCCase)

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