Search in sources :

Example 1 with Tree

use of com.redhat.ceylon.compiler.typechecker.tree.Tree in project ceylon-compiler by ceylon.

the class StatementTransformer method transform.

/**
 * Transforms a Ceylon destructuring assignment to Java code.
 * @param stmt The Ceylon destructure
 * @return The Java tree
 */
List<JCVariableDecl> transform(Tree.Destructure stmt) {
    List<JCVariableDecl> result = List.nil();
    // Create temp var to hold result of expression
    Tree.Pattern pat = stmt.getPattern();
    Naming.SyntheticName tmpVarName = naming.synthetic(pat);
    Expression destExpr = stmt.getSpecifierExpression().getExpression();
    JCExpression typeExpr = makeJavaType(destExpr.getTypeModel());
    JCExpression expr = expressionGen().transformExpression(destExpr);
    at(stmt);
    JCVariableDecl tmpVar = makeVar(Flags.FINAL, tmpVarName, typeExpr, expr);
    result = result.append(tmpVar);
    // Now add the destructured variables
    List<JCVariableDecl> vars = VarDefBuilder.buildAll(transformPattern(pat, tmpVarName.makeIdent()));
    result = result.appendList(vars);
    return result;
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 2 with Tree

use of com.redhat.ceylon.compiler.typechecker.tree.Tree in project ceylon-compiler by ceylon.

the class StatementTransformer method getDocAnnotationText.

private String getDocAnnotationText(Tree.Assertion ass) {
    String docText = null;
    Tree.Annotation doc = getAnnotation(ass.getAnnotationList(), "doc");
    if (doc != null) {
        Tree.Expression expression = null;
        if (doc.getPositionalArgumentList() != null) {
            Tree.PositionalArgument arg = doc.getPositionalArgumentList().getPositionalArguments().get(0);
            if (arg instanceof Tree.ListedArgument)
                expression = ((Tree.ListedArgument) arg).getExpression();
            else
                throw new BugException(arg, "argument to doc annotation cannot be a spread argument or comprehension: " + arg);
        } else if (doc.getNamedArgumentList() != null) {
            Tree.SpecifiedArgument arg = (Tree.SpecifiedArgument) doc.getNamedArgumentList().getNamedArguments().get(0);
            expression = arg.getSpecifierExpression().getExpression();
        } else {
            // Impossible on a well-formed tree
            return null;
        }
        Tree.Literal literal = (Tree.Literal) expression.getTerm();
        docText = literal.getText();
    } else if (ass.getAnnotationList() != null && ass.getAnnotationList().getAnonymousAnnotation() != null) {
        docText = ass.getAnnotationList().getAnonymousAnnotation().getStringLiteral().getText();
    }
    return docText;
}
Also used : Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

Example 3 with Tree

use of com.redhat.ceylon.compiler.typechecker.tree.Tree in project ceylon-compiler by ceylon.

the class BoxingVisitor method visit.

@Override
public void visit(Tree.IfExpression that) {
    super.visit(that);
    if (that.getIfClause() == null || that.getElseClause() == null)
        return;
    Tree.Expression ifExpr = that.getIfClause().getExpression();
    Tree.Expression elseExpr = that.getElseClause().getExpression();
    if (ifExpr == null || elseExpr == null)
        return;
    if (CodegenUtil.isUnBoxed(ifExpr) && CodegenUtil.isUnBoxed(elseExpr) && !willEraseToObject(that.getUnit().denotableType(that.getTypeModel())))
        CodegenUtil.markUnBoxed(that);
    if (that.getTypeModel().isExactly(that.getUnit().getNullValueDeclaration().getType())) {
        CodegenUtil.markTypeErased(that);
    }
// An If expression can never be raw, type erased or untrusted because
// it uses a Let with a new variable declaration, so the rawness,
// erasedness and untrustedness of its branches cannot propagate further
// up the tree.
}
Also used : Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree)

Example 4 with Tree

use of com.redhat.ceylon.compiler.typechecker.tree.Tree in project ceylon-compiler by ceylon.

the class BoxingVisitor method visit.

@Override
public void visit(Tree.SwitchExpression that) {
    super.visit(that);
    SwitchCaseList caseList = that.getSwitchCaseList();
    if (caseList == null || caseList.getCaseClauses() == null)
        return;
    boolean unboxed = true;
    for (Tree.CaseClause caseClause : caseList.getCaseClauses()) {
        Expression expr = caseClause.getExpression();
        if (expr == null)
            return;
        // a single boxed one makes the whole switch boxed
        if (!CodegenUtil.isUnBoxed(expr))
            unboxed = false;
    // A Switch expression can never be raw, type erased or untrusted because
    // it uses a Let with a new variable declaration, so the rawness,
    // erasedness and untrustedness of its branches cannot propagate further
    // up the tree.
    }
    if (caseList.getElseClause() != null) {
        Expression expr = caseList.getElseClause().getExpression();
        if (expr == null)
            return;
        // a single boxed one makes the whole switch boxed
        if (!CodegenUtil.isUnBoxed(expr))
            unboxed = false;
    // see comment about about why we don't propagate rawness etc here.
    }
    if (unboxed && !willEraseToObject(that.getUnit().denotableType(that.getTypeModel())))
        CodegenUtil.markUnBoxed(that);
    if (that.getTypeModel().isExactly(that.getUnit().getNullValueDeclaration().getType())) {
        CodegenUtil.markTypeErased(that);
    }
}
Also used : InvocationExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.InvocationExpression) MemberOrTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.MemberOrTypeExpression) PrefixOperatorExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.PrefixOperatorExpression) PostfixOperatorExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.PostfixOperatorExpression) StaticMemberOrTypeExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression) ParameterizedExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.ParameterizedExpression) IndexExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.IndexExpression) QualifiedMemberExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.QualifiedMemberExpression) Expression(com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression) BaseMemberExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.BaseMemberExpression) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) SwitchCaseList(com.redhat.ceylon.compiler.typechecker.tree.Tree.SwitchCaseList)

Aggregations

Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)4 Expression (com.redhat.ceylon.compiler.typechecker.tree.Tree.Expression)4 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)2 JCTree (com.sun.tools.javac.tree.JCTree)2 SyntheticName (com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)1 BaseMemberExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.BaseMemberExpression)1 IndexExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.IndexExpression)1 InvocationExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.InvocationExpression)1 MemberOrTypeExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.MemberOrTypeExpression)1 ParameterizedExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.ParameterizedExpression)1 PostfixOperatorExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.PostfixOperatorExpression)1 PrefixOperatorExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.PrefixOperatorExpression)1 QualifiedMemberExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.QualifiedMemberExpression)1 SpecifierOrInitializerExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)1 StaticMemberOrTypeExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression)1 SwitchCaseList (com.redhat.ceylon.compiler.typechecker.tree.Tree.SwitchCaseList)1 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)1 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)1