Search in sources :

Example 36 with Parameter

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

the class CallableBuilder method javaStaticMethodReference.

public static CallableBuilder javaStaticMethodReference(CeylonTransformer gen, Type typeModel, final Functional methodOrClass, Reference producedReference) {
    final ParameterList parameterList = methodOrClass.getFirstParameterList();
    CallableBuilder inner = new CallableBuilder(gen, null, typeModel, parameterList);
    ArrayList<Type> pt = new ArrayList<>();
    for (Parameter p : methodOrClass.getFirstParameterList().getParameters()) {
        pt.add(p.getType());
    }
    inner.parameterTypes = pt;
    inner.defaultValueCall = inner.new MemberReferenceDefaultValueCall(methodOrClass);
    JCExpression innerInvocation = gen.expressionGen().makeJavaStaticInvocation(gen, methodOrClass, producedReference, parameterList);
    // Need to worry about boxing for Function and FunctionalParameter 
    if (methodOrClass instanceof TypedDeclaration) {
        innerInvocation = gen.expressionGen().applyErasureAndBoxing(innerInvocation, methodOrClass.getType(), !CodegenUtil.isUnBoxed((TypedDeclaration) methodOrClass), BoxingStrategy.BOXED, methodOrClass.getType());
    } else if (Strategy.isInstantiatorUntyped((Class) methodOrClass)) {
        // $new method declared to return Object, so needs typecast
        innerInvocation = gen.make().TypeCast(gen.makeJavaType(((Class) methodOrClass).getType()), innerInvocation);
    }
    List<JCStatement> innerBody = List.<JCStatement>of(gen.make().Return(innerInvocation));
    inner.useDefaultTransformation(innerBody);
    return inner;
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ArrayList(java.util.ArrayList) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) Class(com.redhat.ceylon.model.typechecker.model.Class) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 37 with Parameter

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

the class AnnotationInvocationVisitor method transformConstructor.

private static JCAnnotation transformConstructor(ExpressionTransformer exprGen, Tree.InvocationExpression invocation, AnnotationInvocation ai, com.sun.tools.javac.util.List<AnnotationFieldName> fieldPath) {
    Map<Parameter, ListBuffer<JCExpression>> args = new LinkedHashMap<Parameter, ListBuffer<JCExpression>>();
    List<Parameter> classParameters = ai.getClassParameters();
    // The class parameter's we've not yet figured out the value for
    ArrayList<Parameter> unbound = new ArrayList<Parameter>(classParameters);
    for (Parameter classParameter : classParameters) {
        for (AnnotationArgument argument : ai.findAnnotationArgumentForClassParameter(classParameter)) {
            JCExpression expr = transformConstructorArgument(exprGen, invocation, classParameter, argument, fieldPath);
            appendArgument(args, classParameter, expr);
            unbound.remove(classParameter);
        }
    }
    outer: for (Parameter classParameter : ((ArrayList<Parameter>) unbound.clone())) {
        // Defaulted argument
        if (ai.isInstantiation()) {
            if (classParameter.isDefaulted()) {
                // That's OK, we'll pick up the default argument from 
                // the Java Annotation type
                unbound.remove(classParameter);
                continue outer;
            }
        } else {
            Function ac2 = (Function) ai.getPrimary();
            AnnotationInvocation i = (AnnotationInvocation) ac2.getAnnotationConstructor();
            for (AnnotationArgument aa : i.getAnnotationArguments()) {
                if (aa.getParameter().equals(classParameter)) {
                    appendArgument(args, classParameter, aa.getTerm().makeAnnotationArgumentValue(exprGen, i, com.sun.tools.javac.util.List.<AnnotationFieldName>of(aa)));
                    unbound.remove(classParameter);
                    continue outer;
                }
            }
        }
        if (Strategy.hasEmptyDefaultArgument(classParameter)) {
            appendArgument(args, classParameter, exprGen.make().NewArray(null, null, com.sun.tools.javac.util.List.<JCExpression>nil()));
            unbound.remove(classParameter);
            continue outer;
        }
    }
    for (Parameter classParameter : unbound) {
        appendArgument(args, classParameter, exprGen.makeErroneous(invocation, "compiler bug: unbound annotation class parameter " + classParameter.getName()));
    }
    ListBuffer<JCExpression> assignments = ListBuffer.<JCExpression>lb();
    for (Map.Entry<Parameter, ListBuffer<JCExpression>> entry : args.entrySet()) {
        ListBuffer<JCExpression> exprs = entry.getValue();
        if (exprs.size() == 1) {
            assignments.append(makeArgument(exprGen, invocation, entry.getKey(), exprs.first()));
        } else {
            assignments.append(makeArgument(exprGen, invocation, entry.getKey(), exprGen.make().NewArray(null, null, exprs.toList())));
        }
    }
    JCAnnotation annotation = exprGen.at(invocation).Annotation(ai.makeAnnotationType(exprGen), assignments.toList());
    return annotation;
}
Also used : ListBuffer(com.sun.tools.javac.util.ListBuffer) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Function(com.redhat.ceylon.model.typechecker.model.Function) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation)

