Search in sources :

Example 61 with Parameter

use of org.eclipse.ceylon.model.typechecker.model.Parameter 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 62 with Parameter

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

the class TreeUtil method buildAnnotations.

public static void buildAnnotations(Tree.AnnotationList al, List<Annotation> annotations) {
    if (al != null) {
        Tree.AnonymousAnnotation aa = al.getAnonymousAnnotation();
        if (aa != null) {
            Annotation ann = new Annotation();
            ann.setName("doc");
            Tree.StringLiteral lit = aa.getStringLiteral();
            if (lit != null) {
                String text = lit.getText();
                ann.addPositionalArgument(text);
                annotations.add(ann);
            }
        }
        for (Tree.Annotation a : al.getAnnotations()) {
            Annotation ann = new Annotation();
            Tree.BaseMemberExpression bma = (Tree.BaseMemberExpression) a.getPrimary();
            String name = bma.getIdentifier().getText();
            ann.setName(name);
            Tree.NamedArgumentList nal = a.getNamedArgumentList();
            if (nal != null) {
                for (Tree.NamedArgument na : nal.getNamedArguments()) {
                    if (na instanceof Tree.SpecifiedArgument) {
                        Tree.SpecifiedArgument sa = (Tree.SpecifiedArgument) na;
                        Tree.SpecifierExpression sie = sa.getSpecifierExpression();
                        Tree.Expression e = sie.getExpression();
                        if (e != null) {
                            Tree.Term t = e.getTerm();
                            Parameter p = sa.getParameter();
                            if (p != null) {
                                String text = toString(t);
                                if (text != null) {
                                    ann.addNamedArgument(p.getName(), text);
                                }
                            }
                        }
                    }
                }
            }
            Tree.PositionalArgumentList pal = a.getPositionalArgumentList();
            if (pal != null) {
                for (Tree.PositionalArgument pa : pal.getPositionalArguments()) {
                    if (pa instanceof Tree.ListedArgument) {
                        Tree.ListedArgument la = (Tree.ListedArgument) pa;
                        Tree.Term t = la.getExpression().getTerm();
                        String text = toString(t);
                        if (text != null) {
                            ann.addPositionalArgument(text);
                        }
                    }
                }
            }
            annotations.add(ann);
        }
    }
}
Also used : Annotation(org.eclipse.ceylon.model.typechecker.model.Annotation) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter)

Example 63 with Parameter

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

the class PrintVisitor method print.

private void print(Node node) {
    print(node.getText());
    print(" [" + node.getNodeType() + "]");
    if (node.getToken() != null) {
        print(" (" + node.getLocation() + ")");
    }
    if (node instanceof Tree.Term) {
        Type type = ((Tree.Term) node).getTypeModel();
        if (type != null) {
            print(" : " + type.asString() + "");
        }
    }
    if (node instanceof Tree.ComprehensionClause) {
        Type type = ((Tree.ComprehensionClause) node).getTypeModel();
        if (type != null) {
            print(" : " + type.asString() + "");
        }
    }
    if (node instanceof Tree.Type) {
        Type type = ((Tree.Type) node).getTypeModel();
        if (type != null) {
            print(" : " + type.asString() + "");
        }
    }
    if (node instanceof Tree.TypeArguments) {
        List<Type> types = ((Tree.TypeArguments) node).getTypeModels();
        if (types != null && !types.isEmpty()) {
            print(" : <");
            int i = 0;
            for (Type pt : types) {
                if (pt != null) {
                    print(pt.asString());
                }
                if (++i != types.size()) {
                    print(", ");
                }
            }
            print(">");
        }
    }
    if (node instanceof Tree.MemberOrTypeExpression) {
        Reference t = ((Tree.MemberOrTypeExpression) node).getTarget();
        Declaration d = ((Tree.MemberOrTypeExpression) node).getDeclaration();
        if (t != null) {
            print(" : " + t.asString() + "");
        }
        if (d != null) {
            print(" : " + d);
        }
    }
    if (node instanceof Tree.Outer) {
        Declaration d = ((Tree.Outer) node).getDeclarationModel();
        if (d != null) {
            print(" : " + d);
        }
    }
    if (node instanceof Tree.SelfExpression) {
        Declaration d = ((Tree.SelfExpression) node).getDeclarationModel();
        if (d != null) {
            print(" : " + d);
        }
    }
    if (node instanceof Tree.Declaration) {
        Declaration d = ((Tree.Declaration) node).getDeclarationModel();
        if (d != null) {
            if (d.isCaptured() || d.isJsCaptured()) {
                print("[captured]");
            }
            print(" : " + d);
            Declaration rd = d.getRefinedDeclaration();
            if (rd != null && !rd.equals(d)) {
                Declaration container = (Declaration) rd.getContainer();
                print(" (refines " + container.getName() + "." + rd.getName() + ")");
            }
        }
    }
    if (node instanceof Tree.SpecifierStatement) {
        Declaration d = ((Tree.SpecifierStatement) node).getDeclaration();
        if (d != null) {
            print(" : " + d);
        }
        if (((Tree.SpecifierStatement) node).getRefinement()) {
            Declaration rd = d.getRefinedDeclaration();
            if (rd != null && !rd.equals(d)) {
                Declaration container = (Declaration) rd.getContainer();
                print(" (refines " + container.getName() + "." + rd.getName() + ")");
            }
        }
    }
    if (node instanceof Tree.SimpleType) {
        Declaration d = ((Tree.SimpleType) node).getDeclarationModel();
        if (d != null) {
            print(" : " + d);
        }
    }
    if (node instanceof Tree.ImportMemberOrType) {
        Declaration d = ((Tree.ImportMemberOrType) node).getDeclarationModel();
        if (d != null) {
            print(" : " + d);
        }
    }
    if (node instanceof Tree.Return) {
        Declaration d = ((Tree.Return) node).getDeclaration();
        if (d != null) {
            print(" : " + d);
        }
    }
    if (node instanceof Tree.PositionalArgument) {
        Parameter p = ((Tree.PositionalArgument) node).getParameter();
        if (p != null) {
            print(" : " + p);
        }
    }
    if (node instanceof Tree.NamedArgument) {
        Parameter p = ((Tree.NamedArgument) node).getParameter();
        if (p != null) {
            print(" : " + p);
        }
    }
    if (node instanceof Tree.SequencedArgument) {
        Parameter p = ((Tree.SequencedArgument) node).getParameter();
        if (p != null) {
            print(" : " + p);
        }
    }
    if (!node.getErrors().isEmpty()) {
        String icon = " [!]";
        for (Message e : node.getErrors()) {
            if (!(e instanceof UsageWarning)) {
                icon = " [X]";
            }
        }
        print(icon + node.getErrors());
    }
}
Also used : Message(org.eclipse.ceylon.compiler.typechecker.tree.Message) Reference(org.eclipse.ceylon.model.typechecker.model.Reference) Type(org.eclipse.ceylon.model.typechecker.model.Type) UsageWarning(org.eclipse.ceylon.compiler.typechecker.analyzer.UsageWarning) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Example 64 with Parameter

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

