Search in sources :

Example 26 with Term

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.

the class AnnotationModelVisitor method defaultedParameter.

public void defaultedParameter(Tree.SpecifierOrInitializerExpression d, AnnotationConstructorParameter annotationConstructorParameter) {
    if (annotationConstructor != null) {
        Declaration t = d.getUnit().getTrueValueDeclaration();
        Declaration f = d.getUnit().getFalseValueDeclaration();
        Term term = d.getExpression().getTerm();
        if (term instanceof Tree.InvocationExpression) {
            Tree.Primary primary = ((Tree.InvocationExpression) term).getPrimary();
            if (primary instanceof Tree.BaseMemberOrTypeExpression && (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()) || isAnnotationClass(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration()))) {
                final AnnotationInvocation prevInstantiation = this.instantiation;
                this.instantiation = new AnnotationInvocation();
                if (isAnnotationConstructor(((Tree.BaseMemberOrTypeExpression) primary).getDeclaration())) {
                    Function constructor = (Function) ((Tree.BaseMemberOrTypeExpression) primary).getDeclaration();
                    instantiation.setConstructorDeclaration(constructor);
                    for (AnnotationConstructorParameter x : ((AnnotationInvocation) constructor.getAnnotationConstructor()).getConstructorParameters()) {
                        instantiation.addConstructorParameter(x);
                    }
                }
                checkingDefaults = true;
                super.visit(d);
                annotationConstructorParameter.setDefaultArgument(this.term);
                this.term = null;
                checkingDefaults = false;
                this.instantiation = prevInstantiation;
            } else {
                errorDefaultedParameter(d);
            }
        } else if (term instanceof Tree.Literal || (term instanceof Tree.NegativeOp && ((Tree.NegativeOp) term).getTerm() instanceof Tree.Literal) || (term instanceof Tree.BaseMemberExpression && (((Tree.BaseMemberExpression) term).getDeclaration().equals(t) || ((Tree.BaseMemberExpression) term).getDeclaration().equals(f) || ((Tree.BaseMemberExpression) term).getDeclaration().isParameter() || Decl.isAnonCaseOfEnumeratedType((Tree.BaseMemberExpression) term)))) {
            checkingDefaults = true;
            super.visit(d);
            annotationConstructorParameter.setDefaultArgument(this.term);
            this.term = null;
            checkingDefaults = false;
        } else if (term instanceof Tree.Tuple || term instanceof Tree.SequenceEnumeration) {
            // TODO Tuples and SequenceEnumerations of the above cases should also be allowed
            checkingDefaults = true;
            super.visit(d);
            annotationConstructorParameter.setDefaultArgument(this.term);
            this.term = null;
            checkingDefaults = false;
        } else {
            errorDefaultedParameter(d);
        }
    }
}
Also used : Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) Function(org.eclipse.ceylon.model.typechecker.model.Function) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 27 with Term

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.

the class GenerateJsVisitor method assignOp.

private void assignOp(final Tree.AssignmentOp that, final String functionName, final Map<TypeParameter, Type> targs) {
    if (errVisitor.hasErrors(that))
        return;
    Term lhs = that.getLeftTerm();
    final boolean isNative = "||".equals(functionName) || "&&".equals(functionName);
    if (lhs instanceof BaseMemberExpression) {
        BaseMemberExpression lhsBME = (BaseMemberExpression) lhs;
        Declaration lhsDecl = lhsBME.getDeclaration();
        final String getLHS = memberAccess(lhsBME, null);
        out("(");
        BmeGenerator.generateMemberAccess(lhsBME, new GenerateCallback() {

            @Override
            public void generateValue() {
                if (isNative) {
                    out(getLHS, functionName);
                } else {
                    out(getLHS, ".", functionName, "(");
                }
                if (!isNaturalLiteral(that.getRightTerm())) {
                    that.getRightTerm().visit(GenerateJsVisitor.this);
                }
                if (!isNative) {
                    if (targs != null) {
                        out(",");
                        TypeUtils.printTypeArguments(that, targs, GenerateJsVisitor.this, false, null);
                    }
                    out(")");
                }
            }
        }, null, this);
        if (!hasSimpleGetterSetter(lhsDecl)) {
            out(",", getLHS);
        }
        out(")");
    } else if (lhs instanceof QualifiedMemberExpression) {
        QualifiedMemberExpression lhsQME = (QualifiedMemberExpression) lhs;
        if (TypeUtils.isNativeJs(lhsQME)) {
            // ($1.foo = Box($1.foo).operator($2))
            final String tmp = names.createTempVariable();
            final String dec = isInDynamicBlock() && lhsQME.getDeclaration() == null ? lhsQME.getIdentifier().getText() : lhsQME.getDeclaration().getName();
            out("(", tmp, "=");
            lhsQME.getPrimary().visit(this);
            out(",", tmp, ".", dec, "=");
            int boxType = boxStart(lhsQME);
            out(tmp, ".", dec);
            if (boxType == 4)
                out("/*TODO: callable targs 8*/");
            boxUnboxEnd(boxType);
            out(".", functionName, "(");
            if (!isNaturalLiteral(that.getRightTerm())) {
                that.getRightTerm().visit(this);
            }
            out("))");
        } else {
            final String lhsPrimaryVar = createRetainedTempVar();
            final String getLHS = memberAccess(lhsQME, lhsPrimaryVar);
            out("(", lhsPrimaryVar, "=");
            lhsQME.getPrimary().visit(this);
            out(",");
            BmeGenerator.generateMemberAccess(lhsQME, new GenerateCallback() {

                @Override
                public void generateValue() {
                    out(getLHS, ".", functionName, "(");
                    Tree.Term term = that.getRightTerm();
                    if (!isNaturalLiteral(term)) {
                        term.visit(GenerateJsVisitor.this);
                    }
                    out(")");
                }
            }, lhsPrimaryVar, this);
            if (!hasSimpleGetterSetter(lhsQME.getDeclaration())) {
                out(",", getLHS);
            }
            out(")");
        }
    } else if (lhs instanceof Tree.IndexExpression) {
        lhs.addUnsupportedError("Index expressions are not supported in this kind of assignment.");
    }
}
Also used : QualifiedMemberExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.QualifiedMemberExpression) Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) BaseMemberExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.BaseMemberExpression) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Example 28 with Term

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.

