Search in sources :

Example 81 with Type

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

the class CeylonVisitor method visit.

public void visit(Tree.ExtendedType extendedType) {
    ClassOrInterface forDefinition = classBuilder.getForDefinition();
    Type thisType = forDefinition != null ? forDefinition.getType() : null;
    Type extended = extendedType.getType().getTypeModel();
    if (extended.getDeclaration() instanceof Constructor) {
        extended = extended.getQualifyingType();
    }
    classBuilder.extending(thisType, extended);
    gen.expressionGen().transformSuperInvocation(extendedType, classBuilder);
}
Also used : ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface) Type(com.redhat.ceylon.model.typechecker.model.Type) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor)

Example 82 with Type

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

the class CeylonVisitor method visit.

public void visit(Tree.ClassBody that) {
    // Transform executable statements and declarations in the body
    // except constructors. Record how constructors delegate.
    HashMap<Constructor, CtorDelegation> delegates = new HashMap<Constructor, CtorDelegation>();
    java.util.List<Statement> stmts = getBodyStatements(that);
    HashMap<Constructor, CtorDelegation> broken = new HashMap<Constructor, CtorDelegation>();
    for (Tree.Statement stmt : stmts) {
        if (stmt instanceof Tree.Constructor) {
            Tree.Constructor ctor = (Tree.Constructor) stmt;
            Constructor ctorModel = ctor.getConstructor();
            if (gen.errors().hasDeclarationAndMarkBrokenness(ctor) instanceof Drop) {
                broken.put(ctorModel, CtorDelegation.brokenDelegation(ctorModel));
                continue;
            }
            classBuilder.getInitBuilder().constructor(ctor);
            if (ctor.getDelegatedConstructor() != null) {
                // error recovery
                if (ctor.getDelegatedConstructor().getInvocationExpression() != null) {
                    Tree.ExtendedTypeExpression p = (Tree.ExtendedTypeExpression) ctor.getDelegatedConstructor().getInvocationExpression().getPrimary();
                    Declaration delegatedDecl = p.getDeclaration();
                    delegates.put(ctorModel, ctorDelegation(ctorModel, delegatedDecl, broken));
                }
            } else {
                // implicitly delegating to superclass initializer
                Type et = Decl.getConstructedClass(ctorModel).getExtendedType();
                if (et != null) {
                    Declaration delegatedDecl = et.getDeclaration();
                    delegates.put(ctorModel, ctorDelegation(ctorModel, delegatedDecl, broken));
                }
            }
        } else if (stmt instanceof Tree.Enumerated) {
            Tree.Enumerated singleton = (Tree.Enumerated) stmt;
            Constructor ctorModel = singleton.getEnumerated();
            if (gen.errors().hasDeclarationAndMarkBrokenness(singleton) instanceof Drop) {
                broken.put(ctorModel, CtorDelegation.brokenDelegation(ctorModel));
                continue;
            }
            classBuilder.getInitBuilder().singleton(singleton);
            if (singleton.getDelegatedConstructor() != null) {
                Tree.ExtendedTypeExpression p = (Tree.ExtendedTypeExpression) singleton.getDelegatedConstructor().getInvocationExpression().getPrimary();
                Declaration delegatedDecl = p.getDeclaration();
                delegates.put(ctorModel, ctorDelegation(ctorModel, delegatedDecl, broken));
            } else {
                // implicitly delegating to superclass initializer
                Type et = Decl.getConstructedClass(ctorModel).getExtendedType();
                if (et != null) {
                    Declaration delegatedDecl = et.getDeclaration();
                    delegates.put(ctorModel, ctorDelegation(ctorModel, delegatedDecl, broken));
                }
            }
        } else {
            HasErrorException error = gen.errors().getFirstErrorInitializer(stmt);
            if (error != null) {
                append(gen.makeThrowUnresolvedCompilationError(error));
            } else {
                stmt.visit(this);
            }
        }
    }
    // Now transform constructors
    for (Tree.Statement stmt : stmts) {
        if (stmt instanceof Tree.Constructor) {
            Tree.Constructor ctor = (Tree.Constructor) stmt;
            if (gen.errors().hasDeclarationError(ctor) instanceof Drop) {
                continue;
            }
            transformConstructor(ctor, ctor.getParameterList(), ctor.getDelegatedConstructor(), ctor.getBlock(), ctor.getConstructor(), delegates);
        } else if (stmt instanceof Tree.Enumerated) {
            Tree.Enumerated ctor = (Tree.Enumerated) stmt;
            if (gen.errors().hasDeclarationError(ctor) instanceof Drop) {
                continue;
            }
            transformSingletonConstructor(delegates, ctor);
        }
    }
}
Also used : HashMap(java.util.HashMap) Constructor(com.redhat.ceylon.model.typechecker.model.Constructor) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) Statement(com.redhat.ceylon.compiler.typechecker.tree.Tree.Statement) Drop(com.redhat.ceylon.compiler.java.codegen.recovery.Drop) Type(com.redhat.ceylon.model.typechecker.model.Type) Statement(com.redhat.ceylon.compiler.typechecker.tree.Tree.Statement) HasErrorException(com.redhat.ceylon.compiler.java.codegen.recovery.HasErrorException) CustomTree(com.redhat.ceylon.compiler.typechecker.tree.CustomTree) JCTree(com.sun.tools.javac.tree.JCTree) Tree(com.redhat.ceylon.compiler.typechecker.tree.Tree) Declaration(com.redhat.ceylon.model.typechecker.model.Declaration) TypeDeclaration(com.redhat.ceylon.model.typechecker.model.TypeDeclaration)

