Search in sources :

Example 11 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

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();
        for (AnnotationConstructorParameter acp : ac.getConstructorParameters()) {
            if (acp.getParameter().equals(parameter) && acp.getDefaultArgument() != null) {
                methodBuilder.userAnnotations(acp.getDefaultArgument().makeDpmAnnotations(expressionGen()));
            }
        }
    }
    int modifiers = 0;
    if (noBody) {
        modifiers |= PUBLIC | ABSTRACT;
    } else if (container == null || !(container instanceof Class && Strategy.defaultParameterMethodStatic(container))) {
        // initializers can override parameter defaults
        modifiers |= FINAL;
    }
    if (container != null && container.isShared()) {
        modifiers |= PUBLIC;
    } else if (container == null || (!container.isToplevel() && !noBody)) {
        modifiers |= PRIVATE;
    }
    boolean staticMethod = Strategy.defaultParameterMethodStatic(container);
    if (staticMethod) {
        // static default parameter methods should be consistently public so that if non-shared class Top and
        // shared class Bottom which extends Top both have the same default param name, we don't get an error
        // if the Bottom class tries to "hide" a static public method with a private one
        modifiers &= ~PRIVATE;
        modifiers |= STATIC | PUBLIC;
    }
    methodBuilder.modifiers(modifiers);
    if (container instanceof Constructor) {
        copyTypeParameters((Class) container.getContainer(), methodBuilder);
        methodBuilder.reifiedTypeParameters(((Class) container.getContainer()).getTypeParameters());
    } else if (container instanceof Generic) {
        // make sure reified type parameters are accepted
        copyTypeParameters((Generic) container, methodBuilder);
        methodBuilder.reifiedTypeParameters(((Generic) container).getTypeParameters());
    }
    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.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 = ((Generic) 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(com.redhat.ceylon.model.typechecker.model.TypeParameter) JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) NonWideningParam(com.redhat.ceylon.compiler.java.codegen.MethodDefinitionBuilder.NonWideningParam) ThrowerCatchallConstructor(com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Generic(com.redhat.ceylon.model.typechecker.model.Generic) WideningRules(com.redhat.ceylon.compiler.java.codegen.MethodDefinitionBuilder.WideningRules) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass)

Example 12 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

the class ClassTransformer method makeGetterOrSetter.

private AttributeDefinitionBuilder makeGetterOrSetter(Tree.AttributeDeclaration decl, boolean forCompanion, boolean lazy, AttributeDefinitionBuilder builder, boolean isGetter) {
    at(decl);
    if (forCompanion || lazy) {
        SpecifierOrInitializerExpression specOrInit = decl.getSpecifierOrInitializerExpression();
        if (specOrInit != null) {
            HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(specOrInit.getExpression());
            if (error != null) {
                builder.getterBlock(make().Block(0, List.<JCStatement>of(this.makeThrowUnresolvedCompilationError(error))));
            } else {
                Value declarationModel = decl.getDeclarationModel();
                TypedReference typedRef = getTypedReference(declarationModel);
                TypedReference nonWideningTypedRef = nonWideningTypeDecl(typedRef);
                Type nonWideningType = nonWideningType(typedRef, nonWideningTypedRef);
                JCExpression expr = expressionGen().transformExpression(specOrInit.getExpression(), CodegenUtil.getBoxingStrategy(declarationModel), nonWideningType);
                expr = convertToIntIfHashAttribute(declarationModel, expr);
                builder.getterBlock(make().Block(0, List.<JCStatement>of(make().Return(expr))));
            }
        } else {
            JCExpression accessor = naming.makeQualifiedName(naming.makeQuotedThis(), decl.getDeclarationModel(), Naming.NA_MEMBER | (isGetter ? Naming.NA_GETTER : Naming.NA_SETTER));
            if (isGetter) {
                builder.getterBlock(make().Block(0, List.<JCStatement>of(make().Return(make().Apply(null, accessor, List.<JCExpression>nil())))));
            } else {
                List<JCExpression> args = List.<JCExpression>of(naming.makeName(decl.getDeclarationModel(), Naming.NA_MEMBER | Naming.NA_IDENT));
                builder.setterBlock(make().Block(0, List.<JCStatement>of(make().Exec(make().Apply(null, accessor, args)))));
            }
        }
    }
    if (forCompanion)
        builder.notActual();
    return builder.modifiers(transformAttributeGetSetDeclFlags(decl.getDeclarationModel(), forCompanion)).isFormal((Decl.isFormal(decl) || Decl.withinInterface(decl)) && !forCompanion);
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) SpecifierOrInitializerExpression(com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Example 13 with HasErrorException

use of com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException in project ceylon-compiler by ceylon.

the class ExpressionTransformer method transform.

public JCStatement transform(Tree.SpecifierStatement op) {
    // SpecifierStatement do not return any value, therefore we don't care about the type of the expressions.
    inStatement = true;
    JCStatement result;
    HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(op.getBaseMemberExpression());
    if (error != null) {
        result = this.makeThrowUnresolvedCompilationError(error);
    } else if ((error = errors().getFirstExpressionErrorAndMarkBrokenness(op.getSpecifierExpression().getExpression())) != null) {
        result = this.makeThrowUnresolvedCompilationError(error);
    } else {
        result = at(op).Exec(transformAssignment(op, op.getBaseMemberExpression(), op.getSpecifierExpression().getExpression()));
    }
    inStatement = false;
    return result;
}
Also used : HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement)

Aggregations

HasErrorException (com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException)13 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)11 Type (com.redhat.ceylon.model.typechecker.model.Type)6 JCTree (com.sun.tools.javac.tree.JCTree)6 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)5 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)5 JCBlock (com.sun.tools.javac.tree.JCTree.JCBlock)4 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)3 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)3 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)3 JCPrimitiveTypeTree (com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree)3 CustomTree (com.redhat.ceylon.compiler.typechecker.tree.CustomTree)2 SpecifierOrInitializerExpression (com.redhat.ceylon.compiler.typechecker.tree.Tree.SpecifierOrInitializerExpression)2 Statement (com.redhat.ceylon.compiler.typechecker.tree.Tree.Statement)2 Constructor (com.redhat.ceylon.model.typechecker.model.Constructor)2 Function (com.redhat.ceylon.model.typechecker.model.Function)2 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)2 Functional (com.redhat.ceylon.model.typechecker.model.Functional)2 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)2 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)2