the class GenerateJsVisitor method boxUnboxStart.

int boxUnboxStart(final Tree.Term fromTerm, boolean toNative) {
    // Box the value
    final Type fromType = fromTerm.getTypeModel();
    if (fromType == null) {
        return 0;
    }
    final boolean fromNative = TypeUtils.isNativeJs(fromTerm);
    if (fromNative != toNative || fromType.isCallable()) {
        if (fromNative) {
            // conversion from native value to Ceylon value
            if (fromType.isInteger() || fromType.isFloat()) {
                out("(");
            } else if (fromType.isBoolean()) {
                out("(");
            } else if (fromType.isCharacter()) {
                out(getClAlias(), "Character(");
            } else if (fromType.isCallable()) {
                out(getClAlias(), "jsc$2(");
                return 4;
            } else {
                return 0;
            }
            return 1;
        } else if (fromType.isFloat()) {
            // conversion from Ceylon Float to native value
            return toNative ? 2 : 1;
        } else if (fromType.isCallable()) {
            Term _t = fromTerm;
            if (_t instanceof Tree.InvocationExpression) {
                _t = ((Tree.InvocationExpression) _t).getPrimary();
            }
            // Don't box callables if they're not members or anonymous
            if (_t instanceof Tree.MemberOrTypeExpression) {
                final Declaration d = ((Tree.MemberOrTypeExpression) _t).getDeclaration();
                if (d != null && !(d.isClassOrInterfaceMember() || d.isAnonymous())) {
                    return 0;
                }
            }
            out(getClAlias(), "jsc$2(");
            return 4;
        } else {
            return 3;
        }
    }
    return 0;
}
Also used : IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ExtendedType(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExtendedType) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) StaticMemberOrTypeExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.StaticMemberOrTypeExpression) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Example 29 with Term

use of org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term in project ceylon by eclipse.

the class GenerateJsVisitor method generateIsOfType.

/**
 * Generates js code to check if a term is of a certain type. We solve this in JS by
 * checking against all types that Type satisfies (in the case of union types, matching any
 * type will do, and in case of intersection types, all types must be matched).
 * @param term The term that is to be checked against a type
 * @param termString (optional) a string to be used as the term to be checked
 * @param type The type to check against
 * @param tmpvar (optional) a variable to which the term is assigned
 * @param negate If true, negates the generated condition
 */
void generateIsOfType(Node term, String termString, final Type type, String tmpvar, final boolean negate, final boolean coerceDynamic) {
    if (negate) {
        out("!");
    }
    out(getClAlias(), "is$(");
    if (isInDynamicBlock() && coerceDynamic) {
        out(getClAlias(), "dre$$(");
    }
    if (term instanceof Term) {
        conds.specialConditionRHS((Term) term, tmpvar);
    } else {
        conds.specialConditionRHS(termString, tmpvar);
    }
    if (isInDynamicBlock() && coerceDynamic) {
        out(",");
        TypeUtils.typeNameOrList(term, type, this, false);
        out(",false)");
    }
    out(",");
    TypeUtils.typeNameOrList(term, type, this, false);
    if (type.getQualifyingType() != null) {
        Type outer = type.getQualifyingType();
        boolean first = true;
        while (outer != null) {
            if (first) {
                out(",[");
                first = false;
            } else {
                out(",");
            }
            TypeUtils.typeNameOrList(term, outer, this, false);
            outer = outer.getQualifyingType();
        }
        if (!first) {
            out("]");
        }
    } else if (type.getDeclaration() != null) {
        Scope container = type.getDeclaration().getContainer();
        if (container != null) {
            Declaration d = ModelUtil.getContainingDeclarationOfScope(container);
            if (d != null && d instanceof Function && !((Function) d).getTypeParameters().isEmpty()) {
                out(",", names.typeArgsParamName((Function) d));
            }
        }
    }
    out(")");
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ExtendedType(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExtendedType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Aggregations

Term (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term)29 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)27 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)14 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)11 Type (org.eclipse.ceylon.model.typechecker.model.Type)11 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)11 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)10 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)10 Expression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.Expression)6 Function (org.eclipse.ceylon.model.typechecker.model.Function)6 UnionType (org.eclipse.ceylon.model.typechecker.model.UnionType)6 AttributeDeclaration (org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)5 TreeUtil.unwrapExpressionUntilTerm (org.eclipse.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm)5 BaseMemberExpression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.BaseMemberExpression)4 ExtendedType (org.eclipse.ceylon.compiler.typechecker.tree.Tree.ExtendedType)4 LazySpecifierExpression (org.eclipse.ceylon.compiler.typechecker.tree.Tree.LazySpecifierExpression)4 IntersectionType (org.eclipse.ceylon.model.typechecker.model.IntersectionType)4 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)3 AssignmentOperatorTranslation (org.eclipse.ceylon.compiler.java.codegen.Operators.AssignmentOperatorTranslation)3 OperatorTranslation (org.eclipse.ceylon.compiler.java.codegen.Operators.OperatorTranslation)3