Search in sources :

Example 61 with ParameterList

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

the class ExpressionTransformer method transformFunctionalInterfaceBridge.

public JCExpression transformFunctionalInterfaceBridge(Tree.StaticMemberOrTypeExpression expr, Value functional, Type expectedType) {
    ParameterList paramList = new ParameterList();
    Type callableType = expr.getTypeModel().getSupertype(typeFact().getCallableDeclaration());
    int i = 0;
    for (Type type : typeFact().getCallableArgumentTypes(callableType)) {
        Parameter param = new Parameter();
        Value paramModel = new Value();
        param.setModel(paramModel);
        param.setName("arg" + i);
        paramModel.setName("arg" + i);
        paramModel.setType(type);
        paramList.getParameters().add(param);
        i++;
    }
    return CallableBuilder.methodReference(gen(), expr, paramList, expectedType, expr.getTypeModel(), false);
}
Also used : UnionType(org.eclipse.ceylon.model.typechecker.model.UnionType) Type(org.eclipse.ceylon.model.typechecker.model.Type) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter)

Example 62 with ParameterList

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

the class ClassTransformer method transformMplBody.

/**
 * Constructs all but the outer-most method of a {@code Function} with
 * multiple parameter lists
 * @param model The {@code Function} model
 * @param body The inner-most body
 */
List<JCStatement> transformMplBody(java.util.List<Tree.ParameterList> parameterListsTree, Function model, List<JCStatement> body) {
    Type resultType = model.getType();
    for (int index = model.getParameterLists().size() - 1; index > 0; index--) {
        ParameterList pl = model.getParameterLists().get(index);
        resultType = gen().typeFact().getCallableType(List.of(resultType, typeFact().getParameterTypesAsTupleType(pl.getParameters(), model.getReference())));
        CallableBuilder cb = CallableBuilder.mpl(gen(), resultType, pl, parameterListsTree.get(index), body);
        body = List.<JCStatement>of(make().Return(cb.build()));
    }
    return body;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList)

Example 63 with ParameterList

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

the class ClassTransformer method generateInstantiators.

private void generateInstantiators(ClassDefinitionBuilder classBuilder, Class cls, Constructor ctor, ClassDefinitionBuilder instantiatorDeclCb, ClassDefinitionBuilder instantiatorImplCb, Tree.Declaration node, Tree.ParameterList pl) {
    // TODO Instantiators on companion classes
    if (ModelUtil.isEnumeratedConstructor(ctor)) {
        return;
    }
    ParameterList parameterList = ctor != null ? ctor.getFirstParameterList() : cls.getParameterList();
    if (cls.isInterfaceMember()) {
        DefaultedArgumentOverload overloaded = new DefaultedArgumentInstantiator(daoAbstract, cls, ctor, instantiatorDeclCb.isCompanionBuilder());
        MethodDefinitionBuilder instBuilder = overloaded.makeOverload(parameterList, null, cls.getTypeParameters());
        instantiatorDeclCb.method(instBuilder);
    }
    if (!cls.isInterfaceMember() || !cls.isFormal()) {
        DefaultedArgumentOverload overloaded = new DefaultedArgumentInstantiator(!cls.isFormal() ? new DaoThis(node, pl) : daoAbstract, cls, ctor, instantiatorImplCb.isCompanionBuilder());
        MethodDefinitionBuilder instBuilder = overloaded.makeOverload(parameterList, null, cls.getTypeParameters());
        instantiatorImplCb.method(instBuilder);
    }
}
Also used : ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList)

Example 64 with ParameterList

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

the class CallableBuilder method unboundValueMemberReference.

/**
 * Used for "static" value references. For example:
 * <pre>
 *     value x = Integer.plus;
 *     value y = Foo.method;
 *     value z = Outer.Inner;
 * </pre>
 */
