Search in sources :

Example 51 with TypedDeclaration

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

the class ExpressionTransformer method transformAssignment.

private JCExpression transformAssignment(Node op, Tree.Term leftTerm, JCExpression lhs, JCExpression rhs) {
    JCExpression result = null;
    // FIXME: can this be anything else than a Tree.StaticMemberOrTypeExpression or Tree.ParameterizedExpression?
    TypedDeclaration decl;
    if (leftTerm instanceof Tree.StaticMemberOrTypeExpression) {
        decl = (TypedDeclaration) ((Tree.StaticMemberOrTypeExpression) leftTerm).getDeclaration();
        lhs = addInterfaceImplAccessorIfRequired(lhs, (Tree.StaticMemberOrTypeExpression) leftTerm, decl);
        lhs = addThisOrObjectQualifierIfRequired(lhs, (Tree.StaticMemberOrTypeExpression) leftTerm, decl);
    } else {
        // instanceof Tree.ParameterizedExpression
        decl = (TypedDeclaration) ((Tree.MemberOrTypeExpression) ((Tree.ParameterizedExpression) leftTerm).getPrimary()).getDeclaration();
    }
    boolean variable = decl.isVariable();
    at(op);
    String selector = naming.selector(decl, Naming.NA_SETTER);
    if (decl.isToplevel()) {
        // must use top level setter
        lhs = naming.makeName(decl, Naming.NA_FQ | Naming.NA_WRAPPER);
    } else if (Decl.isGetter(decl)) {
        if (Decl.isTransient(decl) && !decl.isVariable()) {
            JCExpression attr = gen().transformAttributeGetter(decl, rhs);
            result = at(op).Assign(naming.makeQualifiedName(lhs, decl, Naming.NA_WRAPPER), attr);
        } else {
            // must use the setter
            if (Decl.isLocal(decl)) {
                lhs = naming.makeQualifiedName(lhs, decl, Naming.NA_WRAPPER | Naming.NA_SETTER);
            } else if (decl.isStaticallyImportable()) {
                lhs = naming.makeTypeDeclarationExpression(null, (TypeDeclaration) decl.getContainer(), DeclNameFlag.QUALIFIED);
            }
        }
    } else if (decl instanceof Function && Decl.isDeferred(decl)) {
        if (Decl.isLocal(decl)) {
            // Deferred method initialization of a local function
            // The Callable field has the same name as the method, so use NA_MEMBER
            result = at(op).Assign(naming.makeQualifiedName(lhs, decl, Naming.NA_WRAPPER_UNQUOTED | Naming.NA_MEMBER), rhs);
        } else {
            // Deferred method initialization of a class function
            result = at(op).Assign(naming.makeQualifiedName(lhs, decl, Naming.NA_MEMBER), rhs);
        }
    } else if ((variable || decl.isLate()) && (Decl.isClassAttribute(decl))) {
        // must use the setter, nothing to do, unless it's a java field
        if (Decl.isJavaField(decl)) {
            if (decl.isStaticallyImportable()) {
                // static field
                result = at(op).Assign(naming.makeName(decl, Naming.NA_FQ | Naming.NA_WRAPPER_UNQUOTED), rhs);
            } else {
                // normal field
                result = at(op).Assign(naming.makeQualifiedName(lhs, decl, Naming.NA_IDENT), rhs);
            }
        }
    } else if (variable && (decl.isCaptured() || decl.isShared())) {
        // must use the qualified setter
        if (Decl.isBoxedVariable(decl)) {
            result = at(op).Assign(naming.makeName(decl, Naming.NA_Q_LOCAL_INSTANCE | Naming.NA_MEMBER | Naming.NA_SETTER), rhs);
        } else if (Decl.isLocalNotInitializer(decl)) {
            lhs = naming.makeQualifiedName(lhs, decl, Naming.NA_WRAPPER);
        } else if (isWithinSuperInvocation() && decl.isCaptured() && decl.isVariable()) {
            lhs = naming.makeUnquotedIdent(Naming.getAliasedParameterName(((Value) decl).getInitializerParameter()));
            result = at(op).Assign(lhs, rhs);
        }
    } else {
        result = at(op).Assign(naming.makeQualifiedName(lhs, decl, Naming.NA_IDENT), rhs);
    }
    if (result == null) {
        result = make().Apply(List.<JCTree.JCExpression>nil(), makeQualIdent(lhs, selector), List.<JCTree.JCExpression>of(rhs));
    }
    return result;
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Function(com.redhat.ceylon.model.typechecker.model.Function) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 52 with TypedDeclaration

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

the class ExpressionTransformer method transformAssignment.

private JCExpression transformAssignment(Node op, Tree.Term leftTerm, Tree.Term rightTerm) {
    // Remember and disable inStatement for RHS
    boolean tmpInStatement = inStatement;
    inStatement = false;
    // FIXME: can this be anything else than a Tree.MemberOrTypeExpression or Tree.ParameterizedExpression?
    final JCExpression rhs;
    BoxingStrategy boxing;
    if (leftTerm instanceof Tree.MemberOrTypeExpression) {
        TypedDeclaration decl = (TypedDeclaration) ((Tree.MemberOrTypeExpression) leftTerm).getDeclaration();
        boxing = CodegenUtil.getBoxingStrategy(decl);
        if (decl instanceof Value) {
            Value val = (Value) decl;
            if (val.getSetter() != null && val.getSetter().getUnboxed() != null) {
                boxing = CodegenUtil.getBoxingStrategy(val.getSetter());
            }
        }
        Type targetType = tmpInStatement ? leftTerm.getTypeModel() : rightTerm.getTypeModel();
        // if we're dealing with widening do not trust the type of the declaration and get the real type
        if (CodegenUtil.hasUntrustedType(decl)) {
            TypedReference typedRef = (TypedReference) ((Tree.MemberOrTypeExpression) leftTerm).getTarget();
            TypedReference nonWideningTypedRef = nonWideningTypeDecl(typedRef);
            targetType = nonWideningType(typedRef, nonWideningTypedRef);
        }
        rhs = transformExpression(rightTerm, boxing, targetType, decl.hasUncheckedNullType() ? EXPR_TARGET_ACCEPTS_NULL : 0);
    } else {
        // instanceof Tree.ParameterizedExpression
        boxing = CodegenUtil.getBoxingStrategy(leftTerm);
        Tree.ParameterizedExpression paramExpr = (Tree.ParameterizedExpression) leftTerm;
        FunctionOrValue decl = (FunctionOrValue) ((Tree.MemberOrTypeExpression) paramExpr.getPrimary()).getDeclaration();
        CallableBuilder callableBuilder = CallableBuilder.anonymous(gen(), paramExpr, decl, (Tree.Expression) rightTerm, paramExpr.getParameterLists(), paramExpr.getPrimary().getTypeModel(), decl instanceof Function ? !((Function) decl).isDeferred() : false);
        rhs = callableBuilder.build();
    }
    if (tmpInStatement) {
        return transformAssignment(op, leftTerm, rhs);
    } else {
        Type valueType = rightTerm.getTypeModel();
        if (isNull(valueType))
            valueType = leftTerm.getTypeModel();
        return transformAssignAndReturnOperation(op, leftTerm, boxing == BoxingStrategy.BOXED, leftTerm.getTypeModel(), valueType, new AssignAndReturnOperationFactory() {

            @Override
            public JCExpression getNewValue(JCExpression previousValue) {
                return rhs;
            }
        });
    }
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) FieldValue(com.redhat.ceylon.model.loader.model.FieldValue) Value(com.redhat.ceylon.model.typechecker.model.Value) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue)

Aggregations

TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)52 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)28 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)26 Type (com.redhat.ceylon.model.typechecker.model.Type)26 Function (com.redhat.ceylon.model.typechecker.model.Function)23 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)17 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)17 Class (com.redhat.ceylon.model.typechecker.model.Class)15 Value (com.redhat.ceylon.model.typechecker.model.Value)14 JCTree (com.sun.tools.javac.tree.JCTree)14 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)13 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)12 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)11 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)10 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)10 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)9 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)9 Interface (com.redhat.ceylon.model.typechecker.model.Interface)8 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)8 AttributeDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)7