the class ValueDeclarationImpl method $getJavaAnnotations$.

@Override
@Ignore
public java.lang.annotation.Annotation[] $getJavaAnnotations$() {
    Class<?> javaClass = Metamodel.getJavaClass(declaration);
    ArrayList<java.lang.annotation.Annotation> result = new ArrayList<>();
    HashSet<Class<? extends java.lang.annotation.Annotation>> cls = new HashSet<>();
    if (javaClass != null) {
        // FIXME: pretty sure this doesn't work with interop and fields
        Method declaredGetter = Reflection.getDeclaredGetter(javaClass, NamingBase.getGetterName(declaration));
        if (declaredGetter != null) {
            addToList(result, cls, declaredGetter.getAnnotations());
        }
        if (!((Value) declaration).isTransient()) {
            // TODO only include those which are java annotations
            Field field = Reflection.getDeclaredField(javaClass, declaration.getName());
            if (field != null) {
                Annotation[] fieldAnnos = field.getAnnotations();
                addToList(result, cls, fieldAnnos);
            }
            Method setter = Reflection.getDeclaredSetter(javaClass, NamingBase.getSetterName(declaration.getName()));
            if (setter != null) {
                Annotation[] setterAnnos = setter.getAnnotations();
                addToList(result, cls, setterAnnos);
            }
        }
    }
    // one last chance
    if (parameter != null && !parameter.getModel().isShared()) {
        // get the annotations from the parameter itself
        Annotation[][] parameterAnnotations;
        Scope container = parameter.getModel().getContainer();
        if (container instanceof org.eclipse.ceylon.model.typechecker.model.Function) {
            parameterAnnotations = Metamodel.getJavaMethod((org.eclipse.ceylon.model.typechecker.model.Function) container).getParameterAnnotations();
        } else if (container instanceof org.eclipse.ceylon.model.typechecker.model.ClassAlias) {
            parameterAnnotations = Reflection.findClassAliasInstantiator(Metamodel.getJavaClass((org.eclipse.ceylon.model.typechecker.model.Class) container), (org.eclipse.ceylon.model.typechecker.model.ClassAlias) container).getParameterAnnotations();
        } else if (container instanceof org.eclipse.ceylon.model.typechecker.model.Class) {
            // FIXME: pretty sure that's wrong because of synthetic params. See ReflectionMethod.getParameters
            parameterAnnotations = Reflection.findConstructor(Metamodel.getJavaClass((org.eclipse.ceylon.model.typechecker.model.Class) container)).getParameterAnnotations();
        } else {
            throw Metamodel.newModelError("Unsupported parameter container");
        }
        // now find the right parameter
        List<Parameter> parameters = ((org.eclipse.ceylon.model.typechecker.model.Functional) container).getFirstParameterList().getParameters();
        int index = parameters.indexOf(parameter);
        if (index == -1)
            throw Metamodel.newModelError("Parameter " + parameter + " not found in container " + parameter.getModel().getContainer());
        if (index >= parameterAnnotations.length)
            throw Metamodel.newModelError("Parameter " + parameter + " index is greater than JVM parameters for " + parameter.getModel().getContainer());
        addToList(result, cls, parameterAnnotations[index]);
    }
    // nope
    return result.toArray(new java.lang.annotation.Annotation[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) HashSet(java.util.HashSet) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.compiler.java.metadata.TypeParameter) Ignore(org.eclipse.ceylon.compiler.java.metadata.Ignore)

Example 65 with Parameter

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

the class Metamodel method namedApply.

public static <Return> Return namedApply(Callable<? extends Return> function, DefaultValueProvider defaultValueProvider, org.eclipse.ceylon.model.typechecker.model.Functional declaration, ceylon.language.Iterable<? extends ceylon.language.Entry<? extends ceylon.language.String, ? extends java.lang.Object>, ? extends java.lang.Object> arguments, List<Type> parameterProducedTypes) {
    // FIXME: throw for Java declarations
    java.util.Map<java.lang.String, java.lang.Object> argumentMap = collectArguments(arguments);
    java.util.List<Parameter> parameters = declaration.getFirstParameterList().getParameters();
    // store the values in an array
    Array<java.lang.Object> values = new Array<java.lang.Object>(Anything.$TypeDescriptor$, parameters.size(), (java.lang.Object) null);
    int parameterIndex = 0;
    for (Parameter parameter : parameters) {
        // get the parameter value and remove it so we can keep track of those we used
        java.lang.Object value;
        if (argumentMap.containsKey(parameter.getName())) {
            value = argumentMap.remove(parameter.getName());
            // we have a value: check the type
            Type argumentType = Metamodel.getProducedType(value);
            Type parameterType = parameterProducedTypes.get(parameterIndex);
            if (!argumentType.isSubtypeOf(parameterType))
                throw new ceylon.language.meta.model.IncompatibleTypeException("Invalid argument " + parameter.getName() + ", expected type " + parameterType + " but got " + argumentType);
        } else {
            // make sure it has a default value
            if (!parameter.isDefaulted())
                throw new InvocationException("Missing value for non-defaulted parameter " + parameter.getName());
            // we need to fetch the default value
            value = defaultValueProvider.getDefaultParameterValue(parameter, values, parameterIndex);
            argumentMap.remove(parameter.getName());
        }
        values.set(parameterIndex++, value);
    }
    // do we have extra unknown/unused parameters left?
    if (!argumentMap.isEmpty()) {
        for (String name : argumentMap.keySet()) {
            throw new InvocationException("No such parameter " + name);
        }
    }
    // FIXME: don't we need to spread any variadic param?
    // now do a regular invocation
    Sequential<? extends Object> argumentSequence = values.sequence();
    // we can trust any variadic or pseudo-variadic since we checked parameter by parameter (no spreading possible)
    return Util.apply(function, argumentSequence, null);
}
Also used : IncompatibleTypeException(ceylon.language.meta.model.IncompatibleTypeException) CharArray(org.eclipse.ceylon.compiler.java.language.CharArray) ShortArray(org.eclipse.ceylon.compiler.java.language.ShortArray) BooleanArray(org.eclipse.ceylon.compiler.java.language.BooleanArray) Array(ceylon.language.Array) LongArray(org.eclipse.ceylon.compiler.java.language.LongArray) FloatArray(org.eclipse.ceylon.compiler.java.language.FloatArray) ByteArray(org.eclipse.ceylon.compiler.java.language.ByteArray) ObjectArray(org.eclipse.ceylon.compiler.java.language.ObjectArray) DoubleArray(org.eclipse.ceylon.compiler.java.language.DoubleArray) IntArray(org.eclipse.ceylon.compiler.java.language.IntArray) ReifiedType(org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType) NothingType(org.eclipse.ceylon.model.typechecker.model.NothingType) OpenClassOrInterfaceType(ceylon.language.meta.declaration.OpenClassOrInterfaceType) OpenType(ceylon.language.meta.declaration.OpenType) DeclarationType(org.eclipse.ceylon.model.loader.ModelLoader.DeclarationType) Type(org.eclipse.ceylon.model.typechecker.model.Type) UnknownType(org.eclipse.ceylon.model.typechecker.model.UnknownType) InvocationException(ceylon.language.meta.model.InvocationException) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)

Aggregations

Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)160 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)123 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)71 Type (org.eclipse.ceylon.model.typechecker.model.Type)71 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)57 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)48 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)45 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)45 Function (org.eclipse.ceylon.model.typechecker.model.Function)43 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)42 Value (org.eclipse.ceylon.model.typechecker.model.Value)42 ArrayList (java.util.ArrayList)38 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)30 AnalyzerUtil.getMatchingParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getMatchingParameter)27 AnalyzerUtil.getUnspecifiedParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getUnspecifiedParameter)27 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)27 Class (org.eclipse.ceylon.model.typechecker.model.Class)23 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)21 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)19 TypedReference (org.eclipse.ceylon.model.typechecker.model.TypedReference)19