public static CallableBuilder unboundValueMemberReference(CeylonTransformer gen, Tree.QualifiedMemberOrTypeExpression qmte, Type typeModel, final TypedDeclaration value, Type expectedType) {
    CallBuilder callBuilder = CallBuilder.instance(gen);
    Type qualifyingType = qmte.getTarget().getQualifyingType();
    JCExpression target = gen.naming.makeUnquotedIdent(Unfix.$instance$);
    target = gen.expressionGen().applyErasureAndBoxing(target, qmte.getPrimary().getTypeModel(), true, BoxingStrategy.BOXED, qualifyingType);
    if (gen.expressionGen().isThrowableMessage(qmte)) {
        callBuilder.invoke(gen.utilInvocation().throwableMessage());
        callBuilder.argument(target);
    } else if (gen.expressionGen().isThrowableSuppressed(qmte)) {
        callBuilder.invoke(gen.utilInvocation().suppressedExceptions());
        callBuilder.argument(target);
    } else {
        JCExpression memberName = gen.naming.makeQualifiedName(target, value, Naming.NA_GETTER | Naming.NA_MEMBER);
        if (value instanceof FieldValue) {
            callBuilder.fieldRead(memberName);
        } else {
            callBuilder.invoke(memberName);
        }
    }
    JCExpression innerInvocation = callBuilder.build();
    // use the return type since the value is actually applied
    Type returnType = gen.getReturnTypeOfCallable(typeModel);
    innerInvocation = gen.expressionGen().applyErasureAndBoxing(innerInvocation, returnType, // expression is a Callable
    qmte.getTypeErased(), !CodegenUtil.isUnBoxed(value), BoxingStrategy.BOXED, returnType, 0);
    ParameterList outerPl = new ParameterList();
    Parameter instanceParameter = new Parameter();
    instanceParameter.setName(Naming.name(Unfix.$instance$));
    Value valueModel = new Value();
    instanceParameter.setModel(valueModel);
    Type accessType = gen.getParameterTypeOfCallable(typeModel, 0);
    ;
    if (!value.isShared()) {
        accessType = Decl.getPrivateAccessType(qmte);
    }
    valueModel.setName(instanceParameter.getName());
    valueModel.setInitializerParameter(instanceParameter);
    valueModel.setType(accessType);
    valueModel.setUnboxed(false);
    outerPl.getParameters().add(instanceParameter);
    CallableBuilder outer = new CallableBuilder(gen, qmte, typeModel, outerPl);
    outer.parameterTypes = outer.getParameterTypesFromParameterModels();
    List<JCStatement> innerBody = List.<JCStatement>of(gen.make().Return(innerInvocation));
    outer.useDefaultTransformation(innerBody);
    outer.companionAccess = Decl.isPrivateAccessRequiringCompanion(qmte);
    if (expectedType != null)
        outer.checkForFunctionalInterface(expectedType);
    return outer;
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement)

Example 65 with ParameterList

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

the class CallableBuilder method unboundFunctionalMemberReference.

/**
 * Used for "static" method or class references. For example:
 * <pre>
 *     value x = Integer.plus;
 *     value y = Foo.method;
 *     value z = Outer.Inner;
 * </pre>
 */
