Search in sources :

Example 16 with Functional

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

the class ExpressionTransformer method transformConstructorDelegation.

/**
 * Transform a delegated constructor call ({@code extends XXX()})
 * which may be either a superclass initializer/constructor or a
 * same-class constructor.
 * @param extendedType
 * @param delegation The kind of delegation
 * @param invocation
 * @param classBuilder
 * @return
 */
JCStatement transformConstructorDelegation(Node extendedType, CtorDelegation delegation, Tree.InvocationExpression invocation, ClassDefinitionBuilder classBuilder, boolean forDelegationConstructor) {
    if (delegation != null && delegation.isError()) {
        return delegation.makeThrow(this);
    }
    Declaration primaryDeclaration = ((Tree.MemberOrTypeExpression) invocation.getPrimary()).getDeclaration();
    java.util.List<ParameterList> paramLists = ((Functional) primaryDeclaration).getParameterLists();
    if (paramLists.isEmpty()) {
        classBuilder.getInitBuilder().delegateCall(at(extendedType).Exec(makeErroneous(extendedType, "compiler bug: super class " + primaryDeclaration.getName() + " is missing parameter list")));
        return null;
    }
    SuperInvocation builder = new SuperInvocation(this, classBuilder.getForDefinition(), delegation, invocation, paramLists.get(0), forDelegationConstructor);
    CallBuilder callBuilder = CallBuilder.instance(this);
    boolean prevFnCall = withinInvocation(true);
    try {
        if (invocation.getPrimary() instanceof Tree.StaticMemberOrTypeExpression) {
            transformTypeArguments(callBuilder, (Tree.StaticMemberOrTypeExpression) invocation.getPrimary());
        }
        at(builder.getNode());
        JCExpression expr = null;
        Scope outerDeclaration;
        if (Decl.isConstructor(primaryDeclaration)) {
            outerDeclaration = builder.getPrimaryDeclaration().getContainer().getContainer();
        } else {
            outerDeclaration = builder.getPrimaryDeclaration().getContainer();
        }
        if ((Strategy.generateInstantiator(builder.getPrimaryDeclaration()) || builder.getPrimaryDeclaration() instanceof Class) && outerDeclaration instanceof Interface && !((Interface) outerDeclaration).isJava()) {
            // If the subclass is inner to an interface then it will be
            // generated inner to the companion and we need to qualify the
            // super(), *unless* the subclass is nested within the same
            // interface as it's superclass.
            Scope outer = builder.getSub().getContainer();
            while (!(outer instanceof Package)) {
                if (outer == outerDeclaration) {
                    expr = naming.makeSuper();
                    break;
                }
                outer = outer.getContainer();
            }
            if (expr == null) {
                if (delegation.isSelfDelegation()) {
                    throw new BugException();
                }
                Interface iface = (Interface) outerDeclaration;
                JCExpression superQual;
                if (ModelUtil.getClassOrInterfaceContainer(classBuilder.getForDefinition(), false) instanceof Interface) {
                    superQual = naming.makeCompanionAccessorCall(naming.makeQuotedThis(), iface);
                } else {
                    superQual = naming.makeCompanionFieldName(iface);
                }
                expr = naming.makeQualifiedSuper(superQual);
            }
        } else {
            expr = delegation.isSelfDelegation() ? naming.makeThis() : naming.makeSuper();
        }
        final List<JCExpression> superArguments = transformSuperInvocationArguments(classBuilder, builder, callBuilder);
        JCExpression superExpr = callBuilder.invoke(expr).arguments(superArguments).build();
        return at(extendedType).Exec(superExpr);
    // classBuilder.getInitBuilder().superCall(at(extendedType).Exec(superExpr));
    } finally {
        withinInvocation(prevFnCall);
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) Scope(org.eclipse.ceylon.model.typechecker.model.Scope) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) 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) Package(org.eclipse.ceylon.model.typechecker.model.Package) Interface(org.eclipse.ceylon.model.typechecker.model.Interface) ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) LazyInterface(org.eclipse.ceylon.model.loader.model.LazyInterface)

Example 17 with Functional

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

the class MethodDefinitionBuilder method implementsRawParameter.

