Search in sources :

Example 46 with Symbol

use of com.sun.tools.javac.code.Symbol in project error-prone by google.

the class RefasterScanner method visitClass.

@Override
public Void visitClass(ClassTree node, Context context) {
    Symbol sym = ASTHelpers.getSymbol(node);
    if (sym == null || !sym.getQualifiedName().contentEquals(rule().qualifiedTemplateClass())) {
        ListBuffer<JCStatement> statements = new ListBuffer<>();
        for (Tree tree : node.getMembers()) {
            if (tree instanceof JCStatement) {
                statements.append((JCStatement) tree);
            } else {
                tree.accept(this, context);
            }
        }
        scan(TreeMaker.instance(context).Block(0, statements.toList()), context);
    }
    return null;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) ListBuffer(com.sun.tools.javac.util.ListBuffer) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) IfTree(com.sun.source.tree.IfTree) JCTree(com.sun.tools.javac.tree.JCTree) DoWhileLoopTree(com.sun.source.tree.DoWhileLoopTree) SynchronizedTree(com.sun.source.tree.SynchronizedTree) WhileLoopTree(com.sun.source.tree.WhileLoopTree) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 47 with Symbol

use of com.sun.tools.javac.code.Symbol in project error-prone by google.

the class UFreeIdent method defaultAction.

@Override
protected Choice<Unifier> defaultAction(Tree target, final Unifier unifier) {
    if (target instanceof JCExpression) {
        JCExpression expression = (JCExpression) target;
        JCExpression currentBinding = unifier.getBinding(key());
        // Check that the expression does not reference any template-local variables.
        boolean isGood = trueOrNull(new TreeScanner<Boolean, Void>() {

            @Override
            public Boolean reduce(@Nullable Boolean left, @Nullable Boolean right) {
                return trueOrNull(left) && trueOrNull(right);
            }

            @Override
            public Boolean visitIdentifier(IdentifierTree ident, Void v) {
                Symbol identSym = ASTHelpers.getSymbol(ident);
                for (ULocalVarIdent.Key key : Iterables.filter(unifier.getBindings().keySet(), ULocalVarIdent.Key.class)) {
                    if (identSym == unifier.getBinding(key).getSymbol()) {
                        return false;
                    }
                }
                return true;
            }
        }.scan(expression, null));
        if (!isGood) {
            return Choice.none();
        } else if (currentBinding == null) {
            unifier.putBinding(key(), expression);
            return Choice.of(unifier);
        } else if (currentBinding.toString().equals(expression.toString())) {
            // If it's the same code, treat it as the same expression.
            return Choice.of(unifier);
        }
    }
    return Choice.none();
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TreeScanner(com.sun.source.util.TreeScanner) Symbol(com.sun.tools.javac.code.Symbol) IdentifierTree(com.sun.source.tree.IdentifierTree) Nullable(javax.annotation.Nullable)

Example 48 with Symbol

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

use of com.sun.tools.javac.code.Symbol 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)

Example 50 with Symbol

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

the class Lower method visitEnumDef.