public static JCExpression unboundFunctionalMemberReference(CeylonTransformer gen, Tree.QualifiedMemberOrTypeExpression qmte, Type typeModel, final Functional methodClassOrCtor, Reference producedReference, Type expectedType) {
    final ParameterList parameterList = methodClassOrCtor.getFirstParameterList();
    Type type = typeModel;
    JCExpression target;
    boolean memberClassCtorRef = ModelUtil.getConstructor((Declaration) methodClassOrCtor) != null && !ModelUtil.getConstructedClass((Declaration) methodClassOrCtor).isToplevel() && qmte.getPrimary() instanceof Tree.QualifiedTypeExpression;
    boolean hasOuter = !(Decl.isConstructor((Declaration) methodClassOrCtor) && gen.getNumParameterLists(typeModel) == 1);
    if (!hasOuter) {
        type = typeModel;
        if (memberClassCtorRef) {
            target = gen.naming.makeUnquotedIdent(Unfix.$instance$);
        } else {
            target = null;
        }
    } else {
        type = gen.getReturnTypeOfCallable(type);
        Type qualifyingType = qmte.getTarget().getQualifyingType();
        target = gen.naming.makeUnquotedIdent(Unfix.$instance$);
        target = gen.expressionGen().applyErasureAndBoxing(target, producedReference.getQualifyingType(), true, BoxingStrategy.BOXED, qualifyingType);
    }
    CallableBuilder inner = new CallableBuilder(gen, qmte, type, parameterList);
    // FromParameterModels();
    inner.parameterTypes = inner.getParameterTypesFromCallableModel();
    if (hasOuter) {
        inner.defaultValueCall = inner.new MemberReferenceDefaultValueCall(methodClassOrCtor);
    }
    CallBuilder callBuilder = CallBuilder.instance(gen);
    Type accessType = gen.getParameterTypeOfCallable(typeModel, 0);
    boolean needsCast = false;
    if (Decl.isConstructor((Declaration) methodClassOrCtor)) {
        Constructor ctor = ModelUtil.getConstructor((Declaration) methodClassOrCtor);
        Class cls = ModelUtil.getConstructedClass(ctor);
        if (Strategy.generateInstantiator(ctor)) {
            needsCast = Strategy.isInstantiatorUntyped(ctor);
            callBuilder.invoke(gen.naming.makeInstantiatorMethodName(target, cls));
        } else if (Decl.isJavaArrayWith(ctor)) {
            callBuilder.arrayWith(gen.getReturnTypeOfCallable(typeModel).getQualifyingType(), gen.makeJavaType(gen.getReturnTypeOfCallable(typeModel), JT_CLASS_NEW));
        } else {
            callBuilder.instantiate(gen.makeJavaType(gen.getReturnTypeOfCallable(typeModel), JT_CLASS_NEW));
            if (!ctor.isShared()) {
                accessType = Decl.getPrivateAccessType(qmte);
            }
        }
    } else if (methodClassOrCtor instanceof Function && ((Function) methodClassOrCtor).isParameter()) {
        callBuilder.invoke(gen.naming.makeQualifiedName(target, (Function) methodClassOrCtor, Naming.NA_MEMBER));
    } else if (methodClassOrCtor instanceof Function) {
        callBuilder.invoke(gen.naming.makeQualifiedName(target, (Function) methodClassOrCtor, Naming.NA_MEMBER));
        if (!((TypedDeclaration) methodClassOrCtor).isShared()) {
            accessType = Decl.getPrivateAccessType(qmte);
        }
    } else if (methodClassOrCtor instanceof Class) {
        Class cls = (Class) methodClassOrCtor;
        if (Strategy.generateInstantiator(cls)) {
            callBuilder.invoke(gen.naming.makeInstantiatorMethodName(target, cls));
        } else {
            callBuilder.instantiate(new ExpressionAndType(target, null), gen.makeJavaType(cls.getType(), JT_CLASS_NEW | AbstractTransformer.JT_NON_QUALIFIED));
            if (!cls.isShared()) {
                accessType = Decl.getPrivateAccessType(qmte);
            }
        }
    } else {
        throw BugException.unhandledDeclarationCase((Declaration) methodClassOrCtor, qmte);
    }
    ListBuffer<ExpressionAndType> reified = new ListBuffer<ExpressionAndType>();
    DirectInvocation.addReifiedArguments(gen, producedReference, reified);
    for (ExpressionAndType reifiedArgument : reified) {
        callBuilder.argument(reifiedArgument.expression);
    }
    if (Decl.isConstructor((Declaration) methodClassOrCtor) && !Decl.isDefaultConstructor(ModelUtil.getConstructor((Declaration) methodClassOrCtor)) && !Decl.isJavaArrayWith((Constructor) methodClassOrCtor)) {
        // invoke the param class ctor
        Constructor ctor = ModelUtil.getConstructor((Declaration) methodClassOrCtor);
        callBuilder.argument(gen.naming.makeNamedConstructorName(ctor, false));
    }
    for (Parameter parameter : parameterList.getParameters()) {
        callBuilder.argument(gen.naming.makeQuotedIdent(Naming.getAliasedParameterName(parameter)));
    }
    JCExpression innerInvocation = callBuilder.build();
    if (needsCast) {
        innerInvocation = gen.make().TypeCast(gen.makeJavaType(gen.getReturnTypeOfCallable(type)), innerInvocation);
    }
    // Need to worry about boxing for Function and FunctionalParameter
    if (methodClassOrCtor instanceof TypedDeclaration && !Decl.isConstructor((Declaration) methodClassOrCtor)) {
        // use the method return type since the function is actually applied
        Type returnType = gen.getReturnTypeOfCallable(type);
        innerInvocation = gen.expressionGen().applyErasureAndBoxing(innerInvocation, returnType, // expression is a Callable
        CodegenUtil.hasTypeErased((TypedDeclaration) methodClassOrCtor), !CodegenUtil.isUnBoxed((TypedDeclaration) methodClassOrCtor), BoxingStrategy.BOXED, returnType, 0);
    } else if (methodClassOrCtor instanceof Class && Strategy.isInstantiatorUntyped((Class) methodClassOrCtor)) {
        // $new method declared to return Object, so needs typecast
        innerInvocation = gen.make().TypeCast(gen.makeJavaType(((Class) methodClassOrCtor).getType()), innerInvocation);
    }
    List<JCStatement> innerBody = List.<JCStatement>of(gen.make().Return(innerInvocation));
    inner.useDefaultTransformation(innerBody);
    JCExpression callable = inner.build();
    if (!hasOuter) {
        if (memberClassCtorRef) {
            ;
            JCVariableDecl def = gen.makeVar(Unfix.$instance$.toString(), gen.makeJavaType(((QualifiedMemberOrTypeExpression) qmte.getPrimary()).getPrimary().getTypeModel()), gen.expressionGen().transformQualifiedMemberPrimary(qmte));
            return gen.make().LetExpr(def, callable);
        }
        return callable;
    }
    ParameterList outerPl = new ParameterList();
    Parameter instanceParameter = new Parameter();
    instanceParameter.setName(Naming.name(Unfix.$instance$));
    Value valueModel = new Value();
    instanceParameter.setModel(valueModel);
    valueModel.setName(instanceParameter.getName());
    valueModel.setInitializerParameter(instanceParameter);
    valueModel.setType(accessType);
    valueModel.setUnboxed(false);
    outerPl.getParameters().add(instanceParameter);
    CallableBuilder outer = new CallableBuilder(gen, qmte, typeModel, outerPl);
    outer.parameterTypes = outer.getParameterTypesFromParameterModels();
    List<JCStatement> outerBody = List.<JCStatement>of(gen.make().Return(callable));
    outer.useDefaultTransformation(outerBody);
    outer.companionAccess = Decl.isPrivateAccessRequiringCompanion(qmte);
    if (expectedType != null)
        outer.checkForFunctionalInterface(expectedType);
    return outer.build();
}
Also used : TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) Function(org.eclipse.ceylon.model.typechecker.model.Function) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue) Value(org.eclipse.ceylon.model.typechecker.model.Value) FieldValue(org.eclipse.ceylon.model.loader.model.FieldValue) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) Class(org.eclipse.ceylon.model.typechecker.model.Class) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration)

Aggregations

ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)69 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)47 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)44 Type (org.eclipse.ceylon.model.typechecker.model.Type)25 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)24 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)24 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)23 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)22 Function (org.eclipse.ceylon.model.typechecker.model.Function)21 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)20 Value (org.eclipse.ceylon.model.typechecker.model.Value)20 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)19 ArrayList (java.util.ArrayList)16 AnalyzerUtil.getMatchingParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getMatchingParameter)12 AnalyzerUtil.getUnspecifiedParameter (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getUnspecifiedParameter)12 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)12 FieldValue (org.eclipse.ceylon.model.loader.model.FieldValue)12 Class (org.eclipse.ceylon.model.typechecker.model.Class)11 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)10 HashMap (java.util.HashMap)9