Search in sources :

Example 6 with Functional

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

the class AbstractTransformer method nonWideningType.

Type nonWideningType(TypedReference declaration, TypedReference refinedDeclaration) {
    final Reference pr;
    if (declaration.equals(refinedDeclaration)) {
        pr = declaration;
    } else {
        Type refinedType = refinedDeclaration.getType();
        // since it may have changed name
        if (refinedType.getDeclaration() instanceof TypeParameter && refinedType.getDeclaration().getContainer() instanceof Function) {
            // find its index in the refined declaration
            TypeParameter refinedTypeParameter = (TypeParameter) refinedType.getDeclaration();
            Function refinedMethod = (Function) refinedTypeParameter.getContainer();
            int i = 0;
            for (TypeParameter tp : refinedMethod.getTypeParameters()) {
                if (tp.getName().equals(refinedTypeParameter.getName()))
                    break;
                i++;
            }
            if (i >= refinedMethod.getTypeParameters().size()) {
                throw new BugException("can't find type parameter " + refinedTypeParameter.getName() + " in its container " + refinedMethod.getName());
            }
            // the refining method type parameter should be at the same index
            if (declaration.getDeclaration() instanceof Function == false)
                throw new BugException("refining declaration is not a method: " + declaration);
            Function refiningMethod = (Function) declaration.getDeclaration();
            if (i >= refiningMethod.getTypeParameters().size()) {
                throw new BugException("refining method does not have enough type parameters to refine " + refinedMethod.getName());
            }
            pr = refiningMethod.getTypeParameters().get(i).getType();
        } else {
            pr = refinedType;
        }
    }
    if (pr.getDeclaration() instanceof Functional && Decl.isMpl((Functional) pr.getDeclaration())) {
        // the innermost Callable.
        return getReturnTypeOfCallable(pr.getFullType());
    }
    return pr.getType();
}
Also used : Functional(com.redhat.ceylon.model.typechecker.model.Functional) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Reference(com.redhat.ceylon.model.typechecker.model.Reference) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference)

Example 7 with Functional

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

the class ClassOrPackageDoc method writeParameters.

protected final void writeParameters(Declaration decl) throws IOException {
    if (decl instanceof Functional) {
        Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> parametersAssertions = getParametersAssertions(decl);
        boolean first = true;
        List<ParameterList> parameterLists = ((Functional) decl).getParameterLists();
        for (ParameterList parameterList : parameterLists) {
            for (Parameter parameter : parameterList.getParameters()) {
                ParameterDocData parameterDocData = getParameterDocData(parameter, parametersAssertions);
                if (!parameterDocData.isEmpty()) {
                    if (first) {
                        first = false;
                        open("div class='parameters section'");
                        around("span class='title'", "Parameters: ");
                        open("ul");
                    }
                    open("li");
                    open("code");
                    around("span class='parameter' id='" + decl.getName() + "-" + parameter.getName() + "'", parameter.getName());
                    // if parameter is function, we need to produce links to its parameters
                    if (parameter.getModel() instanceof Function) {
                        writeParameterLinksIfRequired((Function) parameter.getModel(), false, decl.getName() + "-");
                    }
                    if (!isEmpty(parameterDocData.defaultValue)) {
                        around("span class='parameter-default-value' title='Parameter default value'", " = " + parameterDocData.defaultValue);
                    }
                    close("code");
                    if (!isEmpty(parameterDocData.doc)) {
                        around("div class='doc section'", parameterDocData.doc);
                    }
                    writeParameterAssertions(decl, parameterDocData.parameterAssertions);
                    close("li");
                }
            }
        }
        if (!first) {
            close("ul");
            close("div");
        }
    }
}
Also used : Functional(com.redhat.ceylon.model.typechecker.model.Functional) Function(com.redhat.ceylon.model.typechecker.model.Function) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with Functional

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

the class ClassOrPackageDoc method doc.

protected final void doc(String name, Declaration d) throws IOException {
    String declarationName = Util.getDeclarationName(d);
    boolean alias = Util.nullSafeCompare(name, declarationName) != 0;
    // put the id on the td because IE8 doesn't support id attributes on tr (yeah right)
    open("tr");
    open("td id='" + name + "' nowrap");
    writeIcon(d);
    if (!(d instanceof Constructor)) {
        around("code class='decl-label'", name);
        close("td");
        open("td");
    }
    writeLinkOneSelf(d);
    if (alias) {
        writeTagged(d);
        writeAlias(d);
    } else {
        writeLinkSource(d);
        writeTagged(d);
        if (d instanceof Functional) {
            writeParameterLinksIfRequired((Functional) d);
        }
        open("code class='signature'");
        around("span class='modifiers'", getModifiers(d));
        write(" ");
        if (!ModelUtil.isConstructor(d)) {
            if (d instanceof Functional && ((Functional) d).isDeclaredVoid()) {
                around("span class='void'", "void");
            } else if (d instanceof TypedDeclaration) {
                linkRenderer().to(((TypedDeclaration) d).getType()).useScope(d).write();
            } else {
                linkRenderer().to(d).useScope(d).write();
            }
        }
        write(" ");
        open("span class='identifier'");
        write(name);
        close("span");
        if (isConstantValue(d)) {
            writeConstantValue((Value) d);
        }
        if (d instanceof Generic) {
            Generic f = (Generic) d;
            writeTypeParameters(f.getTypeParameters(), d);
        }
        if (d instanceof Functional) {
            writeParameterList((Functional) d, d);
        }
        if (d instanceof Generic) {
            Generic f = (Generic) d;
            writeTypeParametersConstraints(f.getTypeParameters(), d);
        }
        if (d instanceof Value) {
            Setter setter = ((Value) d).getSetter();
            if (setter != null && Util.getAnnotation(setter.getUnit(), setter.getAnnotations(), "doc") != null) {
                tool.warningSetterDoc(d.getQualifiedNameString(), d);
            }
        }
        close("code");
        writeDescription(d);
    }
    close("td");
    close("tr");
}
Also used : Functional(com.redhat.ceylon.model.typechecker.model.Functional) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Generic(com.redhat.ceylon.model.typechecker.model.Generic) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Setter(com.redhat.ceylon.model.typechecker.model.Setter)

