Search in sources :

Example 66 with Parameter

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

the class FunctionalUtil method getParameters.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Sequential<FunctionOrValueDeclaration> getParameters(Functional declaration) {
    ParameterList parameterList = ((Functional) declaration).getFirstParameterList();
    if (parameterList == null)
        return (Sequential) empty_.get_();
    List<Parameter> modelParameters = parameterList.getParameters();
    ceylon.language.meta.declaration.FunctionOrValueDeclaration[] parameters = new ceylon.language.meta.declaration.FunctionOrValueDeclaration[modelParameters.size()];
    int i = 0;
    for (Parameter modelParameter : modelParameters) {
        parameters[i] = (ceylon.language.meta.declaration.FunctionOrValueDeclaration) Metamodel.getOrCreateMetamodel(modelParameter.getModel());
        i++;
    }
    return Util.sequentialWrapper(ceylon.language.meta.declaration.FunctionOrValueDeclaration.$TypeDescriptor$, parameters);
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) FunctionOrValueDeclaration(ceylon.language.meta.declaration.FunctionOrValueDeclaration)

Example 67 with Parameter

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

the class Metamodel method getJavaInstantiators.

public static List<java.lang.reflect.Method> getJavaInstantiators(org.eclipse.ceylon.model.typechecker.model.Constructor declaration) {
    org.eclipse.ceylon.model.typechecker.model.Class classModel = (org.eclipse.ceylon.model.typechecker.model.Class) declaration.getContainer();
    // Class<?> javaClass = getJavaClass(classModel);
    Class<?> outerJavaClass = getJavaClass((Declaration) classModel.getContainer());
    // java.lang.reflect.Method[] ctors = outerJavaClass.getDeclaredMethods();
    ArrayList<java.lang.reflect.Method> result = new ArrayList<java.lang.reflect.Method>();
    // find the appropriate ultimate constructor
    String methodName = classModel.getName() + "$new$";
    java.lang.reflect.Method ultimate = getJavaInstantiator(declaration, methodName);
    result.add(ultimate);
    List<Parameter> parameters = declaration.getFirstParameterList().getParameters();
    Class<?>[] javapl = ultimate.getParameterTypes();
    // according to the parameter list
    for (int ii = parameters.size() - 1, jj = javapl.length; ii >= 0; ii--, jj--) {
        Parameter p = parameters.get(ii);
        if (p.isDefaulted() || (p.isSequenced() && !p.isAtLeastOne())) {
            Class<?>[] sig = Arrays.copyOfRange(javapl, 0, jj - 1);
            try {
                java.lang.reflect.Method overloaded = outerJavaClass.getDeclaredMethod(methodName, sig);
                result.add(overloaded);
            } catch (NoSuchMethodException e) {
                throw Metamodel.newModelError("Could not find overloaded constructor with signature " + Arrays.toString(sig), e);
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) JavaMethod(org.eclipse.ceylon.model.loader.model.JavaMethod) Method(java.lang.reflect.Method) AnnotationProxyMethod(org.eclipse.ceylon.model.loader.model.AnnotationProxyMethod) ReflectionMethod(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionMethod) 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) AnnotationProxyClass(org.eclipse.ceylon.model.loader.model.AnnotationProxyClass) ReflectionClass(org.eclipse.ceylon.model.loader.impl.reflect.mirror.ReflectionClass) LazyClass(org.eclipse.ceylon.model.loader.model.LazyClass) Method(java.lang.reflect.Method)

Example 68 with Parameter

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

the class GenerateJsVisitor method initDefaultedParameters.

/**
 * Create special functions with the expressions for defaulted parameters in a parameter list.
 */
void initDefaultedParameters(final Tree.ParameterList params, Tree.AnyMethod container) {
    if (!(container instanceof Tree.MethodDeclaration || container.getDeclarationModel().isMember())) {
        return;
    }
    final boolean isMember = container.getDeclarationModel().isMember();
    for (final Tree.Parameter param : params.getParameters()) {
        Parameter pd = param.getParameterModel();
        if (pd.isDefaulted()) {
            final SpecifierOrInitializerExpression expr = getDefaultExpression(param);
            if (expr == null) {
                continue;
            }
            if (isMember) {
                qualify(params, container.getDeclarationModel());
                out(names.name(container.getDeclarationModel()), "$defs$", pd.getName(), "=function");
            } else {
                out("function ", names.name(container.getDeclarationModel()), "$defs$", pd.getName());
            }
            params.visit(this);
            out("{");
            initSelf(expr);
            out("return ");
            if (param instanceof Tree.ParameterDeclaration) {
                Tree.TypedDeclaration node = ((Tree.ParameterDeclaration) param).getTypedDeclaration();
                if (node instanceof Tree.MethodDeclaration) {
                    // function parameter defaulted using "=>"
                    FunctionHelper.singleExprFunction(((Tree.MethodDeclaration) node).getParameterLists(), expr.getExpression(), null, true, true, this);
                } else if (!isNaturalLiteral(expr.getExpression().getTerm())) {
                    expr.visit(this);
                }
            } else if (!isNaturalLiteral(expr.getExpression().getTerm())) {
                expr.visit(this);
            }
            out(";}");
            endLine(true);
        }
    }
}
Also used : Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) SpecifierOrInitializerExpression(org.eclipse.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)

Example 69 with Parameter

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

the class GenerateJsVisitor method classDeclaration.

private void classDeclaration(final Tree.ClassDeclaration that) {
    // Don't even bother with nodes that have errors
    if (errVisitor.hasErrors(that))
        return;
    comment(that);
    final Class d = that.getDeclarationModel();
    final String aname = names.name(d);
    final Tree.ClassSpecifier ext = that.getClassSpecifier();
    out(function, aname, "(");
    // Generate each parameter because we need to append one at the end
    for (Tree.Parameter p : that.getParameterList().getParameters()) {
        p.visit(this);
        out(", ");
    }
    if (d.getTypeParameters() != null && !d.getTypeParameters().isEmpty()) {
        out("$$targs$$,");
    }
    out(names.self(d), "){");
    initSelf(that);
    initParameters(that.getParameterList(), d, null);
    out("return ");
    TypeDeclaration aliased = ext.getType().getDeclarationModel();
    final String aliasedName;
    aliasedName = names.name(aliased);
    qualify(that, aliased);
    Scope superscope = getSuperMemberScope(ext.getType());
    if (superscope != null) {
        out("getT$all()['");
        out(superscope.getQualifiedNameString());
        out("'].$$.prototype.", aliasedName, ".call(", names.self(prototypeOwner), ",");
    } else {
        out(aliasedName, "(");
    }
    Tree.PositionalArgumentList posArgs = ext.getInvocationExpression().getPositionalArgumentList();
    if (posArgs != null) {
        posArgs.visit(this);
        if (!posArgs.getPositionalArguments().isEmpty()) {
            out(",");
        }
    } else {
        out("/*PENDIENTE NAMED ARG CLASS DECL */");
    }
    Map<TypeParameter, Type> invargs = ext.getType().getTypeModel().getTypeArguments();
    if (invargs != null && !invargs.isEmpty()) {
        TypeUtils.printTypeArguments(that, invargs, this, true, ext.getType().getTypeModel().getVarianceOverrides());
        out(",");
    }
    out(names.self(d), ");}");
    endLine();
    out(aname, ".$$=");
    qualify(that, aliased);
    out(aliasedName, ".$$");
    endLine(true);
    out(aname, ".$crtmm$=");
    TypeUtils.encodeForRuntime(that, d, this);
    endLine(true);
    share(d);
    if (aliased instanceof Class && ((Class) aliased).hasConstructors() || aliased instanceof Constructor) {
        Class ac = aliased instanceof Constructor ? (Class) ((Constructor) aliased).getContainer() : (Class) aliased;
        for (Declaration cm : ac.getMembers()) {
            if (cm instanceof Constructor && cm != ac.getDefaultConstructor() && cm.isShared()) {
                Constructor cons = (Constructor) cm;
                final String constructorName = aname + names.constructorSeparator(cons) + names.name(cons);
                out("function ", constructorName, "(");
                List<Parameter> parameters = cons.getFirstParameterList().getParameters();
                ArrayList<String> pnames = new ArrayList<>(parameters.size() + 1);
                boolean first = true;
                for (int i = 0; i < parameters.size(); i++) {
                    final String pname = names.createTempVariable();
                    pnames.add(pname);
                    if (first) {
                        first = false;
                    } else {
                        out(",");
                    }
                    out(pname);
                }
                out("){return ");
                qualify(that, cons);
                out(names.name(cons), "(");
                first = true;
                for (String pname : pnames) {
                    if (first) {
                        first = false;
                    } else {
                        out(",");
                    }
                    out(pname);
                }
                out(");}");
                if (ac.isShared()) {
                    sharePrefix(ac, true);
                    out(constructorName, "=", constructorName, ";");
                    endLine();
                }
            }
        }
    }
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ArrayList(java.util.ArrayList) 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) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) 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) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)

