Search in sources :

Example 41 with Parameter

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

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

the class NamedArgumentInvocation method appendVarsForSequencedArguments.

private void appendVarsForSequencedArguments(Tree.SequencedArgument sequencedArgument, java.util.List<Parameter> declaredParams) {
    // FIXME: this is suspisciously similar to AbstractTransformer.makeIterable(java.util.List<Tree.PositionalArgument> list, Type seqElemType)
    // and possibly needs to be merged
    Parameter parameter = sequencedArgument.getParameter();
    Type parameterType = parameterType(parameter, parameter.getType(), AbstractTransformer.TP_TO_BOUND);
    // find out the individual type, we use the argument type for the value, and the param type for the temp variable
    gen.at(sequencedArgument);
    Type tupleType = AnalyzerUtil.getTupleType(sequencedArgument.getPositionalArguments(), gen.typeFact(), false);
    // we can't just generate types like Foo<?> if the target type param is not raw because the bounds will
    // not match, so we go raw, we also ignore primitives naturally
    int flags = JT_RAW | JT_NO_PRIMITIVES;
    Type argumentsType;
    Type iteratedType;
    Type absentType;
    if (gen.typeFact().isIterableType(tupleType)) {
        argumentsType = tupleType.getSupertype(gen.typeFact().getIterableDeclaration());
        iteratedType = gen.typeFact().getIteratedType(argumentsType);
        absentType = gen.typeFact().getIteratedAbsentType(argumentsType);
    } else if (gen.typeFact().isJavaIterableType(tupleType)) {
        argumentsType = tupleType.getSupertype(gen.typeFact().getJavaIterableDeclaration());
        iteratedType = gen.typeFact().getJavaIteratedType(argumentsType);
        absentType = gen.typeFact().getNullType();
    } else if (gen.typeFact().isJavaObjectArrayType(tupleType)) {
        argumentsType = tupleType.getSupertype(gen.typeFact().getJavaObjectArrayDeclaration());
        iteratedType = gen.typeFact().getJavaArrayElementType(argumentsType);
        absentType = gen.typeFact().getNullType();
    } else if (gen.typeFact().isJavaPrimitiveArrayType(tupleType)) {
        argumentsType = tupleType.getSupertype(gen.typeFact().getJavaObjectArrayDeclaration());
        iteratedType = gen.typeFact().getJavaArrayElementType(tupleType);
        absentType = gen.typeFact().getNullType();
    } else {
        throw BugException.unhandledTypeCase(tupleType);
    }
    JCTree.JCExpression sequenceValue;
    if (Strategy.useConstantIterable(sequencedArgument)) {
        sequenceValue = gen.makeConstantIterable(sequencedArgument, iteratedType, absentType, flags);
    } else {
        sequenceValue = gen.makeLazyIterable(sequencedArgument, iteratedType, absentType, flags);
    }
    JCTree.JCExpression sequenceType = gen.makeJavaType(parameterType, flags);
    Naming.SyntheticName argName = argName(parameter);
    JCTree.JCVariableDecl varDecl = gen.makeVar(argName, sequenceType, sequenceValue);
    gen.at(getPrimary());
    bind(parameter, argName, gen.makeJavaType(parameterType, flags), List.<JCTree.JCStatement>of(varDecl));
}
Also used : Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 43 with Parameter

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

the class NamedArgumentInvocation method appendVarsForDefaulted.

private boolean appendVarsForDefaulted(java.util.List<Parameter> declaredParams) {
    boolean hasDefaulted = false;
    if (!Decl.isOverloaded(getPrimaryDeclaration())) {
        // append any arguments for defaulted parameters
        for (Parameter param : declaredParams) {
            if (bound.contains(param)) {
                continue;
            }
            final JCExpression argExpr;
            if (Strategy.hasDefaultParameterValueMethod(param)) {
                // special handling for "element" optional param of java array constructors
                if (getPrimaryDeclaration() instanceof Class && gen.isJavaArray(((Class) getPrimaryDeclaration()).getType())) {
                    // default values are hard-coded to Java default values, and are actually ignored
                    continue;
                } else if (gen.typeFact().getExceptionDeclaration().equals(getPrimaryDeclaration())) {
                    // erasure means there's no default value method, but we know it's null
                    argExpr = gen.makeNull();
                } else if (getQmePrimary() != null && gen.isJavaArray(getQmePrimary().getTypeModel())) {
                    // we support array methods with optional parameters
                    if (getPrimaryDeclaration() instanceof Function && getPrimaryDeclaration().getName().equals("copyTo")) {
                        if (param.getName().equals("sourcePosition") || param.getName().equals("destinationPosition")) {
                            argExpr = gen.makeInteger(0);
                            hasDefaulted |= true;
                        } else if (param.getName().equals("length")) {
                            argExpr = gen.makeSelect(varBaseName.suffixedBy(Suffix.$argthis$).makeIdent(), "length");
                            hasDefaulted |= true;
                        } else {
                            argExpr = gen.makeErroneous(this.getNode(), "compiler bug: argument to copyTo method of java array type not supported: " + param.getName());
                        }
                    } else {
                        argExpr = gen.makeErroneous(this.getNode(), "compiler bug: virtual method of java array type not supported: " + getPrimaryDeclaration());
                    }
                } else {
                    argExpr = makeDefaultedArgumentMethodCall(param);
                    hasDefaulted |= true;
                }
            } else if (Strategy.hasEmptyDefaultArgument(param)) {
                argExpr = gen.makeEmptyAsSequential(true);
            } else if (gen.typeFact().isIterableType(param.getType())) {
                // must be an iterable we need to fill with empty
                // FIXME: deal with this erasure bug later
                argExpr = gen.make().TypeCast(gen.makeJavaType(gen.typeFact().getIterableDeclaration().getType(), AbstractTransformer.JT_RAW), gen.makeEmpty());
            } else {
                // more expensive but worth a try
                Type appliedType = gen.getTypeForParameter(param, producedReference, AbstractTransformer.TP_TO_BOUND);
                if (gen.typeFact().isIterableType(appliedType)) {
                    argExpr = gen.make().TypeCast(gen.makeJavaType(gen.typeFact().getIterableDeclaration().getType(), AbstractTransformer.JT_RAW), gen.makeEmpty());
                } else {
                    argExpr = gen.makeErroneous(this.getNode(), "compiler bug: missing argument, and parameter is not defaulted");
                }
            }
            appendDefaulted(param, argExpr);
        }
    }
    return hasDefaulted;
}
Also used : 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) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) Class(org.eclipse.ceylon.model.typechecker.model.Class)

Example 44 with Parameter

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

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

the class ParameterDefinitionBuilder method functionalParameters.

static void functionalParameters(StringBuilder sb, ParameterList pl) {
    sb.append('(');
    Iterator<Parameter> parameters = pl.getParameters().iterator();
    while (parameters.hasNext()) {
        Parameter p = parameters.next();
        FunctionOrValue pm = p.getModel();
        sb.append(pm.getName());
        if (p.isSequenced()) {
            if (p.isAtLeastOne()) {
                sb.append('+');
            } else {
                sb.append('*');
            }
        }
        if (pm instanceof Function) {
            functionalName(sb, (Function) pm);
        }
        if (parameters.hasNext()) {
            sb.append(',');
        }
    }
    sb.append(')');
}
Also used : Function(org.eclipse.ceylon.model.typechecker.model.Function) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) FunctionOrValue(org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)

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