/** Translate an enum class. */
private void visitEnumDef(JCClassDecl tree) {
    make_at(tree.pos());
    // add the supertype, if needed
    if (tree.extending == null)
        tree.extending = make.Type(types.supertype(tree.type));
    // classOfType adds a cache field to tree.defs unless
    // target.hasClassLiterals().
    JCExpression e_class = classOfType(tree.sym.type, tree.pos()).setType(types.erasure(syms.classType));
    // process each enumeration constant, adding implicit constructor parameters
    int nextOrdinal = 0;
    ListBuffer<JCExpression> values = new ListBuffer<JCExpression>();
    ListBuffer<JCTree> enumDefs = new ListBuffer<JCTree>();
    ListBuffer<JCTree> otherDefs = new ListBuffer<JCTree>();
    for (List<JCTree> defs = tree.defs; defs.nonEmpty(); defs = defs.tail) {
        if (defs.head.getTag() == JCTree.VARDEF && (((JCVariableDecl) defs.head).mods.flags & ENUM) != 0) {
            JCVariableDecl var = (JCVariableDecl) defs.head;
            visitEnumConstantDef(var, nextOrdinal++);
            values.append(make.QualIdent(var.sym));
            enumDefs.append(var);
        } else {
            otherDefs.append(defs.head);
        }
    }
    // private static final T[] #VALUES = { a, b, c };
    Name valuesName = names.fromString(target.syntheticNameChar() + "VALUES");
    while (// avoid name clash
    tree.sym.members().lookup(valuesName).scope != null) valuesName = names.fromString(valuesName + "" + target.syntheticNameChar());
    Type arrayType = new ArrayType(types.erasure(tree.type), syms.arrayClass);
    VarSymbol valuesVar = new VarSymbol(PRIVATE | FINAL | STATIC | SYNTHETIC, valuesName, arrayType, tree.type.tsym);
    JCNewArray newArray = make.NewArray(make.Type(types.erasure(tree.type)), List.<JCExpression>nil(), values.toList());
    newArray.type = arrayType;
    enumDefs.append(make.VarDef(valuesVar, newArray));
    tree.sym.members().enter(valuesVar);
    Symbol valuesSym = lookupMethod(tree.pos(), names.values, tree.type, List.<Type>nil());
    List<JCStatement> valuesBody;
    if (useClone()) {
        // return (T[]) $VALUES.clone();
        JCTypeCast valuesResult = make.TypeCast(valuesSym.type.getReturnType(), make.App(make.Select(make.Ident(valuesVar), syms.arrayCloneMethod)));
        valuesBody = List.<JCStatement>of(make.Return(valuesResult));
    } else {
        // template: T[] $result = new T[$values.length];
        Name resultName = names.fromString(target.syntheticNameChar() + "result");
        while (// avoid name clash
        tree.sym.members().lookup(resultName).scope != null) resultName = names.fromString(resultName + "" + target.syntheticNameChar());
        VarSymbol resultVar = new VarSymbol(FINAL | SYNTHETIC, resultName, arrayType, valuesSym);
        JCNewArray resultArray = make.NewArray(make.Type(types.erasure(tree.type)), List.of(make.Select(make.Ident(valuesVar), syms.lengthVar)), null);
        resultArray.type = arrayType;
        JCVariableDecl decl = make.VarDef(resultVar, resultArray);
        // template: System.arraycopy($VALUES, 0, $result, 0, $VALUES.length);
        if (systemArraycopyMethod == null) {
            systemArraycopyMethod = new MethodSymbol(PUBLIC | STATIC, names.fromString("arraycopy"), new MethodType(List.<Type>of(syms.objectType, syms.intType, syms.objectType, syms.intType, syms.intType), syms.voidType, List.<Type>nil(), syms.methodClass), syms.systemType.tsym);
        }
        JCStatement copy = make.Exec(make.App(make.Select(make.Ident(syms.systemType.tsym), systemArraycopyMethod), List.of(make.Ident(valuesVar), make.Literal(0), make.Ident(resultVar), make.Literal(0), make.Select(make.Ident(valuesVar), syms.lengthVar))));
        // template: return $result;
        JCStatement ret = make.Return(make.Ident(resultVar));
        valuesBody = List.<JCStatement>of(decl, copy, ret);
    }
    JCMethodDecl valuesDef = make.MethodDef((MethodSymbol) valuesSym, make.Block(0, valuesBody));
    enumDefs.append(valuesDef);
    if (debugLower)
        System.err.println(tree.sym + ".valuesDef = " + valuesDef);
    /** The template for the following code is:
         *
         *     public static E valueOf(String name) {
         *         return (E)Enum.valueOf(E.class, name);
         *     }
         *
         *  where E is tree.sym
         */
    MethodSymbol valueOfSym = lookupMethod(tree.pos(), names.valueOf, tree.sym.type, List.of(syms.stringType));
    Assert.check((valueOfSym.flags() & STATIC) != 0);
    VarSymbol nameArgSym = valueOfSym.params.head;
    JCIdent nameVal = make.Ident(nameArgSym);
    JCStatement enum_ValueOf = make.Return(make.TypeCast(tree.sym.type, makeCall(make.Ident(syms.enumSym), names.valueOf, List.of(e_class, nameVal))));
    JCMethodDecl valueOf = make.MethodDef(valueOfSym, make.Block(0, List.of(enum_ValueOf)));
    nameVal.sym = valueOf.params.head.sym;
    if (debugLower)
        System.err.println(tree.sym + ".valueOf = " + valueOf);
    enumDefs.append(valueOf);
    enumDefs.appendList(otherDefs.toList());
    tree.defs = enumDefs.toList();
    // Add the necessary members for the EnumCompatibleMode
    if (target.compilerBootstrap(tree.sym)) {
        addEnumCompatibleMembers(tree);
    }
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree) Type(com.sun.tools.javac.code.Type)

Aggregations

Symbol (com.sun.tools.javac.code.Symbol)195 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)56 Type (com.sun.tools.javac.code.Type)54 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)53 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)36 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)29 JCTree (com.sun.tools.javac.tree.JCTree)28 ClassType (com.sun.tools.javac.code.Type.ClassType)18 Tree (com.sun.source.tree.Tree)17 ExpressionTree (com.sun.source.tree.ExpressionTree)15 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)15 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)15 ClassTree (com.sun.source.tree.ClassTree)14 MethodTree (com.sun.source.tree.MethodTree)14 Name (com.sun.tools.javac.util.Name)14 IdentifierTree (com.sun.source.tree.IdentifierTree)13 ArrayType (com.sun.tools.javac.code.Type.ArrayType)12 MethodType (com.sun.tools.javac.code.Type.MethodType)12 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)12