private boolean implementsRawParameter(FunctionOrValue decl) {
    if (ModelUtil.containsRawType(decl.getType()))
        return true;
    // Taken pretty much straight from JvmBackendUtil.getTopmostRefinement
    Functional func = (Functional) JvmBackendUtil.getParameterized((FunctionOrValue) decl);
    if (func == null || func instanceof TypedDeclaration == false)
        return false;
    Declaration kk = getFirstRefinedDeclaration((TypedDeclaration) func);
    // error recovery
    if (kk instanceof Functional == false)
        return false;
    Functional refinedFunc = (Functional) kk;
    // shortcut if the functional doesn't override anything
    if (ModelUtil.equal((Declaration) refinedFunc, (Declaration) func)) {
        return false;
    }
    if (func.getParameterLists().size() != refinedFunc.getParameterLists().size()) {
        // invalid input
        return false;
    }
    for (int ii = 0; ii < func.getParameterLists().size(); ii++) {
        if (func.getParameterLists().get(ii).getParameters().size() != refinedFunc.getParameterLists().get(ii).getParameters().size()) {
            // invalid input
            return false;
        }
        // find the index of the parameter in the declaration
        int index = 0;
        for (Parameter px : func.getParameterLists().get(ii).getParameters()) {
            if (px.getModel() == null || px.getModel().equals(decl)) {
                // And return the corresponding parameter from the refined declaration
                FunctionOrValue refinedDecl = refinedFunc.getParameterLists().get(ii).getParameters().get(index).getModel();
                return implementsRawParameter(refinedDecl);
            }
            index++;
        }
        continue;
    }
    // invalid input
    return false;
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) 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) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

Example 18 with Functional

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

the class ParameterAnnotationTerm method encode.

@Override
public int encode(AbstractTransformer gen, ListBuffer<JCExpression> instantiations) {
    Parameter parameter = getSourceParameter();
    int index = ((Functional) parameter.getDeclaration()).getFirstParameterList().getParameters().indexOf(parameter);
    if (isSpread()) {
        index += 256;
    }
    return index;
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter)

Example 19 with Functional

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

the class RefinementVisitor method inheritDefaultedArguments.

private void inheritDefaultedArguments(Declaration d) {
    Declaration rd = d.getRefinedDeclaration();
    if (rd != d && rd instanceof Functional && d instanceof Functional) {
        Functional fd = (Functional) d;
        Functional frd = (Functional) rd;
        List<ParameterList> tdpls = fd.getParameterLists();
        List<ParameterList> rdpls = frd.getParameterLists();
        if (!tdpls.isEmpty() && !rdpls.isEmpty()) {
            List<Parameter> tdps = tdpls.get(0).getParameters();
            List<Parameter> rdps = rdpls.get(0).getParameters();
            for (int i = 0; i < tdps.size() && i < rdps.size(); i++) {
                Parameter tdp = tdps.get(i);
                Parameter rdp = rdps.get(i);
                if (tdp != null && rdp != null) {
                    tdp.setDefaulted(rdp.isDefaulted());
                }
            }
        }
    }
}
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) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) 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 20 with Functional

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

the class RefinementVisitor method checkRefiningMemberParameters.

private void checkRefiningMemberParameters(Tree.Declaration that, Declaration refining, Declaration refined, Reference refinedMember, Reference refiningMember, boolean forNative) {
    Functional refiningFun = (Functional) refining;
    Functional refinedFun = (Functional) refined;
    List<ParameterList> refiningParamLists = refiningFun.getParameterLists();
    List<ParameterList> refinedParamLists = refinedFun.getParameterLists();
    if (refinedParamLists.size() != refiningParamLists.size()) {
        String subject = forNative ? "native header" : "refined member";
        String current = forNative ? "native implementation" : "refining member";
        StringBuilder message = new StringBuilder();
        message.append(current).append(" must have the same number of parameter lists as ").append(subject).append(": ").append(message(refining));
        if (!forNative) {
            message.append(" refines ").append(message(refined));
        }
        that.addError(message.toString());
    }
    for (int i = 0; i < refinedParamLists.size() && i < refiningParamLists.size(); i++) {
        checkParameterTypes(that, getParameterList(that, i), refiningMember, refinedMember, refiningParamLists.get(i), refinedParamLists.get(i), forNative);
    }
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) ParameterList(org.eclipse.ceylon.model.typechecker.model.ParameterList)

Aggregations

Functional (org.eclipse.ceylon.model.typechecker.model.Functional)58 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)30 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)28 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)28 Parameter (org.eclipse.ceylon.model.typechecker.model.Parameter)27 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)25 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)24 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)24 Type (org.eclipse.ceylon.model.typechecker.model.Type)22 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)13 TypedReference (org.eclipse.ceylon.model.typechecker.model.TypedReference)13 CustomTree (org.eclipse.ceylon.compiler.typechecker.tree.CustomTree)12 Value (org.eclipse.ceylon.model.typechecker.model.Value)12 AnalyzerUtil.getTypedDeclaration (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.getTypedDeclaration)11 Class (org.eclipse.ceylon.model.typechecker.model.Class)11 Function (org.eclipse.ceylon.model.typechecker.model.Function)11 Reference (org.eclipse.ceylon.model.typechecker.model.Reference)11 ModelUtil.appliedType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.appliedType)10 ModelUtil.intersectionType (org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType)9 AnalyzerUtil.checkCasesDisjoint (org.eclipse.ceylon.compiler.typechecker.analyzer.AnalyzerUtil.checkCasesDisjoint)8