Search in sources :

Example 61 with Function

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

the class RefinementVisitor method visit.

@Override
public void visit(Tree.Declaration that) {
    super.visit(that);
    Declaration dec = that.getDeclarationModel();
    if (dec != null) {
        boolean mayBeShared = dec.isToplevel() || dec.isClassOrInterfaceMember();
        if (dec.isShared() && !mayBeShared) {
            that.addError("shared declaration is not a member of a class, interface, or package: " + message(dec) + " may not be annotated 'shared'", 1200);
        }
        boolean mayBeRefined = dec instanceof Value || dec instanceof Function || dec instanceof Class;
        if (!mayBeRefined) {
            checkNonrefinableDeclaration(that, dec);
        }
        boolean member = dec.isClassOrInterfaceMember() && dec.isShared() && !isConstructor(dec) && // TODO: what about nested interfaces and abstract classes?!
        !(dec instanceof TypeParameter);
        if (member) {
            checkMember(that, dec);
        } else {
            checkNonMember(that, dec);
        }
        if (dec.isNativeImplementation() || isNativeMember(dec)) {
            checkNative(that, dec);
        }
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Class(org.eclipse.ceylon.model.typechecker.model.Class) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 62 with Function

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

the class RefinementVisitor method visit.

@Override
public void visit(Tree.SpecifierStatement that) {
    super.visit(that);
    List<Type> sig = new ArrayList<Type>();
    Tree.Term term = that.getBaseMemberExpression();
    while (term instanceof Tree.ParameterizedExpression) {
        sig.clear();
        Tree.ParameterizedExpression pe = (Tree.ParameterizedExpression) term;
        Tree.TypeParameterList typeParameterList = pe.getTypeParameterList();
        if (typeParameterList != null) {
            // TODO: remove this for #1329
            typeParameterList.addError("specification statements may not have type parameters");
        }
        Tree.ParameterList pl = pe.getParameterLists().get(0);
        for (Tree.Parameter p : pl.getParameters()) {
            if (p == null) {
                sig.add(null);
            } else {
                Parameter model = p.getParameterModel();
                if (model != null) {
                    sig.add(model.getType());
                } else {
                    sig.add(null);
                }
            }
        }
        term = pe.getPrimary();
    }
    if (term instanceof Tree.BaseMemberExpression) {
        Tree.BaseMemberExpression bme = (Tree.BaseMemberExpression) term;
        Unit unit = that.getUnit();
        TypedDeclaration td = getTypedDeclaration(bme.getScope(), name(bme.getIdentifier()), sig, false, unit);
        if (td != null) {
            that.setDeclaration(td);
            Scope scope = that.getScope();
            Scope container = scope.getContainer();
            Scope realScope = getRealScope(container);
            if (realScope instanceof ClassOrInterface) {
                ClassOrInterface ci = (ClassOrInterface) realScope;
                Scope tdcontainer = td.getContainer();
                if (td.isClassOrInterfaceMember()) {
                    ClassOrInterface tdci = (ClassOrInterface) tdcontainer;
                    if (!tdcontainer.equals(realScope) && ci.inherits(tdci)) {
                        boolean lazy = that.getSpecifierExpression() instanceof Tree.LazySpecifierExpression;
                        if (!lazy && td.isVariable() && td.isJava()) {
                        // allow assignment to variable
                        // member of Java supertype
                        } else // refinement of an inherited member
                        if (tdcontainer == scope) {
                            that.addError("parameter declaration hides refining member: '" + td.getName(unit) + "' (rename parameter)");
                        } else if (td instanceof Value) {
                            refineAttribute((Value) td, bme, that, ci);
                        } else if (td instanceof Function) {
                            refineMethod((Function) td, bme, that, ci);
                        } else {
                            // TODO!
                            bme.addError("not a reference to a formal attribute: '" + td.getName(unit) + "'");
                        }
                    }
                }
            }
        }
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) AnalyzerUtil.getTypedDeclaration(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) ArrayList(java.util.ArrayList) Unit(org.eclipse.ceylon.model.typechecker.model.Unit) Function(org.eclipse.ceylon.model.typechecker.model.Function) IntersectionType(org.eclipse.ceylon.model.typechecker.model.IntersectionType) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) LazyType(org.eclipse.ceylon.model.typechecker.model.LazyType) Type(org.eclipse.ceylon.model.typechecker.model.Type) ModelUtil.erasedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.erasedType) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ModelUtil.getRealScope(org.eclipse.ceylon.model.typechecker.model.ModelUtil.getRealScope) Value(org.eclipse.ceylon.model.typechecker.model.Value) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Example 63 with Function

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

the class ExpressionVisitor method visitGenericBaseMemberReference.

private void visitGenericBaseMemberReference(Tree.StaticMemberOrTypeExpression that, TypedDeclaration member) {
    if (member instanceof Function && member.isParameterized()) {
        Function generic = (Function) member;
        Scope scope = that.getScope();
        Type outerType = scope.getDeclaringType(member);
        TypedReference target = member.appliedTypedReference(outerType, NO_TYPE_ARGS);
        that.setTarget(target);
        Type functionType = genericFunctionType(generic, scope, member, target, unit);
        that.setTypeModel(functionType);
        checkNotJvm(that, "type functions are not supported on the JVM: '" + member.getName(unit) + "' is generic (specify explicit type arguments)");
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) ModelUtil.intersectionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType) ModelUtil.unionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType) AnalyzerUtil.spreadType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.spreadType) AnalyzerUtil.getTupleType(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTupleType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) ModelUtil.appliedType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType) ModelUtil.genericFunctionType(org.eclipse.ceylon.model.typechecker.model.ModelUtil.genericFunctionType) NativeUtil.declarationScope(org.eclipse.ceylon.compiler.typechecker.util.NativeUtil.declarationScope) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) TypedReference(org.eclipse.ceylon.model.typechecker.model.TypedReference)