Example 83 with Type

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

the class ClassTransformer method transformAnnotationMethodType.

private JCExpression transformAnnotationMethodType(Tree.Parameter parameter) {
    Type parameterType = parameter.getParameterModel().getType();
    JCExpression type = null;
    if (isScalarAnnotationParameter(parameterType)) {
        type = makeJavaType(parameterType.withoutUnderlyingType(), JT_ANNOTATION);
    } else if (isMetamodelReference(parameterType)) {
        type = make().Type(syms().stringType);
    } else if (Decl.isEnumeratedTypeWithAnonCases(parameterType)) {
        type = makeJavaClassTypeBounded(parameterType);
    } else if (typeFact().isIterableType(parameterType)) {
        Type iteratedType = typeFact().getIteratedType(parameterType);
        if (isScalarAnnotationParameter(iteratedType)) {
            JCExpression scalarType = makeJavaType(iteratedType, JT_ANNOTATION);
            type = make().TypeArray(scalarType);
        } else if (isMetamodelReference(iteratedType)) {
            JCExpression scalarType = make().Type(syms().stringType);
            type = make().TypeArray(scalarType);
        } else if (Decl.isEnumeratedTypeWithAnonCases(iteratedType)) {
            JCExpression scalarType = makeJavaClassTypeBounded(iteratedType);
            type = make().TypeArray(scalarType);
        }
    }
    if (type == null) {
        type = makeErroneous(parameter, "compiler bug: " + parameter.getParameterModel().getName() + " has an unsupported annotation parameter type");
    }
    return type;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression)

Example 84 with Type

use of com.redhat.ceylon.model.typechecker.model.Type 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 85 with Type

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

the class ClassDefinitionBuilder method refineReifiedType.

public ClassDefinitionBuilder refineReifiedType(Type thisType) {
    // init: $type$impl.$refine(tp1, tp2...)
    Interface iface = (Interface) thisType.getDeclaration();
    String companion = gen.naming.getCompanionFieldName(iface);
    ListBuffer<JCExpression> typeParameters = new ListBuffer<JCExpression>();
    for (Type tp : thisType.getTypeArgumentList()) {
        typeParameters.add(gen.makeReifiedTypeArgument(tp));
    }
    JCExpression refine = gen.make().Apply(null, gen.makeSelect(companion, gen.naming.getRefineTypeParametersMethodName()), typeParameters.toList());
    initBuilder.init(gen.make().Exec(refine));
    return this;
}
Also used : Type(com.redhat.ceylon.model.typechecker.model.Type) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) ListBuffer(com.sun.tools.javac.util.ListBuffer) Interface(com.redhat.ceylon.model.typechecker.model.Interface) ClassOrInterface(com.redhat.ceylon.model.typechecker.model.ClassOrInterface)

Aggregations

Type (com.redhat.ceylon.model.typechecker.model.Type)237 TypeDeclaration (com.redhat.ceylon.model.typechecker.model.TypeDeclaration)98 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)87 TypeParameter (com.redhat.ceylon.model.typechecker.model.TypeParameter)56 JCTree (com.sun.tools.javac.tree.JCTree)53 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)51 ModelUtil.appliedType (com.redhat.ceylon.model.typechecker.model.ModelUtil.appliedType)46 Class (com.redhat.ceylon.model.typechecker.model.Class)45 ClassOrInterface (com.redhat.ceylon.model.typechecker.model.ClassOrInterface)43 TypedDeclaration (com.redhat.ceylon.model.typechecker.model.TypedDeclaration)41 IntersectionType (com.redhat.ceylon.model.typechecker.model.IntersectionType)37 UnionType (com.redhat.ceylon.model.typechecker.model.UnionType)37 Test (org.junit.Test)37 TypeParser (com.redhat.ceylon.model.loader.TypeParser)36 Declaration (com.redhat.ceylon.model.typechecker.model.Declaration)34 Function (com.redhat.ceylon.model.typechecker.model.Function)33 Interface (com.redhat.ceylon.model.typechecker.model.Interface)30 JCTypeParameter (com.sun.tools.javac.tree.JCTree.JCTypeParameter)30 TypedReference (com.redhat.ceylon.model.typechecker.model.TypedReference)29 ArrayList (java.util.ArrayList)28