Search in sources :

Example 16 with Value

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

the class ClassTransformer method addWriteReplace.

/**
     * Adds a write replace method which replaces value constructor instances 
     * with a SerializationProxy
     * @param model
     * @param classBuilder
     */
protected void addWriteReplace(final Class model, ClassDefinitionBuilder classBuilder) {
    MethodDefinitionBuilder mdb = MethodDefinitionBuilder.systemMethod(this, "writeReplace");
    mdb.resultType(null, make().Type(syms().objectType));
    mdb.modifiers(PRIVATE | FINAL);
    ListBuffer<JCStatement> stmts = ListBuffer.<JCStatement>lb();
    SyntheticName name = naming.synthetic(Unfix.$name$);
    stmts.add(makeVar(FINAL, name, make().Type(syms().stringType), null));
    if (model.hasEnumerated()) {
        JCStatement tail;
        if (Decl.hasOnlyValueConstructors(model)) {
            tail = make().Throw(statementGen().makeNewEnumeratedTypeError("Instance not of any constructor"));
        } else {
            tail = make().Return(naming.makeThis());
        }
        for (Declaration member : model.getMembers()) {
            if (Decl.isValueConstructor(member)) {
                Value val = (Value) member;
                tail = make().If(make().Binary(JCTree.EQ, naming.makeThis(), naming.getValueConstructorFieldName(val).makeIdent()), make().Block(0, List.<JCStatement>of(make().Exec(make().Assign(name.makeIdent(), make().Literal(Naming.getGetterName(member)))))), tail);
            }
        }
        stmts.add(tail);
    } else if (model.isAnonymous()) {
        stmts.add(make().Exec(make().Assign(name.makeIdent(), make().Literal(Naming.getGetterName((Value) model.getContainer().getDirectMember(model.getName(), null, false))))));
    } else {
        throw new BugException("Unsupported need for writeReplace()");
    }
    // final String name;
    // if(this == instA) {
    //    name = "getInstA";
    // } // ... else { throw new 
    // return new SerializationProxy(outer, Foo.clazz, name);
    List<JCExpression> args = List.<JCExpression>of(name.makeIdent());
    if (model.isMember()) {
        ClassOrInterface outer = (ClassOrInterface) model.getContainer();
        args = args.prepend(makeClassLiteral(outer.getType()));
        args = args.prepend(naming.makeQualifiedThis(naming.makeTypeDeclarationExpression(null, outer, DeclNameFlag.QUALIFIED)));
    } else {
        args = args.prepend(makeClassLiteral(model.getType()));
    }
    stmts.add(make().Return(make().NewClass(null, null, make().QualIdent(syms().ceylonSerializationProxyType.tsym), args, null)));
    mdb.body(stmts.toList());
    classBuilder.method(mdb);
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Example 17 with Value

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

the class CeylonVisitor method transformSingletonConstructor.

protected void transformSingletonConstructor(HashMap<Constructor, CtorDelegation> delegates, Tree.Enumerated ctor) {
    // generate a constructor
    transformConstructor(ctor, //ctor.getParameterList(), 
    null, ctor.getDelegatedConstructor(), ctor.getBlock(), ctor.getEnumerated(), delegates);
    Class clz = Decl.getConstructedClass(ctor.getEnumerated());
    Value singletonModel = ctor.getDeclarationModel();
    // generate a field
    AttributeDefinitionBuilder adb = AttributeDefinitionBuilder.singleton(gen, //gen.naming.makeTypeDeclarationName(Decl.getConstructedClass(ctor.getEnumerated())), 
    null, null, singletonModel.getName(), singletonModel, false);
    adb.modelAnnotations(gen.makeAtEnumerated());
    adb.modelAnnotations(gen.makeAtIgnore());
    adb.userAnnotations(gen.expressionGen().transformAnnotations(OutputElement.GETTER, ctor));
    adb.fieldAnnotations(gen.expressionGen().transformAnnotations(OutputElement.FIELD, ctor));
    // not setter
    adb.immutable();
    SyntheticName field = gen.naming.getValueConstructorFieldName(singletonModel);
    if (clz.isToplevel()) {
        adb.modifiers((singletonModel.isShared() ? PUBLIC : PRIVATE) | STATIC | FINAL);
        adb.initialValue(gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null));
        classBuilder.defs(adb.build());
    } else if (clz.isClassMember()) {
        adb.modifiers(singletonModel.isShared() ? 0 : PRIVATE);
        // lazy
        adb.initialValue(gen.makeNull());
        List<JCStatement> l = List.<JCStatement>of(gen.make().If(gen.make().Binary(JCTree.EQ, field.makeIdent(), gen.makeNull()), gen.make().Exec(gen.make().Assign(field.makeIdent(), gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null))), null), gen.make().Return(field.makeIdent()));
        adb.getterBlock(gen.make().Block(0, l));
        classBuilder.getContainingClassBuilder().defs(gen.makeVar(PRIVATE | TRANSIENT, field, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), gen.makeNull()));
        classBuilder.getContainingClassBuilder().defs(adb.build());
    } else {
        // LOCAL
        classBuilder.after(gen.makeVar(FINAL, field, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), gen.make().NewClass(null, null, gen.naming.makeTypeDeclarationExpression(null, Decl.getConstructedClass(ctor.getEnumerated())), List.<JCExpression>of(gen.make().TypeCast(gen.naming.makeNamedConstructorType(ctor.getEnumerated(), false), gen.makeNull())), null)));
        gen.naming.addVariableSubst(singletonModel, field.getName());
    }
}
Also used : JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) Value(com.redhat.ceylon.model.typechecker.model.Value) SyntheticName(com.redhat.ceylon.compiler.java.codegen.Naming.SyntheticName) Class(com.redhat.ceylon.model.typechecker.model.Class) List(com.sun.tools.javac.util.List)