Example 38 with Parameter

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

the class AnnotationInvocation method makeAnnotation.

public JCExpression makeAnnotation(ExpressionTransformer exprGen, AnnotationInvocation ai, com.sun.tools.javac.util.List<AnnotationFieldName> parameterPath) {
    ListBuffer<JCExpression> args = ListBuffer.<JCExpression>lb();
    for (AnnotationArgument aa : getAnnotationArguments()) {
        Parameter name = aa.getParameter();
        if (!isInstantiation()) {
            AnnotationInvocation annotationInvocation = (AnnotationInvocation) getConstructorDeclaration().getAnnotationConstructor();
            for (AnnotationArgument a2 : annotationInvocation.getAnnotationArguments()) {
                if (a2.getTerm() instanceof ParameterAnnotationTerm) {
                    if (((ParameterAnnotationTerm) a2.getTerm()).getSourceParameter().equals(aa.getParameter())) {
                        name = a2.getParameter();
                        break;
                    }
                }
            }
        }
        args.append(makeAnnotationArgument(exprGen, ai, name, parameterPath.append(aa), aa.getTerm()));
    }
    return exprGen.make().Annotation(makeAnnotationType(exprGen), args.toList());
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter)

Example 39 with Parameter

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

the class AnnotationInvocationVisitor method transformConstructorArgument.

public static JCExpression transformConstructorArgument(ExpressionTransformer exprGen, Tree.InvocationExpression invocation, Parameter classParameter, AnnotationArgument argument, com.sun.tools.javac.util.List<AnnotationFieldName> fieldPath) {
    AnnotationInvocation anno = annoCtorModel(invocation);
    AnnotationInvocationVisitor visitor = new AnnotationInvocationVisitor(exprGen, invocation, anno);
    visitor.parameter = classParameter;
    AnnotationTerm term = argument.getTerm();
    if (term instanceof ParameterAnnotationTerm) {
        ParameterAnnotationTerm parameterArgument = (ParameterAnnotationTerm) term;
        Parameter sp = parameterArgument.getSourceParameter();
        int argumentIndex = ((Functional) sp.getDeclaration()).getFirstParameterList().getParameters().indexOf(sp);
        if (invocation.getPositionalArgumentList() != null) {
            java.util.List<Tree.PositionalArgument> positionalArguments = invocation.getPositionalArgumentList().getPositionalArguments();
            if (parameterArgument.isSpread()) {
                visitor.transformSpreadArgument(positionalArguments.subList(argumentIndex, positionalArguments.size()), classParameter);
            } else {
                if (0 <= argumentIndex && argumentIndex < positionalArguments.size()) {
                    Tree.PositionalArgument pargument = positionalArguments.get(argumentIndex);
                    if (pargument.getParameter().isSequenced()) {
                        visitor.transformVarargs(argumentIndex, positionalArguments);
                    } else {
                        visitor.transformArgument(pargument);
                    }
                } else if (sp.isDefaulted()) {
                    visitor.makeDefaultExpr(invocation, parameterArgument, sp);
                } else if (sp.isSequenced()) {
                    visitor.appendBuiltArray(visitor.startArray());
                }
            }
        } else if (invocation.getNamedArgumentList() != null) {
            boolean found = false;
            for (Tree.NamedArgument na : invocation.getNamedArgumentList().getNamedArguments()) {
                Parameter parameter = na.getParameter();
                int parameterIndex = anno.indexOfConstructorParameter(parameter);
                if (parameterIndex == argumentIndex) {
                    visitor.transformArgument(na);
                    found = true;
                    break;
                }
            }
            if (!found) {
                if (sp.isDefaulted()) {
                    visitor.makeDefaultExpr(invocation, parameterArgument, sp);
                } else if (sp.isSequenced()) {
                    visitor.appendBuiltArray(visitor.startArray());
                } else {
                    visitor.append(exprGen.makeErroneous(invocation, "Unable to find argument"));
                }
            }
        }
    } else if (term instanceof LiteralAnnotationTerm) {
        visitor.append(term.makeAnnotationArgumentValue(visitor.exprGen, visitor.anno, fieldPath.append(argument)));
    } else if (term instanceof InvocationAnnotationTerm) {
        AnnotationInvocation instantiation = ((InvocationAnnotationTerm) term).getInstantiation();
        visitor.append(transformConstructor(visitor.exprGen, invocation, instantiation, fieldPath.append(argument)));
    } else {
        visitor.append(visitor.exprGen.makeErroneous(invocation, "Unable to find argument"));
    }
    return visitor.getExpression();
}
Also used : PositionalArgument(com.redhat.ceylon.compiler.typechecker.tree.Tree.PositionalArgument) Functional(com.redhat.ceylon.model.typechecker.model.Functional) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) PositionalArgument(com.redhat.ceylon.compiler.typechecker.tree.Tree.PositionalArgument)