Example 64 with Function

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

the class ExpressionVisitor method createAnonymousFunctionParameters.

/**
 * Create the parameter list of the given
 * anonymous function with a missing argument
 * list, from the given parameter list of a
 * functional parameter to which it is assigned.
 */
private void createAnonymousFunctionParameters(List<Parameter> parameters, Tree.Term term, Tree.FunctionArgument anon) {
    Function model = anon.getDeclarationModel();
    if (anon.getParameterLists().isEmpty()) {
        Tree.ParameterList pl = new Tree.ParameterList(null);
        ParameterList mpl = new ParameterList();
        for (Parameter parameter : parameters) {
            Tree.InitializerParameter ip = new Tree.InitializerParameter(null);
            CommonToken token = new CommonToken(LIDENTIFIER, parameter.getName());
            token.setStartIndex(term.getStartIndex());
            Token tok = term.getToken();
            token.setLine(tok.getLine());
            token.setCharPositionInLine(tok.getCharPositionInLine());
            Tree.Identifier id = new Tree.Identifier(token);
            ip.setIdentifier(id);
            pl.addParameter(ip);
            ip.setUnit(unit);
            ip.setScope(model);
            id.setUnit(unit);
            id.setScope(model);
            Parameter mp = new Parameter();
            mp.setDeclaration(model);
            mp.setName(parameter.getName());
            ip.setParameterModel(mp);
            mpl.getParameters().add(mp);
            pl.setModel(mpl);
        }
        pl.setUnit(unit);
        pl.setScope(model);
        model.addParameterList(mpl);
        anon.addParameterList(pl);
    }
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) CustomTree(org.eclipse.ceylon.compiler.typechecker.tree.CustomTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) AnalyzerUtil.getUnspecifiedParameter(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getUnspecifiedParameter) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) AnalyzerUtil.getMatchingParameter(org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getMatchingParameter) CommonToken(org.antlr.runtime.CommonToken) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken)

Example 65 with Function

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

the class TreeUtil method hasUncheckedNulls.

private static boolean hasUncheckedNulls(Tree.Term term, boolean invoking) {
    if (term instanceof Tree.MemberOrTypeExpression) {
        Tree.MemberOrTypeExpression mte = (Tree.MemberOrTypeExpression) term;
        Declaration d = mte.getDeclaration();
        if (d instanceof TypedDeclaration) {
            TypedDeclaration td = (TypedDeclaration) d;
            return td.hasUncheckedNullType() && // because java method references can't be null
            (!(d instanceof Function) || invoking);
        } else {
            return false;
        }
    } else if (term instanceof Tree.QualifiedMemberOrTypeExpression) {
        Tree.QualifiedMemberOrTypeExpression qmte = (Tree.QualifiedMemberOrTypeExpression) term;
        return hasUncheckedNulls(qmte.getPrimary(), invoking);
    } else if (term instanceof Tree.InvocationExpression) {
        Tree.InvocationExpression ite = (Tree.InvocationExpression) term;
        return hasUncheckedNulls(ite.getPrimary(), true);
    } else if (term instanceof Tree.DefaultOp) {
        Tree.DefaultOp op = (Tree.DefaultOp) term;
        return hasUncheckedNulls(op.getRightTerm(), invoking);
    } else if (term instanceof Tree.Expression) {
        Tree.Expression e = (Tree.Expression) term;
        return hasUncheckedNulls(e.getTerm(), invoking);
    } else if (term instanceof Tree.LetExpression) {
        Tree.LetExpression e = (Tree.LetExpression) term;
        return hasUncheckedNulls(e.getLetClause().getExpression(), invoking);
    } else if (term instanceof Tree.IfExpression) {
        Tree.IfExpression e = (Tree.IfExpression) term;
        return hasUncheckedNulls(e.getIfClause().getExpression(), invoking) || hasUncheckedNulls(e.getElseClause().getExpression(), invoking);
    } else if (term instanceof Tree.SwitchExpression) {
        Tree.SwitchExpression e = (Tree.SwitchExpression) term;
        for (CaseClause clause : e.getSwitchCaseList().getCaseClauses()) {
            if (hasUncheckedNulls(clause.getExpression(), invoking)) {
                return true;
            }
        }
        if (e.getSwitchCaseList().getElseClause() != null) {
            return hasUncheckedNulls(e.getSwitchCaseList().getElseClause().getExpression(), invoking);
        }
        return false;
    } else {
        return false;
    }
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) CaseClause(org.eclipse.ceylon.compiler.typechecker.tree.Tree.CaseClause) Function(org.eclipse.ceylon.model.typechecker.model.Function) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Aggregations

Function (org.eclipse.ceylon.model.typechecker.model.Function)167 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)71 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)70 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)69 Type (org.eclipse.ceylon.model.typechecker.model.Type)68 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)62 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)57 Value (org.eclipse.ceylon.model.typechecker.model.Value)50 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)46 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)43 Class (org.eclipse.ceylon.model.typechecker.model.Class)39 ArrayList (java.util.ArrayList)32 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)29 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)26 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)23 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)23 TypedReference (org.eclipse.ceylon.model.typechecker.model.TypedReference)23 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)23 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)22 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)21