Search in sources :

Example 71 with JCStatement

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

the class StatementTransformer method openOuterSubstitutionIfNeeded.

JCStatement openOuterSubstitutionIfNeeded(Value value, Type t, List<JCAnnotation> annots, int modifiers) {
    JCStatement result = null;
    if (value.isSpecifiedInForElse()) {
        DeferredSpecification d = new DeferredSpecification(value, modifiers, annots, t);
        deferredSpecifications.put(value, d);
        result = d.openOuterSubstitution();
    }
    return result;
}
Also used : JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 72 with JCStatement

use of com.sun.tools.javac.tree.JCTree.JCStatement 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 73 with JCStatement

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

the class TreePruner method delegatingConstructor.

private static boolean delegatingConstructor(List<JCStatement> stats) {
    if (stats.isEmpty()) {
        return false;
    }
    JCStatement stat = stats.get(0);
    if (stat.getKind() != Kind.EXPRESSION_STATEMENT) {
        return false;
    }
    JCExpression expr = ((JCExpressionStatement) stat).getExpression();
    if (expr.getKind() != Kind.METHOD_INVOCATION) {
        return false;
    }
    JCExpression method = ((JCMethodInvocation) expr).getMethodSelect();
    Name name;
    switch(method.getKind()) {
        case IDENTIFIER:
            name = ((JCIdent) method).getName();
            break;
        case MEMBER_SELECT:
            name = ((JCFieldAccess) method).getIdentifier();
            break;
        default:
            return false;
    }
    return name.contentEquals("this") || name.contentEquals("super");
}
Also used : JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCExpressionStatement(com.sun.tools.javac.tree.JCTree.JCExpressionStatement) Name(com.sun.tools.javac.util.Name)

Example 74 with JCStatement

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

the class UPlaceholderStatement method apply.

@Override
public Choice<UnifierWithUnconsumedStatements> apply(final UnifierWithUnconsumedStatements initState) {
    final PlaceholderUnificationVisitor visitor = PlaceholderUnificationVisitor.create(TreeMaker.instance(initState.unifier().getContext()), arguments());
    PlaceholderVerificationVisitor verification = new PlaceholderVerificationVisitor(Collections2.transform(placeholder().requiredParameters(), Functions.forMap(arguments())), arguments().values());
    // The choices where we might conceivably have a completed placeholder match.
    Choice<State<ConsumptionState>> realOptions = Choice.none();
    // The choice of consumption states to this point in the block. 
    Choice<State<ConsumptionState>> choiceToHere = Choice.of(State.create(List.<UVariableDecl>nil(), initState.unifier(), ConsumptionState.empty()));
    if (verification.allRequiredMatched()) {
        realOptions = choiceToHere.or(realOptions);
    }
    for (final StatementTree targetStatement : initState.unconsumedStatements()) {
        if (!verification.scan(targetStatement, initState.unifier())) {
            // we saw a variable that's not allowed to be referenced
            break;
        }
        // Consume another statement, or if that fails, fall back to the previous choices...
        choiceToHere = choiceToHere.thenChoose(new Function<State<ConsumptionState>, Choice<State<ConsumptionState>>>() {

            @Override
            public Choice<State<ConsumptionState>> apply(final State<ConsumptionState> consumptionState) {
                return visitor.unifyStatement(targetStatement, consumptionState).transform(new Function<State<? extends JCStatement>, State<ConsumptionState>>() {

                    @Override
                    public State<ConsumptionState> apply(State<? extends JCStatement> stmtState) {
                        return stmtState.withResult(consumptionState.result().consume(stmtState.result()));
                    }
                });
            }
        });
        if (verification.allRequiredMatched()) {
            realOptions = choiceToHere.or(realOptions);
        }
    }
    return realOptions.thenOption(new Function<State<ConsumptionState>, Optional<UnifierWithUnconsumedStatements>>() {

        @Override
        public Optional<UnifierWithUnconsumedStatements> apply(State<ConsumptionState> consumptionState) {
            if (ImmutableSet.copyOf(consumptionState.seenParameters()).containsAll(placeholder().requiredParameters())) {
                Unifier resultUnifier = consumptionState.unifier().fork();
                int nConsumedStatements = consumptionState.result().consumedStatements();
                java.util.List<? extends StatementTree> remainingStatements = initState.unconsumedStatements().subList(nConsumedStatements, initState.unconsumedStatements().size());
                UnifierWithUnconsumedStatements result = UnifierWithUnconsumedStatements.create(resultUnifier, remainingStatements);
                List<JCStatement> impl = consumptionState.result().placeholderImplInReverseOrder().reverse();
                ControlFlowVisitor.Result implFlow = ControlFlowVisitor.INSTANCE.visitStatements(impl);
                if (implFlow == implementationFlow()) {
                    List<JCStatement> prevBinding = resultUnifier.getBinding(placeholder().blockKey());
                    if (prevBinding != null && prevBinding.toString().equals(impl.toString())) {
                        return Optional.of(result);
                    } else if (prevBinding == null) {
                        resultUnifier.putBinding(placeholder().blockKey(), impl);
                        return Optional.of(result);
                    }
                }
            }
            return Optional.absent();
        }
    });
}
Also used : Optional(com.google.common.base.Optional) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) StatementTree(com.sun.source.tree.StatementTree) Function(com.google.common.base.Function) State(com.google.errorprone.refaster.PlaceholderUnificationVisitor.State) List(com.sun.tools.javac.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 75 with JCStatement

use of com.sun.tools.javac.tree.JCTree.JCStatement in project lombok by rzwitserloot.

the class JavacJavaUtilMapSingularizer method generateMethods.

@Override
public void generateMethods(SingularData data, JavacNode builderType, JCTree source, boolean fluent, boolean chain) {
    if (useGuavaInstead(builderType)) {
        guavaMapSingularizer.generateMethods(data, builderType, source, fluent, chain);
        return;
    }
    JavacTreeMaker maker = builderType.getTreeMaker();
    Symtab symbolTable = builderType.getSymbolTable();
    JCExpression returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    JCStatement returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generateSingularMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generatePluralMethod(maker, returnType, returnStatement, data, builderType, source, fluent);
    returnType = chain ? cloneSelfType(builderType) : maker.Type(createVoidType(symbolTable, CTC_VOID));
    returnStatement = chain ? maker.Return(maker.Ident(builderType.toName("this"))) : null;
    generateClearMethod(maker, returnType, returnStatement, data, builderType, source);
}
Also used : Symtab(com.sun.tools.javac.code.Symtab) JavacTreeMaker(lombok.javac.JavacTreeMaker) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Aggregations

JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)112 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)82 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)37 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)33 JCTree (com.sun.tools.javac.tree.JCTree)32 Name (com.sun.tools.javac.util.Name)32 Type (com.redhat.ceylon.model.typechecker.model.Type)26 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)26 ListBuffer (com.sun.tools.javac.util.ListBuffer)25 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)23 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)18 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)16 JavacTreeMaker (lombok.javac.JavacTreeMaker)16 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)14 JCModifiers (com.sun.tools.javac.tree.JCTree.JCModifiers)14 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)12 JCAnnotation (com.sun.tools.javac.tree.JCTree.JCAnnotation)12 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)12 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)11 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)10