Example 9 with Functional

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

the class ClassOrPackageDoc method getParametersAssertions.

private Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> getParametersAssertions(final Declaration decl) {
    final Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> parametersAssertions = new LinkedHashMap<Parameter, Map<Tree.Assertion, List<Tree.Condition>>>();
    if (((Functional) decl).getParameterLists().isEmpty()) {
        return parametersAssertions;
    }
    Node node = tool.getNode(decl);
    PhasedUnit pu = tool.getUnit(decl);
    if (node == null || pu == null) {
        return parametersAssertions;
    }
    Tree.Body body = null;
    if (node instanceof Tree.MethodDefinition) {
        body = ((Tree.MethodDefinition) node).getBlock();
    } else if (node instanceof Tree.ClassDefinition) {
        body = ((Tree.ClassDefinition) node).getClassBody();
    }
    if (body == null) {
        return parametersAssertions;
    }
    final Map<String, Parameter> parametersNames = new HashMap<String, Parameter>();
    for (ParameterList parameterList : ((Functional) decl).getParameterLists()) {
        for (Parameter parameter : parameterList.getParameters()) {
            parametersNames.put(parameter.getName(), parameter);
        }
    }
    body.visitChildren(new Visitor() {

        private boolean stop = false;

        private Tree.Assertion assertion = null;

        private Set<Parameter> referencedParameters = new HashSet<Parameter>();

        @Override
        public void visit(Tree.Assertion that) {
            assertion = that;
            super.visit(that);
            assertion = null;
        }

        @Override
        public void visit(Tree.Condition that) {
            referencedParameters.clear();
            super.visit(that);
            if (assertion != null && !referencedParameters.isEmpty()) {
                for (Parameter referencedParameter : referencedParameters) {
                    Map<Tree.Assertion, List<Tree.Condition>> parameterAssertions = parametersAssertions.get(referencedParameter);
                    if (parameterAssertions == null) {
                        parameterAssertions = new LinkedHashMap<Tree.Assertion, List<Tree.Condition>>();
                        parametersAssertions.put(referencedParameter, parameterAssertions);
                    }
                    List<Tree.Condition> parameterConditions = parameterAssertions.get(assertion);
                    if (parameterConditions == null) {
                        parameterConditions = new ArrayList<Tree.Condition>();
                        parameterAssertions.put(assertion, parameterConditions);
                    }
                    parameterConditions.add(that);
                }
            }
        }

        @Override
        public void visit(Tree.BaseMemberExpression that) {
            if (assertion != null) {
                Declaration d = that.getDeclaration();
                Scope realScope = com.redhat.ceylon.model.typechecker.model.ModelUtil.getRealScope(d.getScope());
                if (parametersNames.containsKey(d.getName()) && realScope == decl) {
                    referencedParameters.add(parametersNames.get(d.getName()));
                }
            }
            super.visit(that);
        }

        @Override
        public void visit(Tree.Statement that) {
            if (assertion == null) {
                stop = true;
            }
            super.visit(that);
        }

        @Override
        public void visitAny(Node that) {
            if (!stop) {
                super.visitAny(that);
            }
        }
    });
    return parametersAssertions;
}
Also used : Visitor(com.redhat.ceylon.compiler.typechecker.tree.Visitor) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(com.redhat.ceylon.compiler.typechecker.tree.Node) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PhasedUnit(com.redhat.ceylon.compiler.typechecker.context.PhasedUnit) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) ArrayList(java.util.ArrayList) List(java.util.List) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) Util.findBottomMostRefinedDeclaration(com.redhat.ceylon.ceylondoc.Util.findBottomMostRefinedDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) HashSet(java.util.HashSet) Functional(com.redhat.ceylon.model.typechecker.model.Functional) Scope(com.redhat.ceylon.model.typechecker.model.Scope) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with Functional

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

the class UnknownTypeCollector method visit.

public void visit(Tree.BaseMemberOrTypeExpression that) {
    super.visit(that);
    Declaration declaration = that.getDeclaration();
    if (declaration == null)
        return;
    if (declaration instanceof Functional) {
        Functional m = (Functional) declaration;
        collectUnknownTypes(m.getType());
        for (ParameterList pl : m.getParameterLists()) {
            for (Parameter p : pl.getParameters()) {
                collectUnknownTypes(p.getType());
            }
        }
    } else if (declaration instanceof Value) {
        Value v = (Value) declaration;
        collectUnknownTypes(v.getType());
    }
}
Also used : Functional(com.redhat.ceylon.model.typechecker.model.Functional) Value(com.redhat.ceylon.model.typechecker.model.Value) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Aggregations

Functional (com.redhat.ceylon.model.typechecker.model.Functional)17 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)9 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)9 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)8 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)7 Function (com.redhat.ceylon.model.typechecker.model.Function)7 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)7 ParameterList (com.redhat.ceylon.model.typechecker.model.ParameterList)6 Type (com.redhat.ceylon.model.typechecker.model.Type)6 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)5 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)5 Value (com.redhat.ceylon.model.typechecker.model.Value)5 Class (com.redhat.ceylon.model.typechecker.model.Class)4 JCTree (com.sun.tools.javac.tree.JCTree)4 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)3 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)3 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)3 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)3 HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)2 AttributeDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)2