Search in sources :

Example 21 with Parameter

use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.

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 (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(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass)

Example 22 with Parameter

use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.

the class NamedArgumentInvocation method getTransformedArgumentExpression.

@Override
protected JCExpression getTransformedArgumentExpression(int argIndex) {
    Parameter param = callableParameters.get(argIndex);
    // note: we don't deal with unboxing here, as that is taken care of already by CallableBuilder by unboxing the
    // Callable arguments into unboxed local vars if required and if it's a value type
    String paramName;
    if (tempVars) {
        paramName = Naming.getCallableTempVarName(param);
    } else if (getPrimaryDeclaration() instanceof Class && ((Class) getPrimaryDeclaration()).hasConstructors()) {
        paramName = Naming.getAliasedParameterName(param);
    } else {
        paramName = param.getName();
    }
    return gen.makeUnquotedIdent(paramName);
}
Also used : Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass)

Example 23 with Parameter

use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.

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(), gen.TP_TO_BOUND);
    // find out the individual type, we use the argument type for the value, and the param type for the temp variable
    Type tupleType = AnalyzerUtil.getTupleType(sequencedArgument.getPositionalArguments(), gen.typeFact(), false);
    Type argumentsType = tupleType.getSupertype(gen.typeFact().getIterableDeclaration());
    Type iteratedType = gen.typeFact().getIteratedType(argumentsType);
    Type absentType = gen.typeFact().getIteratedAbsentType(argumentsType);
    // 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;
    JCTree.JCExpression 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(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCTree(com.sun.tools.javac.tree.JCTree)

Example 24 with Parameter

use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.

the class AbstractTransformer method objectVariadicToJavaArray.

private JCExpression objectVariadicToJavaArray(SimpleInvocation invocation, Type type, Type exprType, JCExpression expr, List<JCExpression> initialElements) {
    // The type that the java varargs parameter erases to: 
    // this is the type of the array which we need to construct
    java.util.List<Parameter> pl = ((Functional) invocation.getPrimaryDeclaration()).getFirstParameterList().getParameters();
    Parameter varargsParameter = pl.get(pl.size() - 1);
    Type arrayType = simplifyType(typeFact().getIteratedType(varargsParameter.getType()));
    while (arrayType.getDeclaration() instanceof TypeParameter) {
        TypeParameter tp = (TypeParameter) arrayType.getDeclaration();
        if (tp.getSatisfiedTypes().isEmpty()) {
            arrayType = typeFact().getObjectType();
            break;
        } else {
            arrayType = tp.getSatisfiedTypes().get(0);
        }
    }
    // we could have a <X>variadic(X&Object), so we need to pick a type which satisfies the bound
    Type castType = simplifyType(type);
    if (typeFact().isIntersection(castType)) {
        for (Type t : castType.getSatisfiedTypes()) {
            if (t.isSubtypeOf(arrayType)) {
                castType = t;
                break;
            }
        }
    }
    Naming.SyntheticName seqName = naming.temp().suffixedBy(0);
    Type sequentialType = typeFact().getSequentialDeclaration().appliedType(null, Arrays.asList(type));
    JCExpression seqTypeExpr1 = makeJavaType(sequentialType);
    //JCExpression seqTypeExpr2 = makeJavaType(fixedSizedType);
    JCExpression sizeExpr = make().Apply(List.<JCExpression>nil(), make().Select(seqName.makeIdent(), names().fromString("getSize")), List.<JCExpression>nil());
    sizeExpr = utilInvocation().toInt(sizeExpr);
    // add initial elements if required
    if (!initialElements.isEmpty())
        sizeExpr = make().Binary(JCTree.PLUS, sizeExpr, makeInteger(initialElements.size()));
    JCExpression array = make().NewArray(makeJavaType(arrayType, JT_RAW | JT_NO_PRIMITIVES), List.of(sizeExpr), null);
    if (!arrayType.isExactly(castType)) {
        array = make().TypeCast(make().TypeArray(makeJavaType(castType, JT_CLASS_NEW | JT_NO_PRIMITIVES)), array);
    }
    JCExpression sequenceToArrayExpr = utilInvocation().toArray(seqName.makeIdent(), array, initialElements, makeJavaType(castType, JT_CLASS_NEW | JT_NO_PRIMITIVES));
    return //make().TypeArray(makeJavaType(arrayType)),
    makeLetExpr(seqName, List.<JCStatement>nil(), seqTypeExpr1, expr, sequenceToArrayExpr);
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) ModelUtil.appliedType(com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName)

Example 25 with Parameter

use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.

the class AnnotationLoader method loadAnnotationInvocationArguments.

private void loadAnnotationInvocationArguments(List<AnnotationFieldName> path, LazyFunction method, AnnotationInvocation ai, AnnotationMirror annotationInvocationAnnotation, List<AnnotationMirror> annotationTree, AnnotatedMirror dpm) {
    @SuppressWarnings("unchecked") List<Short> argumentCodes = (List<Short>) annotationInvocationAnnotation.getValue(AbstractModelLoader.CEYLON_ANNOTATION_INSTANTIATION_ARGUMENTS_MEMBER);
    if (argumentCodes != null) {
        for (int ii = 0; ii < argumentCodes.size(); ii++) {
            short code = argumentCodes.get(ii);
            AnnotationArgument argument = new AnnotationArgument();
            Parameter classParameter = ai.getParameters().get(ii);
            argument.setParameter(classParameter);
            path.add(argument);
            argument.setTerm(loadAnnotationArgumentTerm(path, method, ai, classParameter, annotationTree, dpm, code));
            path.remove(path.size() - 1);
            ai.getAnnotationArguments().add(argument);
        }
    }
}
Also used : Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) AnnotationConstructorParameter(com.redhat.ceylon.compiler.java.codegen.AnnotationConstructorParameter) ArrayList(java.util.ArrayList) List(java.util.List) AnnotationArgument(com.redhat.ceylon.compiler.java.codegen.AnnotationArgument)

Aggregations

Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)56 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)40 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)25 Type (com.redhat.ceylon.model.typechecker.model.Type)24 Function (com.redhat.ceylon.model.typechecker.model.Function)19 ParameterList (com.redhat.ceylon.model.typechecker.model.ParameterList)18 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)17 JCTree (com.sun.tools.javac.tree.JCTree)15 Class (com.redhat.ceylon.model.typechecker.model.Class)14 ArrayList (java.util.ArrayList)14 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)13 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)13 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)12 Value (com.redhat.ceylon.model.typechecker.model.Value)12 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)11 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)11 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)11 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)10 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)9 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)9