Search in sources :

Example 21 with JCExpression

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.

the class AttributeDefinitionBuilder method getterExceptionThrowing.

public List<JCStatement> getterExceptionThrowing(List<JCStatement> stmts) {
    JCExpression init = initTest.makeInitTest(true);
    if (init != null) {
        List<JCStatement> catchStmts;
        JCExpression msg = owner.make().Literal(attrTypedDecl.isLate() ? "Accessing uninitialized 'late' attribute '" + attrName + "'" : "Cyclic initialization trying to read the value of '" + attrName + "' before it was set");
        JCTree.JCThrow throwStmt = owner.make().Throw(owner.makeNewClass(owner.makeIdent(owner.syms().ceylonInitializationErrorType), List.<JCExpression>of(msg)));
        if (deferredInitError) {
            JCStatement rethrow = owner.make().Exec(owner.utilInvocation().rethrow(makeExceptionFieldAccess()));
            // rethrow the init exception if we have one
            JCIf ifThrow = owner.make().If(owner.make().Binary(JCTree.Tag.NE, makeExceptionFieldAccess(), owner.makeNull()), rethrow, null);
            catchStmts = List.<JCTree.JCStatement>of(ifThrow, throwStmt);
        } else if (memoizedInitialValue != null) {
            JCExpression f = makeValueFieldAccess();
            if (isHash) {
                f = owner.convertToIntForHashAttribute(f);
            }
            catchStmts = List.<JCStatement>of(owner.make().Return(f)).prependList(makeInitialized(true, initValueField()));
        } else {
            catchStmts = List.<JCTree.JCStatement>of(throwStmt);
        }
        stmts = List.<JCTree.JCStatement>of(owner.make().If(init, owner.make().Block(0, stmts), owner.make().Block(0, catchStmts)));
    }
    return stmts;
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCIf(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCIf) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 22 with JCExpression

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.

the class AttributeDefinitionBuilder method setter.

private List<JCStatement> setter() {
    JCExpression fld = makeValueFieldAccess();
    JCExpression setValue = owner.makeQuotedIdent(setterParameterName());
    if (useJavaBox && !owner.isJavaString(attrType)) {
        setValue = owner.make().Conditional(owner.make().Binary(JCTree.Tag.EQ, owner.makeQuotedIdent(setterParameterName()), owner.makeNull()), owner.makeNull(), owner.boxJavaType(owner.unboxType(setValue, owner.simplifyType(attrType)), attrType));
    }
    List<JCStatement> stmts = List.<JCStatement>of(owner.make().Exec(owner.make().Assign(fld, setValue)));
    return stmts;
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 23 with JCExpression

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.

the class CallBuilder method build.

public JCExpression build() {
    if (built) {
        throw new BugException("already built");
    }
    built = true;
    JCExpression result;
    List<JCExpression> arguments;
    final JCExpression newEncl;
    if ((cbOpts & CB_ALIAS_ARGS) != 0) {
        if (instantiateQualfier != null && instantiateQualfier.expression != null) {
            if (instantiateQualfier.type == null) {
                throw new BugException(MISSING_TYPE);
            }
            SyntheticName qualName = getQualifierName(basename);
            appendStatement(gen.makeVar(Flags.FINAL, qualName, instantiateQualfier.type, instantiateQualfier.expression));
            newEncl = qualName.makeIdent();
        } else {
            newEncl = null;
        }
        arguments = List.<JCExpression>nil();
        int argumentNum = 0;
        for (ExpressionAndType argumentAndType : argumentsAndTypes) {
            SyntheticName name = getArgumentName(basename, argumentNum);
            if (argumentAndType.type == null) {
                throw new BugException(MISSING_TYPE);
            }
            if ((cbOpts & CB_ALIAS_ARGS) != 0) {
                appendStatement(gen.makeVar(Flags.FINAL, name, argumentAndType.type, argumentAndType.expression));
            }
            arguments = arguments.append(name.makeIdent());
            argumentNum++;
        }
    } else {
        newEncl = this.instantiateQualfier != null ? this.instantiateQualfier.expression : null;
        arguments = ExpressionAndType.toExpressionList(this.argumentsAndTypes);
    }
    if (haveLocation) {
        gen.at(this.location);
    }
    switch(kind) {
        case APPLY:
            result = gen.make().Apply(this.typeargs.toList(), this.methodOrClass, arguments);
            break;
        case NEW:
            result = gen.make().NewClass(newEncl, null, this.methodOrClass, arguments, classDefs);
            break;
        case ARRAY_READ:
            result = gen.make().Indexed(this.methodOrClass, arguments.head);
            break;
        case ARRAY_WRITE:
            {
                JCExpression array;
                if (arrayWriteNeedsCast)
                    array = gen.make().TypeCast(gen.make().TypeArray(gen.make().Type(gen.syms().objectType)), this.methodOrClass);
                else
                    array = this.methodOrClass;
                result = gen.make().Assign(gen.make().Indexed(array, arguments.head), arguments.tail.head);
            }
            break;
        case NEW_ARRAY:
            // methodOrClass must be a ArrayType, so we get the element type out
            JCExpression elementTypeExpr = ((JCTree.JCArrayTypeTree) this.methodOrClass).elemtype;
            if (arrayInstanceReifiedType == null) {
                result = gen.make().NewArray(elementTypeExpr, List.of(arguments.head), null);
                if (arrayInstanceCast != null) {
                    result = gen.make().TypeCast(arrayInstanceCast, result);
                }
            } else {
                List<JCExpression> dimensions = List.nil();
                if (arrayInstanceDimensions > 1) {
                    for (int i = 1; i < arrayInstanceDimensions; i++) {
                        dimensions = dimensions.prepend(gen.makeInteger(0));
                    }
                }
                dimensions = dimensions.prepend(arguments.head);
                dimensions = dimensions.prepend(arrayInstanceReifiedType);
                result = gen.utilInvocation().makeArray(dimensions);
            }
            if (arguments.tail.nonEmpty()) {
                // must fill it
                result = gen.utilInvocation().fillArray(List.of(result, arguments.tail.head));
            }
            break;
        case NEW_ARRAY_WITH:
            Declaration d = arrayType.getDeclaration();
            if (d.equals(gen.typeFact().getJavaLongArrayDeclaration())) {
                result = gen.utilInvocation().toLongArray(arguments.head, arguments.tail);
            } else if (d.equals(gen.typeFact().getJavaIntArrayDeclaration())) {
                result = gen.utilInvocation().toIntArray(arguments.head, arguments.tail);
            } else if (d.equals(gen.typeFact().getJavaShortArrayDeclaration())) {
                result = gen.utilInvocation().toShortArray(arguments.head, arguments.tail);
            } else if (d.equals(gen.typeFact().getJavaCharArrayDeclaration())) {
                result = gen.utilInvocation().toCharArray(arguments.head, arguments.tail);
            } else if (d.equals(gen.typeFact().getJavaByteArrayDeclaration())) {
                result = gen.utilInvocation().toByteArray(arguments.head, arguments.tail);
            } else if (d.equals(gen.typeFact().getJavaBooleanArrayDeclaration())) {
                result = gen.utilInvocation().toBooleanArray(arguments.head, arguments.tail);
            } else if (d.equals(gen.typeFact().getJavaFloatArrayDeclaration())) {
                result = gen.utilInvocation().toFloatArray(arguments.head, arguments.tail);
            } else if (d.equals(gen.typeFact().getJavaDoubleArrayDeclaration())) {
                result = gen.utilInvocation().toDoubleArray(arguments.head, arguments.tail);
            } else {
                // it must be an ObjectArray
                result = gen.utilInvocation().toArray(arguments.tail.head, // don't use makeClassLiteral, because it doesn't c.l.Object -> j.l.Object
                gen.makeSelect(gen.makeJavaType(arrayType.getTypeArgumentList().get(0), AbstractTransformer.JT_NO_PRIMITIVES), "class"), arguments.tail.tail);
            }
            break;
        case FIELD_READ:
            result = this.methodOrClass;
            break;
        default:
            throw BugException.unhandledEnumCase(kind);
    }
    if ((cbOpts & CB_LET) != 0) {
        if (voidMethod) {
            result = gen.make().LetExpr(statements.toList().append(gen.make().Exec(result)), gen.makeNull());
        } else if (!statements.isEmpty()) {
            result = gen.make().LetExpr(statements.toList(), result);
        }
    }
    return result;
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 24 with JCExpression

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.

the class CallableBuilder method buildTypeConstructor.

protected JCExpression buildTypeConstructor(Type callableType, JCNewClass callableInstance) {
    JCExpression result;
    // Wrap in an anonymous TypeConstructor subcla
    MethodDefinitionBuilder rawApply = MethodDefinitionBuilder.systemMethod(gen, Naming.Unfix.apply.toString());
    rawApply.modifiers(Flags.PUBLIC);
    rawApply.isOverride(true);
    // for (TypeParameter tp : typeModel.getDeclaration().getTypeParameters()) {
    // apply.typeParameter(tp);
    // }
    rawApply.resultType(new TransformedType(gen.makeJavaType(callableType, AbstractTransformer.JT_RAW)));
    {
        ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(gen, "applied");
        pdb.modifiers(Flags.FINAL);
        pdb.type(new TransformedType(gen.make().TypeArray(gen.make().Type(gen.syms().ceylonTypeDescriptorType))));
        rawApply.parameter(pdb);
    }
    rawApply.body(List.<JCStatement>of(gen.make().Return(gen.make().Apply(null, gen.naming.makeUnquotedIdent(Naming.Unfix.$apply$.toString()), List.<JCExpression>of(gen.naming.makeUnquotedIdent("applied"))))));
    MethodDefinitionBuilder typedApply = MethodDefinitionBuilder.systemMethod(gen, Naming.Unfix.$apply$.toString());
    typedApply.modifiers(Flags.PRIVATE);
    // for (TypeParameter tp : typeModel.getDeclaration().getTypeParameters()) {
    // apply.typeParameter(tp);
    // }
    typedApply.resultType(new TransformedType(gen.makeJavaType(callableType)));
    {
        ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(gen, "applied");
        pdb.modifiers(Flags.FINAL);
        pdb.type(new TransformedType(gen.make().TypeArray(gen.make().Type(gen.syms().ceylonTypeDescriptorType))));
        typedApply.parameter(pdb);
    }
    ListBuffer<JCTypeParameter> typeParameters = new ListBuffer<JCTypeParameter>();
    for (TypeParameter typeParameter : Strategy.getEffectiveTypeParameters(typeModel.getDeclaration())) {
        Type typeArgument = typeModel.getTypeArguments().get(typeParameter);
        typeParameters.add(gen.makeTypeParameter(typeParameter, null));
        typedApply.body(gen.makeVar(Flags.FINAL, gen.naming.getTypeArgumentDescriptorName(typeParameter), gen.make().Type(gen.syms().ceylonTypeDescriptorType), gen.make().Indexed(gen.makeUnquotedIdent("applied"), gen.make().Literal(typeModel.getTypeArgumentList().indexOf(typeArgument)))));
    }
    typedApply.body(gen.make().Return(callableInstance));
    // typedApply.body(body.toList());
    MethodDefinitionBuilder ctor = MethodDefinitionBuilder.constructor(gen, false);
    ctor.body(gen.make().Exec(gen.make().Apply(null, gen.naming.makeSuper(), List.<JCExpression>of(gen.make().Literal(typeModel.asString(true))))));
    SyntheticName n = gen.naming.synthetic(typeModel.getDeclaration().getName());
    JCClassDecl classDef = gen.make().ClassDef(gen.make().Modifiers(0, List.<JCAnnotation>nil()), // name,
    n.asName(), typeParameters.toList(), // extending
    gen.make().QualIdent(gen.syms().ceylonAbstractTypeConstructorType.tsym), // implementing,
    List.<JCExpression>nil(), List.<JCTree>of(ctor.build(), rawApply.build(), typedApply.build()));
    result = gen.make().LetExpr(List.<JCStatement>of(classDef), gen.make().NewClass(null, null, n.makeIdent(), List.<JCExpression>nil(), // List.<JCExpression>of(gen.make().Literal(typeModel.asString(true))),
    null));
    return result;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Example 25 with JCExpression

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression in project ceylon by eclipse.

the class CallableBuilder method anonymous.

/**
 * Constructs an {@code AbstractCallable} suitable for an anonymous function.
 */
public static CallableBuilder anonymous(CeylonTransformer gen, Node node, FunctionOrValue model, Tree.Expression expr, java.util.List<Tree.ParameterList> parameterListTree, Type callableTypeModel, boolean delegateDefaultedCalls) {
    boolean prevSyntheticClassBody = gen.expressionGen().withinSyntheticClassBody(true);
    JCExpression transformedExpr = gen.expressionGen().transformExpression(expr, BoxingStrategy.BOXED, gen.getReturnTypeOfCallable(callableTypeModel));
    gen.expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
    final List<JCStatement> stmts = List.<JCStatement>of(gen.make().Return(transformedExpr));
    return methodArgument(gen, node, model, callableTypeModel, parameterListTree, stmts, delegateDefaultedCalls);
}
Also used : JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Aggregations

JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)224 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)95 Type (org.eclipse.ceylon.model.typechecker.model.Type)95 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)74 JCStatement (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)53 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)53 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)45 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)41 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)39 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)38 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)33 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)32 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)30 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)28 Function (org.eclipse.ceylon.model.typechecker.model.Function)26 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)22 Value (org.eclipse.ceylon.model.typechecker.model.Value)22 JCNewClass (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)21 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)21 Class (org.eclipse.ceylon.model.typechecker.model.Class)17