Search in sources :

Example 81 with Class

use of com.redhat.ceylon.model.typechecker.model.Class in project ceylon-compiler by ceylon.

the class ExpressionTransformer method checkForByteLiterals.

private JCExpression checkForByteLiterals(Tree.InvocationExpression ce) {
    // same test as in BoxingVisitor.isByteLiteral()
    if (ce.getPrimary() instanceof Tree.BaseTypeExpression && ce.getPositionalArgumentList() != null) {
        java.util.List<Tree.PositionalArgument> positionalArguments = ce.getPositionalArgumentList().getPositionalArguments();
        if (positionalArguments.size() == 1) {
            PositionalArgument argument = positionalArguments.get(0);
            if (argument instanceof Tree.ListedArgument && ((Tree.ListedArgument) argument).getExpression() != null) {
                Term term = ((Tree.ListedArgument) argument).getExpression().getTerm();
                boolean negative = false;
                if (term instanceof Tree.NegativeOp) {
                    negative = true;
                    term = ((Tree.NegativeOp) term).getTerm();
                }
                if (term instanceof Tree.NaturalLiteral) {
                    Declaration decl = ((Tree.BaseTypeExpression) ce.getPrimary()).getDeclaration();
                    if (decl instanceof Class) {
                        String name = decl.getQualifiedNameString();
                        if (name.equals("ceylon.language::Byte")) {
                            at(ce);
                            try {
                                long value = literalValue((Tree.NaturalLiteral) term);
                                if (negative)
                                    value = -value;
                                // assignment, not for method calls, so it's simpler to always cast
                                return make().TypeCast(syms().byteType, make().Literal(value));
                            } catch (ErroneousException e) {
                                // replaced with a throw.
                                return e.makeErroneous(this);
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : PositionalArgument(com.redhat.ceylon.compiler.typechecker.tree.Tree.PositionalArgument) TreeUtil.unwrapExpressionUntilTerm(com.redhat.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm) Term(com.redhat.ceylon.compiler.typechecker.tree.Tree.Term) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

Class (com.redhat.ceylon.model.typechecker.model.Class)81 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)52 Type (com.redhat.ceylon.model.typechecker.model.Type)45 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)37 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)28 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)26 Function (com.redhat.ceylon.model.typechecker.model.Function)23 IntersectionType (com.redhat.ceylon.model.typechecker.model.IntersectionType)22 UnionType (com.redhat.ceylon.model.typechecker.model.UnionType)22 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)22 TypeParser (com.redhat.ceylon.model.loader.TypeParser)21 Test (org.junit.Test)21 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)20 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)19 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)19 Value (com.redhat.ceylon.model.typechecker.model.Value)19 JCTree (com.sun.tools.javac.tree.JCTree)19 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)17 Interface (com.redhat.ceylon.model.typechecker.model.Interface)17 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)14