Search in sources :

Example 56 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class ExpressionTransformer method isReferenceInSameScope.

private boolean isReferenceInSameScope(Tree.StaticMemberOrTypeExpression expr) {
    if (isWithinSyntheticClassBody()) {
        return false;
    }
    Declaration decl = expr.getDeclaration();
    Scope s = expr.getScope();
    // are we in the same Declaration container?
    while (s != null && s instanceof Declaration == false) {
        s = s.getContainer();
    }
    return Decl.equalScopeDecl(s, decl);
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 57 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

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) {
                        if (((Class) decl).isByte()) {
                            at(ce);
                            try {
                                long value = literalValue((Tree.NaturalLiteral) term).longValue();
                                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(org.eclipse.ceylon.compiler.typechecker.tree.Tree.PositionalArgument) Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) TreeUtil.unwrapExpressionUntilTerm(org.eclipse.ceylon.compiler.typechecker.tree.TreeUtil.unwrapExpressionUntilTerm) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 58 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class ExpressionTransformer method makeTopLevelValueOrFunctionLiteral.

private JCTree makeTopLevelValueOrFunctionLiteral(Tree.MemberLiteral expr) {
    Declaration declaration = expr.getDeclaration();
    JCExpression toplevelCall = makeTopLevelValueOrFunctionDeclarationLiteral(declaration);
    if (!expr.getWantsDeclaration()) {
        ListBuffer<JCExpression> closedTypeArgs = new ListBuffer<JCExpression>();
        // expr is of type Function<Type,Arguments> or Value<Get,Set> so we can get its type like that
        JCExpression reifiedType = makeReifiedTypeArgument(expr.getTypeModel().getTypeArgumentList().get(0));
        closedTypeArgs.append(reifiedType);
        if (Decl.isMethod(declaration)) {
            // expr is of type Function<Type,Arguments> so we can get its arguments type like that
            Type argumentsType = typeFact().getCallableTuple(expr.getTypeModel());
            JCExpression reifiedArguments = makeReifiedTypeArgument(argumentsType);
            closedTypeArgs.append(reifiedArguments);
            if (expr.getTypeArgumentList() != null) {
                java.util.List<Type> typeModels = expr.getTypeArgumentList().getTypeModels();
                if (typeModels != null) {
                    JCExpression closedTypesExpr = getClosedTypesSequential(typeModels);
                    // must apply it
                    closedTypeArgs.append(closedTypesExpr);
                }
            }
        } else {
            JCExpression reifiedSet;
            Type ptype;
            if (!((Value) declaration).isVariable())
                ptype = typeFact().getNothingType();
            else
                ptype = expr.getTypeModel().getTypeArgumentList().get(0);
            reifiedSet = makeReifiedTypeArgument(ptype);
            closedTypeArgs.append(reifiedSet);
        }
        toplevelCall = make().Apply(null, makeSelect(toplevelCall, "apply"), closedTypeArgs.toList());
        // add cast
        Type exprType = expr.getTypeModel().resolveAliases();
        JCExpression typeClass = makeJavaType(exprType, JT_NO_PRIMITIVES);
        JCExpression rawTypeClass = makeJavaType(exprType, JT_NO_PRIMITIVES | JT_RAW);
        return make().TypeCast(typeClass, make().TypeCast(rawTypeClass, toplevelCall));
    }
    return toplevelCall;
}
Also used : UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 59 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class ExpressionTransformer method appendImplicitArguments.

private void appendImplicitArguments(Invocation invocation, TransformedInvocationPrimary transformedPrimary, ListBuffer<ExpressionAndType> result) {
    // Implicit arguments
    // except for Java array constructors
    Declaration primaryDeclaration = invocation.getPrimaryDeclaration();
    Tree.Term primary = invocation.getPrimary();
    if (primaryDeclaration instanceof Value == false) {
        if (primaryDeclaration instanceof Class == false || !isJavaArray(((Class) primaryDeclaration).getType())) {
            invocation.addReifiedArguments(result);
        }
    }
    if (!(primary instanceof Tree.BaseTypeExpression) && !(primary instanceof Tree.QualifiedTypeExpression) && (!(primary instanceof Tree.QualifiedMemberExpression) || !(((Tree.QualifiedMemberExpression) primary).getMemberOperator() instanceof Tree.SpreadOp)) && Invocation.onValueType(this, primary, primaryDeclaration) && transformedPrimary != null) {
        result.add(new ExpressionAndType(transformedPrimary.expr, makeJavaType(primary.getTypeModel())));
    }
}
Also used : Term(org.eclipse.ceylon.compiler.typechecker.tree.Tree.Term) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 60 with Declaration

use of org.eclipse.ceylon.model.typechecker.model.Declaration in project ceylon by eclipse.

the class ExpressionTransformer method needDollarThis.

boolean needDollarThis(Tree.StaticMemberOrTypeExpression expr) {
    if (expr instanceof Tree.BaseMemberExpression || expr instanceof Tree.BaseTypeExpression) {
        // We need to add a `$this` prefix to the member expression if:
        // * The member was declared on an interface I and
        // * The member is being used in the companion class of I or
        // // REMOVED: some subinterface of I, and
        // some member type of I, and
        // * The member is shared (non-shared means its only on the companion class)
        // FIXME: https://github.com/ceylon/ceylon-compiler/issues/1019
        final Declaration decl = expr.getDeclaration();
        if (!decl.isInterfaceMember())
            return false;
        // Find the method/getter/setter where the expr is being used
        Scope scope = expr.getScope();
        while (scope != null) {
            // Is it being used in an interface (=> impl)
            if (scope instanceof Interface && ((Interface) scope).getType().isSubtypeOf(scope.getDeclaringType(decl))) {
                return decl.isShared();
            }
            scope = scope.getContainer();
        }
    }
    return false;
}
Also used : Scope(org.eclipse.ceylon.model.typechecker.model.Scope) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Aggregations

Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)370 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)309 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)264 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)129 Type (org.eclipse.ceylon.model.typechecker.model.Type)100 Class (org.eclipse.ceylon.model.typechecker.model.Class)78 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)74 Value (org.eclipse.ceylon.model.typechecker.model.Value)73 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)72 Function (org.eclipse.ceylon.model.typechecker.model.Function)71 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)71 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)63 ArrayList (java.util.ArrayList)61 AnalyzerUtil.getPackageTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypeDeclaration)60 AnalyzerUtil.getTypeDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypeDeclaration)60 ModelUtil.getNativeDeclaration (org.eclipse.ceylon.model.typechecker.model.ModelUtil.getNativeDeclaration)57 AnalyzerUtil.getPackageTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getPackageTypedDeclaration)51 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)50 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)48 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)45