Example 70 with Parameter

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

the class GenerateJsVisitor method addToPrototype.

private void addToPrototype(ClassOrInterface d, final Tree.Statement s, List<Parameter> params, InitDeferrer initDeferrer) {
    ClassOrInterface oldPrototypeOwner = prototypeOwner;
    prototypeOwner = d;
    if (s instanceof Tree.MethodDefinition) {
        addMethodToPrototype(d, (Tree.MethodDefinition) s);
    } else if (s instanceof Tree.MethodDeclaration) {
        // Don't even bother with nodes that have errors
        if (errVisitor.hasErrors(s))
            return;
        FunctionHelper.methodDeclaration(d, (Tree.MethodDeclaration) s, this, verboseStitcher);
    } else if (s instanceof Tree.AttributeGetterDefinition) {
        addGetterToPrototype(d, (Tree.AttributeGetterDefinition) s);
    } else if (s instanceof Tree.AttributeDeclaration) {
        AttributeGenerator.addGetterAndSetterToPrototype(d, (Tree.AttributeDeclaration) s, this, verboseStitcher);
    } else if (s instanceof Tree.ClassDefinition) {
        addClassToPrototype(d, (Tree.ClassDefinition) s, initDeferrer);
    } else if (s instanceof Tree.InterfaceDefinition) {
        addInterfaceToPrototype(d, (Tree.InterfaceDefinition) s, initDeferrer);
    } else if (s instanceof Tree.ObjectDefinition) {
        addObjectToPrototype(d, (Tree.ObjectDefinition) s, initDeferrer);
    } else if (s instanceof Tree.ClassDeclaration) {
        addClassDeclarationToPrototype(d, (Tree.ClassDeclaration) s);
    } else if (s instanceof Tree.InterfaceDeclaration) {
        addInterfaceDeclarationToPrototype(d, (Tree.InterfaceDeclaration) s);
    } else if (s instanceof Tree.SpecifierStatement) {
        addSpecifierToPrototype(d, (Tree.SpecifierStatement) s);
    } else if (s instanceof Tree.TypeAliasDeclaration) {
        addAliasDeclarationToPrototype(d, (Tree.TypeAliasDeclaration) s);
    }
    // This fixes #231 for prototype style
    if (params != null && s instanceof Tree.Declaration) {
        Declaration m = ((Tree.Declaration) s).getDeclarationModel();
        for (Iterator<Parameter> iter = params.iterator(); iter.hasNext(); ) {
            Parameter _p = iter.next();
            if (m.getName() != null && m.getName().equals(_p.getName())) {
                iter.remove();
                break;
            }
        }
    }
    prototypeOwner = oldPrototypeOwner;
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) ClassDefinition(org.eclipse.ceylon.compiler.typechecker.tree.Tree.ClassDefinition) AttributeDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) 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

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