Search in sources :

Example 66 with JCVariableDecl

use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.

the class NamedArgumentInvocation method makeThis.

private final JCVariableDecl makeThis() {
    // first append $this
    JCExpression defaultedParameterInstance;
    // TODO Fix how we figure out the thisType, because it's doesn't 
    // handle type parameters correctly
    // we used to use thisType = gen.getThisType(getPrimaryDeclaration());
    final JCExpression thisType;
    Reference target = ((Tree.MemberOrTypeExpression) getPrimary()).getTarget();
    if (getPrimary() instanceof Tree.BaseMemberExpression && !gen.expressionGen().isWithinSyntheticClassBody()) {
        if (Decl.withinClassOrInterface(getPrimaryDeclaration())) {
            // a member method
            thisType = gen.makeJavaType(target.getQualifyingType(), JT_NO_PRIMITIVES);
            defaultedParameterInstance = gen.naming.makeThis();
        } else {
            // a local or toplevel function
            thisType = gen.naming.makeName((TypedDeclaration) getPrimaryDeclaration(), Naming.NA_WRAPPER);
            defaultedParameterInstance = gen.naming.makeName((TypedDeclaration) getPrimaryDeclaration(), Naming.NA_MEMBER);
        }
    } else if (getPrimary() instanceof Tree.BaseTypeExpression || getPrimary() instanceof Tree.QualifiedTypeExpression) {
        TypeDeclaration declaration = (TypeDeclaration) ((Tree.MemberOrTypeExpression) getPrimary()).getDeclaration();
        thisType = gen.makeJavaType(declaration.getType(), JT_COMPANION);
        defaultedParameterInstance = gen.make().NewClass(null, null, gen.makeJavaType(declaration.getType(), JT_COMPANION), List.<JCExpression>nil(), null);
    } else {
        if (isOnValueType()) {
            thisType = gen.makeJavaType(target.getQualifyingType());
        } else {
            thisType = gen.makeJavaType(target.getQualifyingType(), JT_NO_PRIMITIVES);
        }
        defaultedParameterInstance = callVarName.makeIdent();
    }
    JCVariableDecl thisDecl = gen.makeVar(varBaseName.suffixedBy(Suffix.$argthis$), thisType, defaultedParameterInstance);
    return thisDecl;
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Reference(com.redhat.ceylon.model.typechecker.model.Reference) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) QualifiedTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.QualifiedTypeExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 67 with JCVariableDecl

use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project ceylon-compiler by ceylon.

the class NamedArgumentInvocation method bindMethodArgument.

private void bindMethodArgument(Tree.MethodArgument methodArg, Parameter declaredParam, Naming.SyntheticName argName) {
    ListBuffer<JCStatement> statements;
    Function model = methodArg.getDeclarationModel();
    List<JCStatement> body;
    boolean prevNoExpressionlessReturn = gen.statementGen().noExpressionlessReturn;
    boolean prevSyntheticClassBody = gen.expressionGen().withinSyntheticClassBody(Decl.isMpl(model) || gen.expressionGen().isWithinSyntheticClassBody());
    try {
        gen.statementGen().noExpressionlessReturn = gen.isAnything(model.getType());
        if (methodArg.getBlock() != null) {
            body = gen.statementGen().transformBlock(methodArg.getBlock());
            if (!methodArg.getBlock().getDefinitelyReturns()) {
                if (gen.isAnything(model.getType())) {
                    body = body.append(gen.make().Return(gen.makeNull()));
                } else {
                    body = body.append(gen.make().Return(gen.makeErroneous(methodArg.getBlock(), "compiler bug: non-void method does not definitely return")));
                }
            }
        } else {
            Expression expr = methodArg.getSpecifierExpression().getExpression();
            BoxingStrategy boxing = CodegenUtil.getBoxingStrategy(model);
            Type type = model.getType();
            JCExpression transExpr = gen.expressionGen().transformExpression(expr, boxing, type);
            JCReturn returnStat = gen.make().Return(transExpr);
            body = List.<JCStatement>of(returnStat);
        }
    } finally {
        gen.expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
        gen.statementGen().noExpressionlessReturn = prevNoExpressionlessReturn;
    }
    Type callableType = model.appliedReference(null, Collections.<Type>emptyList()).getFullType();
    CallableBuilder callableBuilder = CallableBuilder.methodArgument(gen.gen(), methodArg, model, callableType, Collections.singletonList(methodArg.getParameterLists().get(0)), gen.classGen().transformMplBody(methodArg.getParameterLists(), model, body));
    JCExpression callable = callableBuilder.build();
    JCExpression typeExpr = gen.makeJavaType(callableType, JT_RAW);
    JCVariableDecl varDecl = gen.makeVar(argName, typeExpr, callable);
    statements = ListBuffer.<JCStatement>of(varDecl);
    bind(declaredParam, argName, gen.makeJavaType(callableType), statements.toList());
}
Also used : Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) JCReturn(com.sun.tools.javac.tree.JCTree.JCReturn) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) QualifiedTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.QualifiedTypeExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) BoxingStrategy(com.redhat.ceylon.compiler.java.codegen.AbstractTransformer.BoxingStrategy) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 68 with JCVariableDecl

use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project bazel by bazelbuild.

the class TreePrunerTest method parseField.

JCVariableDecl parseField(String line) {
    JCCompilationUnit unit = parseLines("class T {", line, "}");
    JCClassDecl classDecl = (JCClassDecl) getOnlyElement(unit.defs);
    return (JCVariableDecl) getOnlyElement(classDecl.defs);
}
Also used : JCCompilationUnit(com.sun.tools.javac.tree.JCTree.JCCompilationUnit) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 69 with JCVariableDecl

use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project bazel by bazelbuild.

the class TreePrunerTest method printPruned.