Example 18 with Value

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

the class ClassTransformer method addAmbiguousMember.

private void addAmbiguousMember(ClassDefinitionBuilder classBuilder, Interface model, String name) {
    Declaration member = model.getMember(name, null, false);
    Type satisfiedType = model.getType().getSupertype(model);
    if (member instanceof Class) {
        Class klass = (Class) member;
        if (Strategy.generateInstantiator(member) && !klass.hasConstructors()) {
            // instantiator method implementation
            generateInstantiatorDelegate(classBuilder, satisfiedType, model, klass, null, model.getType(), false);
        }
        if (klass.hasConstructors()) {
            for (Declaration m : klass.getMembers()) {
                if (m instanceof Constructor && Strategy.generateInstantiator(m)) {
                    Constructor ctor = (Constructor) m;
                    generateInstantiatorDelegate(classBuilder, satisfiedType, model, klass, ctor, model.getType(), false);
                }
            }
        }
    } else if (member instanceof Function) {
        Function method = (Function) member;
        final TypedReference typedMember = satisfiedType.getTypedMember(method, Collections.<Type>emptyList());
        java.util.List<java.util.List<Type>> producedTypeParameterBounds = producedTypeParameterBounds(typedMember, method);
        final java.util.List<TypeParameter> typeParameters = method.getTypeParameters();
        final java.util.List<Parameter> parameters = method.getFirstParameterList().getParameters();
        for (Parameter param : parameters) {
            if (Strategy.hasDefaultParameterOverload(param)) {
                MethodDefinitionBuilder overload = new DefaultedArgumentMethodTyped(null, MethodDefinitionBuilder.method(this, method), typedMember, true).makeOverload(method.getFirstParameterList(), param, typeParameters);
                overload.modifiers(PUBLIC | ABSTRACT);
                classBuilder.method(overload);
            }
        }
        final MethodDefinitionBuilder concreteMemberDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, method.getTypeParameters(), producedTypeParameterBounds, typedMember.getType(), naming.selector(method), method.getFirstParameterList().getParameters(), ((Function) member).getTypeErased(), null, DelegateType.OTHER, false);
        classBuilder.method(concreteMemberDelegate);
    } else if (member instanceof Value || member instanceof Setter) {
        TypedDeclaration attr = (TypedDeclaration) member;
        final TypedReference typedMember = satisfiedType.getTypedMember(attr, Collections.<Type>emptyList());
        if (member instanceof Value) {
            final MethodDefinitionBuilder getterDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typedMember.getType(), Naming.getGetterName(attr), Collections.<Parameter>emptyList(), attr.getTypeErased(), null, DelegateType.OTHER, false);
            classBuilder.method(getterDelegate);
        }
        if (member instanceof Setter) {
            final MethodDefinitionBuilder setterDelegate = makeDelegateToCompanion(null, typedMember, model.getType(), PUBLIC | ABSTRACT, Collections.<TypeParameter>emptyList(), Collections.<java.util.List<Type>>emptyList(), typeFact().getAnythingType(), Naming.getSetterName(attr), Collections.<Parameter>singletonList(((Setter) member).getParameter()), ((Setter) member).getTypeErased(), null, DelegateType.OTHER, false);
            classBuilder.method(setterDelegate);
        }
    }
}
Also used : TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) ThrowerCatchallConstructor(com.redhat.ceylon.compiler.java.codegen.recovery.ThrowerCatchallConstructor) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Setter(com.redhat.ceylon.model.typechecker.model.Setter) TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Parameter(com.redhat.ceylon.model.typechecker.model.Parameter) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) ArrayList(java.util.ArrayList) AnnotationList(com.redhat.ceylon.compiler.typechecker.tree.Tree.AnnotationList) List(com.sun.tools.javac.util.List) ParameterList(com.redhat.ceylon.model.typechecker.model.ParameterList) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)

