Search in sources :

Example 11 with Generic

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

the class ClassTransformer method makeParamDefaultValueMethod.

/**
 * Creates a (possibly abstract) method for retrieving the value for a
 * defaulted parameter
 * @param typeParameterList
 */
MethodDefinitionBuilder makeParamDefaultValueMethod(boolean noBody, Declaration container, Tree.ParameterList params, Tree.Parameter currentParam) {
    at(currentParam);
    Parameter parameter = currentParam.getParameterModel();
    if (!Strategy.hasDefaultParameterValueMethod(parameter)) {
        throw new BugException();
    }
    MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.systemMethod(this, Naming.getDefaultedParamMethodName(container, parameter));
    methodBuilder.ignoreModelAnnotations();
    if (container != null && Decl.isAnnotationConstructor(container)) {
        AnnotationInvocation ac = (AnnotationInvocation) ((Function) container).getAnnotationConstructor();
        AnnotationConstructorParameter acp = ac.findConstructorParameter(parameter);
        if (acp != null && acp.getDefaultArgument() != null) {
            methodBuilder.userAnnotations(acp.getDefaultArgument().makeDpmAnnotations(expressionGen()));
        }
    }
    methodBuilder.modifiers(modifierTransformation().defaultParameterMethod(noBody, container));
    if (container instanceof Constructor) {
        copyTypeParameters((Class) container.getContainer(), methodBuilder);
        methodBuilder.reifiedTypeParameters(((Class) container.getContainer()).getTypeParameters());
    } else if (container instanceof Declaration) {
        // make sure reified type parameters are accepted
        copyTypeParameters((Declaration) container, methodBuilder);
        methodBuilder.reifiedTypeParameters(Strategy.getEffectiveTypeParameters(container));
    }
    boolean staticMethod = container != null && Strategy.defaultParameterMethodStatic(container);
    WideningRules wideningRules = !staticMethod && container instanceof Class ? WideningRules.CAN_WIDEN : WideningRules.NONE;
    // Add any of the preceding parameters as parameters to the method
    for (Tree.Parameter p : params.getParameters()) {
        if (p.equals(currentParam)) {
            break;
        }
        at(p);
        methodBuilder.parameter(p, p.getParameterModel(), null, 0, wideningRules);
    }
    // The method's return type is the same as the parameter's type
    NonWideningParam nonWideningParam = methodBuilder.getNonWideningParam(currentParam.getParameterModel().getModel(), wideningRules);
    methodBuilder.resultType(nonWideningParam.nonWideningDecl, nonWideningParam.nonWideningType, nonWideningParam.flags);
    // The implementation of the method
    if (noBody) {
        methodBuilder.noBody();
    } else {
        HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(Decl.getDefaultArgument(currentParam).getExpression());
        if (error != null) {
            methodBuilder.body(this.makeThrowUnresolvedCompilationError(error));
        } else {
            java.util.List<TypeParameter> copiedTypeParameters = null;
            if (container instanceof Generic) {
                copiedTypeParameters = container.getTypeParameters();
                if (copiedTypeParameters != null)
                    addTypeParameterSubstitution(copiedTypeParameters);
            }
            try {
                JCExpression expr = expressionGen().transform(currentParam);
                JCBlock body = at(currentParam).Block(0, List.<JCStatement>of(at(currentParam).Return(expr)));
                methodBuilder.block(body);
            } finally {
                if (copiedTypeParameters != null)
                    popTypeParameterSubstitution();
            }
        }
    }
    return methodBuilder;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) NonWideningParam(org.eclipse.ceylon.compiler.java.codegen.MethodDefinitionBuilder.NonWideningParam) ThrowerCatchallConstructor(org.eclipse.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(org.eclipse.ceylon.model.typechecker.model.Constructor) Generic(org.eclipse.ceylon.model.typechecker.model.Generic) WideningRules(org.eclipse.ceylon.compiler.java.codegen.MethodDefinitionBuilder.WideningRules) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) HasErrorException(org.eclipse.ceylon.compiler.java.codegen.recovery.HasErrorException) Parameter(org.eclipse.ceylon.model.typechecker.model.Parameter) TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCPrimitiveTypeTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) Class(org.eclipse.ceylon.model.typechecker.model.Class) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(org.eclipse.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration)

Aggregations

Generic (org.eclipse.ceylon.model.typechecker.model.Generic)11 TypeParameter (org.eclipse.ceylon.model.typechecker.model.TypeParameter)9 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)6 ArrayList (java.util.ArrayList)5 Type (org.eclipse.ceylon.model.typechecker.model.Type)5 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)5 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)5 HashMap (java.util.HashMap)4 Constructor (org.eclipse.ceylon.model.typechecker.model.Constructor)4 Scope (org.eclipse.ceylon.model.typechecker.model.Scope)4 Map (java.util.Map)3 FunctionOrValue (org.eclipse.ceylon.model.typechecker.model.FunctionOrValue)3 NothingType (org.eclipse.ceylon.model.typechecker.model.NothingType)3 ParameterList (org.eclipse.ceylon.model.typechecker.model.ParameterList)3 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)3 List (java.util.List)2 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)2 Class (org.eclipse.ceylon.model.typechecker.model.Class)2 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)2 Functional (org.eclipse.ceylon.model.typechecker.model.Functional)2