String printPruned(String line) {
    JCVariableDecl tree = parseField(line);
    TreePruner.prune(context, tree);
    return tree.toString();
}
Also used : JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 70 with JCVariableDecl

use of com.sun.tools.javac.tree.JCTree.JCVariableDecl in project error-prone by google.

the class SelfAssignment method describeForAssignment.

/**
   * We expect that the lhs is a field and the rhs is an identifier, specifically
   * a parameter to the method.  We base our suggested fixes on this expectation.
   *
   * Case 1: If lhs is a field and rhs is an identifier, find a method parameter
   * of the same type and similar name and suggest it as the rhs.  (Guess that they
   * have misspelled the identifier.)
   *
   * Case 2: If lhs is a field and rhs is not an identifier, find a method parameter
   * of the same type and similar name and suggest it as the rhs.
   *
   * Case 3: If lhs is not a field and rhs is an identifier, find a class field
   * of the same type and similar name and suggest it as the lhs.
   *
   * Case 4: Otherwise suggest deleting the assignment.
   */
public Description describeForAssignment(AssignmentTree assignmentTree, VisitorState state) {
    // the statement that is the parent of the self-assignment expression
    Tree parent = state.getPath().getParentPath().getLeaf();
    // default fix is to delete assignment
    Fix fix = SuggestedFix.delete(parent);
    ExpressionTree lhs = assignmentTree.getVariable();
    ExpressionTree rhs = assignmentTree.getExpression();
    // if this is a method invocation, they must be calling checkNotNull()
    if (assignmentTree.getExpression().getKind() == METHOD_INVOCATION) {
        // change the default fix to be "checkNotNull(x)" instead of "x = checkNotNull(x)"
        fix = SuggestedFix.replace(assignmentTree, rhs.toString());
        // new rhs is first argument to checkNotNull()
        rhs = stripCheckNotNull(rhs, state);
    }
    if (lhs.getKind() == MEMBER_SELECT) {
        // rhs should be either identifier or field access
        assert (rhs.getKind() == IDENTIFIER || rhs.getKind() == MEMBER_SELECT);
        // get current name of rhs
        String rhsName = null;
        if (rhs.getKind() == IDENTIFIER) {
            rhsName = ((JCIdent) rhs).name.toString();
        } else if (rhs.getKind() == MEMBER_SELECT) {
            rhsName = ((JCFieldAccess) rhs).name.toString();
        }
        // find method parameters of the same type
        Type type = ((JCFieldAccess) lhs).type;
        TreePath path = state.getPath();
        while (path != null && path.getLeaf().getKind() != METHOD) {
            path = path.getParentPath();
        }
        JCMethodDecl method = (JCMethodDecl) path.getLeaf();
        int minEditDistance = Integer.MAX_VALUE;
        String replacement = null;
        for (JCVariableDecl var : method.params) {
            if (var.type == type) {
                int editDistance = EditDistance.getEditDistance(rhsName, var.name.toString());
                if (editDistance < minEditDistance) {
                    // pick one with minimum edit distance
                    minEditDistance = editDistance;
                    replacement = var.name.toString();
                }
            }
        }
        if (replacement != null) {
            // suggest replacing rhs with the parameter
            fix = SuggestedFix.replace(rhs, replacement);
        }
    } else if (rhs.getKind() == IDENTIFIER) {
        // lhs should be identifier
        assert (lhs.getKind() == IDENTIFIER);
        // get current name of lhs
        String lhsName = ((JCIdent) rhs).name.toString();
        // find class instance fields of the same type
        Type type = ((JCIdent) lhs).type;
        TreePath path = state.getPath();
        while (path != null && !(path.getLeaf() instanceof JCClassDecl)) {
            path = path.getParentPath();
        }
        if (path == null) {
            throw new IllegalStateException("Expected to find an enclosing class declaration");
        }
        JCClassDecl klass = (JCClassDecl) path.getLeaf();
        int minEditDistance = Integer.MAX_VALUE;
        String replacement = null;
        for (JCTree member : klass.getMembers()) {
            if (member.getKind() == VARIABLE) {
                JCVariableDecl var = (JCVariableDecl) member;
                if (!Flags.isStatic(var.sym) && var.type == type) {
                    int editDistance = EditDistance.getEditDistance(lhsName, var.name.toString());
                    if (editDistance < minEditDistance) {
                        // pick one with minimum edit distance
                        minEditDistance = editDistance;
                        replacement = var.name.toString();
                    }
                }
            }
        }
        if (replacement != null) {
            // suggest replacing lhs with the field
            fix = SuggestedFix.replace(lhs, "this." + replacement);
        }
    }
    return describeMatch(assignmentTree, fix);
}
Also used : JCIdent(com.sun.tools.javac.tree.JCTree.JCIdent) JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JCFieldAccess(com.sun.tools.javac.tree.JCTree.JCFieldAccess) JCTree(com.sun.tools.javac.tree.JCTree) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Type(com.sun.tools.javac.code.Type) Fix(com.google.errorprone.fixes.Fix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) TreePath(com.sun.source.util.TreePath) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Aggregations

JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)98 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)56 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)36 JCTree (com.sun.tools.javac.tree.JCTree)31 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)31 Name (com.sun.tools.javac.util.Name)31 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)27 JavacNode (lombok.javac.JavacNode)27 ListBuffer (com.sun.tools.javac.util.ListBuffer)25 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)24 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)22 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)18 JavacTreeMaker (lombok.javac.JavacTreeMaker)18 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)13 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)12 Type (com.redhat.ceylon.model.typechecker.model.Type)11 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)9 JCMethodInvocation (com.sun.tools.javac.tree.JCTree.JCMethodInvocation)9 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)8 Type (com.sun.tools.javac.code.Type)7