Example 19 with Value

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

the class ClassTransformer method addMissingUnrefinedMembers.

/** 
     * Recover from members not being refined in the class hierarchy 
     * by generating a stub method that throws.
     */
private void addMissingUnrefinedMembers(Node def, Class classModel, ClassDefinitionBuilder classBuilder) {
    for (Reference unrefined : classModel.getUnimplementedFormals()) {
        //classModel.getMember(memberName, null, false);
        Declaration formalMember = unrefined.getDeclaration();
        String errorMessage = "formal member '" + formalMember.getName() + "' of '" + ((TypeDeclaration) formalMember.getContainer()).getName() + "' not implemented in class hierarchy";
        java.util.List<Type> params = new java.util.ArrayList<Type>();
        if (formalMember instanceof Generic) {
            for (TypeParameter tp : ((Generic) formalMember).getTypeParameters()) {
                params.add(tp.getType());
            }
        }
        if (formalMember instanceof Value) {
            addRefinedThrowerAttribute(classBuilder, errorMessage, classModel, (Value) formalMember);
        } else if (formalMember instanceof Function) {
            addRefinedThrowerMethod(classBuilder, errorMessage, classModel, (Function) formalMember);
        } else if (formalMember instanceof Class && formalMember.isClassMember()) {
            addRefinedThrowerInstantiatorMethod(classBuilder, errorMessage, classModel, (Class) formalMember, unrefined);
        }
    // formal member class of interface handled in
    // makeDelegateToCompanion()
    }
}
Also used : TypeParameter(com.redhat.ceylon.model.typechecker.model.TypeParameter) Reference(com.redhat.ceylon.model.typechecker.model.Reference) TypedReference(com.redhat.ceylon.model.typechecker.model.TypedReference) Generic(com.redhat.ceylon.model.typechecker.model.Generic) ArrayList(java.util.ArrayList) Function(com.redhat.ceylon.model.typechecker.model.Function) Type(com.redhat.ceylon.model.typechecker.model.Type) FunctionOrValue(com.redhat.ceylon.model.typechecker.model.FunctionOrValue) JavaBeanValue(com.redhat.ceylon.model.loader.model.JavaBeanValue) Value(com.redhat.ceylon.model.typechecker.model.Value) Class(com.redhat.ceylon.model.typechecker.model.Class) JCNewClass(com.sun.tools.javac.tree.JCTree.JCNewClass) TypedDeclaration(com.redhat.ceylon.model.typechecker.model.TypedDeclaration) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration) MethodDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.MethodDeclaration) AttributeDeclaration(com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 20 with Value

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

the class DefiniteAssignmentVisitor method visit.

public void visit(Tree.SpecifierStatement stmt) {
    Tree.Term bme = stmt.getBaseMemberExpression();
    if (bme instanceof Tree.MemberOrTypeExpression) {
        Declaration decl = ((Tree.MemberOrTypeExpression) bme).getDeclaration();
        if (// non-variable and deferred
        tracked.containsKey(decl) && // specification is in a for/else
        forBlock != null && !forBlock.equals(tracked.get(decl))) {
            // not declared in *this* for/else
            if (elseBlock == null) {
                ((Value) decl).setSpecifiedInForElse(true);
            }
            ControlBlock assigningBlock = elseBlock != null ? elseBlock : forBlock;
            Set<Value> assigned = assigningBlock.getSpecifiedValues();
            if (assigned == null) {
                assigned = new HashSet<Value>(1);
                assigningBlock.setSpecifiedValues(assigned);
            }
            assigned.add((Value) decl);
        }
    }
    super.visit(stmt);
}
Also used : ControlBlock(com.redhat.ceylon.model.typechecker.model.ControlBlock) Value(com.redhat.ceylon.model.typechecker.model.Value) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration)

Aggregations

Value (com.redhat.ceylon.model.typechecker.model.Value)54 FunctionOrValue (com.redhat.ceylon.model.typechecker.model.FunctionOrValue)39 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)28 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)27 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)27 Function (com.redhat.ceylon.model.typechecker.model.Function)25 Type (com.redhat.ceylon.model.typechecker.model.Type)23 Class (com.redhat.ceylon.model.typechecker.model.Class)19 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)19 JavaBeanValue (com.redhat.ceylon.model.loader.model.JavaBeanValue)16 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)15 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)14 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)14 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)13 Parameter (com.redhat.ceylon.model.typechecker.model.Parameter)12 JCTree (com.sun.tools.javac.tree.JCTree)12 AttributeDeclaration (com.redhat.ceylon.compiler.typechecker.tree.Tree.AttributeDeclaration)11 FieldValue (com.redhat.ceylon.model.loader.model.FieldValue)11 ParameterList (com.redhat.ceylon.model.typechecker.model.ParameterList)11 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)11