Example 40 with Parameter

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

the class ClassTransformer method generateInstantiatorDelegate.

private void generateInstantiatorDelegate(ClassDefinitionBuilder classBuilder, Type satisfiedType, Interface iface, Class klass, Constructor ctor, Type currentType, boolean includeBody) {
    Type typeMember = satisfiedType.getTypeMember(klass, klass.getType().getTypeArgumentList());
    if (ctor != null) {
        typeMember = ctor.appliedType(typeMember, Collections.<Type>emptyList());
    }
    java.util.List<TypeParameter> typeParameters = klass.getTypeParameters();
    java.util.List<Parameter> parameters = (ctor != null ? ctor.getParameterLists() : klass.getParameterLists()).get(0).getParameters();
    long flags = PUBLIC;
    if (includeBody)
        flags |= FINAL;
    else
        flags |= ABSTRACT;
    String instantiatorMethodName = naming.getInstantiatorMethodName(klass);
    for (Parameter param : parameters) {
        if (Strategy.hasDefaultParameterValueMethod(param) && !klass.isActual()) {
            final TypedReference typedParameter = typeMember.getTypedParameter(param);
            // If that method has a defaulted parameter, 
            // we need to generate a default value method
            // which also delegates to the $impl
            final MethodDefinitionBuilder defaultValueDelegate = makeDelegateToCompanion(iface, typedParameter, currentType, flags, typeParameters, producedTypeParameterBounds(typeMember, klass), typedParameter.getFullType(), Naming.getDefaultedParamMethodName(ctor != null ? ctor : klass, param), parameters.subList(0, parameters.indexOf(param)), param.getModel().getTypeErased(), null, DelegateType.FOR_DEFAULT_VALUE, includeBody);
            classBuilder.method(defaultValueDelegate);
        }
        if (Strategy.hasDefaultParameterOverload(param)) {
            final MethodDefinitionBuilder overload = makeDelegateToCompanion(iface, typeMember, currentType, flags, typeParameters, producedTypeParameterBounds(typeMember, klass), typeMember.getType(), instantiatorMethodName, parameters.subList(0, parameters.indexOf(param)), false, null, DelegateType.OTHER, includeBody);
            classBuilder.method(overload);
        }
    }
    final MethodDefinitionBuilder overload = makeDelegateToCompanion(iface, typeMember, currentType, flags, typeParameters, producedTypeParameterBounds(typeMember, klass), typeMember.getType(), instantiatorMethodName, parameters, false, null, DelegateType.OTHER, includeBody);
    classBuilder.method(overload);
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter)

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