Package com.redhat.ceylon.compiler.typechecker.model

Examples of com.redhat.ceylon.compiler.typechecker.model.TypeParameter


    @SuppressWarnings("deprecation")
    private void setTypeParameters(Scope scope, List<TypeParameter> params, List<TypeParameterMirror> typeParameters, boolean isCeylon) {
        // We must first add every type param, before we resolve the bounds, which can
        // refer to type params.
        for(TypeParameterMirror typeParam : typeParameters){
            TypeParameter param = new TypeParameter();
            param.setUnit(((Element)scope).getUnit());
            param.setContainer(scope);
            param.setScope(scope);
            DeclarationVisitor.setVisibleScope(param);
            param.setDeclaration((Declaration) scope);
            // let's not trigger the lazy-loading if we're completing a LazyClass/LazyInterface
            if(scope instanceof LazyContainer)
                ((LazyContainer)scope).addMember(param);
            else // must be a method
                scope.getMembers().add(param);
            param.setName(typeParam.getName());
            param.setExtendedType(typeFactory.getAnythingDeclaration().getType());
            params.add(param);
        }
        boolean needsObjectBounds = !isCeylon && scope instanceof Method;
        // Now all type params have been set, we can resolve the references parts
        Iterator<TypeParameter> paramsIterator = params.iterator();
        for(TypeParameterMirror typeParam : typeParameters){
            TypeParameter param = paramsIterator.next();
            List<TypeMirror> bounds = typeParam.getBounds();
            for(TypeMirror bound : bounds){
                ProducedType boundType;
                // we turn java's default upper bound java.lang.Object into ceylon.language.Object
                if(sameType(bound, OBJECT_TYPE)){
                    // avoid adding java's default upper bound if it's just there with no meaning,
                    // especially since we do not want it for types
                    if(bounds.size() == 1)
                        break;
                    boundType = getNonPrimitiveType(getLanguageModule(), CEYLON_OBJECT_TYPE, scope, VarianceLocation.INVARIANT);
                }else
                    boundType = getNonPrimitiveType(Decl.getModuleContainer(scope), bound, scope, VarianceLocation.INVARIANT);
                param.getSatisfiedTypes().add(boundType);
            }
            if(needsObjectBounds && param.getSatisfiedTypes().isEmpty()){
                ProducedType boundType = getNonPrimitiveType(getLanguageModule(), CEYLON_OBJECT_TYPE, scope, VarianceLocation.INVARIANT);
                param.getSatisfiedTypes().add(boundType);
            }
        }
    }
View Full Code Here


    private JCTypeParameter makeTypeParameter(String name, java.util.List<ProducedType> satisfiedTypes, boolean covariant, boolean contravariant) {
        return make().TypeParameter(names().fromString(name), makeTypeParameterBounds(satisfiedTypes));
    }

    JCTypeParameter makeTypeParameter(TypeParameter declarationModel, java.util.List<ProducedType> satisfiedTypesForBounds) {
        TypeParameter typeParameterForBounds = declarationModel;
        if (satisfiedTypesForBounds == null) {
            satisfiedTypesForBounds = declarationModel.getSatisfiedTypes();
        }
        // special case for method refinenement where Java doesn't let us refine the parameter bounds
        if(declarationModel.getContainer() instanceof Method){
            Method method = (Method) declarationModel.getContainer();
            Method refinedMethod = (Method) method.getRefinedDeclaration();
            if (!Decl.equal(method, refinedMethod)) {
                // find the param index
                int index = method.getTypeParameters().indexOf(declarationModel);
                if(index == -1){
                    log.error("Failed to find type parameter index: "+declarationModel.getName());
                }else if(refinedMethod.getTypeParameters().size() > index){
                    // ignore smaller index than size since the typechecker would have found the error
                    TypeParameter refinedTP = refinedMethod.getTypeParameters().get(index);
                    if(!haveSameBounds(declarationModel, refinedTP)){
                        // find the right instantiation of that type parameter
                        TypeDeclaration methodContainer = (TypeDeclaration) method.getContainer();
                        TypeDeclaration refinedMethodContainer = (TypeDeclaration) refinedMethod.getContainer();
                        // find the supertype that gave us that method and its type arguments
                        Map<TypeParameter, ProducedType> typeArguments = methodContainer.getType().getSupertype(refinedMethodContainer).getTypeArguments();
                        satisfiedTypesForBounds = new ArrayList<ProducedType>(refinedTP.getSatisfiedTypes().size());
                        for(ProducedType satisfiedType : refinedTP.getSatisfiedTypes()){
                            // substitute the refined type parameter bounds with the right type arguments
                            satisfiedTypesForBounds.add(satisfiedType.substitute(typeArguments));
                        }
                        typeParameterForBounds = refinedTP;
                    }
View Full Code Here

            }else{
                return make().Apply(null, makeSelect(makeTypeDescriptorType(), "member"),
                                                     List.of(containerType, classDescriptor));
            }
        } else if(declaration instanceof TypeParameter){
            TypeParameter tp = (TypeParameter) declaration;
            String name = naming.getTypeArgumentDescriptorName(tp);
            if(!qualified)
                return makeUnquotedIdent(name);
            Scope container = tp.getContainer();
            JCExpression qualifier = null;
            if(container instanceof Class){
                qualifier = naming.makeQualifiedThis(makeJavaType(((Class)container).getType(), JT_RAW));
            }else if(container instanceof Interface){
                qualifier = naming.makeQualifiedThis(makeJavaType(((Interface)container).getType(), JT_COMPANION | JT_RAW));
View Full Code Here

        }
       
        private Class makeParameterisedClass(String name, Class container) {
            Class klass = makeClass(name, container);
            List<TypeParameter> typeParameters = new ArrayList<TypeParameter>(2);
            TypeParameter typeParam = new TypeParameter();
            typeParam.setName("A");
            typeParameters.add(typeParam);
            typeParam = new TypeParameter();
            typeParam.setName("B");
            typeParameters.add(typeParam);
            klass.setTypeParameters(typeParameters );
            return klass;
        }
View Full Code Here

        refinedDeclType = simplifyType(refinedDeclType);
        // special case for type parameters
        if(declType.getDeclaration() instanceof TypeParameter
                && refinedDeclType.getDeclaration() instanceof TypeParameter){
            // consider them equivalent if they have the same bounds
            TypeParameter tp = (TypeParameter) declType.getDeclaration();
            TypeParameter refinedTP = (TypeParameter) refinedDeclType.getDeclaration();
           
            if(haveSameBounds(tp, refinedTP))
                return false;
            // if they don't have the same bounds and we don't allow subtypes then we're widening
            if(!allowSubtypes)
                return false;
            // if we allow subtypes, we're widening if tp is not a subtype of refinedTP
            return !tp.getType().isSubtypeOf(refinedTP.getType());
        }
        if(allowSubtypes){
           
            if((willEraseToObject(refinedDeclType))){
                // if we refine something that erases to object, and:
View Full Code Here

            // if the refined type is a method TypeParam, use the original decl that will be more correct,
            // since it may have changed name
            if(refinedType.getDeclaration() instanceof TypeParameter
                    && refinedType.getDeclaration().getContainer() instanceof Method){
                // find its index in the refined declaration
                TypeParameter refinedTypeParameter = (TypeParameter) refinedType.getDeclaration();
                Method refinedMethod = (Method) refinedTypeParameter.getContainer();
                int i=0;
                for(TypeParameter tp : refinedMethod.getTypeParameters()){
                    if(tp.getName().equals(refinedTypeParameter.getName()))
                        break;
                    i++;
                }
                if(i >= refinedMethod.getTypeParameters().size()){
                    throw new BugException("can't find type parameter "+refinedTypeParameter.getName()+" in its container "+refinedMethod.getName());
                }
                // the refining method type parameter should be at the same index
                if(declaration.getDeclaration() instanceof Method == false)
                    throw new BugException("refining declaration is not a method: "+declaration);
                Method refiningMethod = (Method) declaration.getDeclaration();
View Full Code Here

        }
        // we can optimise it if we've got a ClassOrInterface with only Anything type parameters
        if(type.getDeclaration() instanceof ClassOrInterface == false)
            return false;
        for(Entry<TypeParameter, ProducedType> entry : type.getTypeArguments().entrySet()){
            TypeParameter tp = entry.getKey();
            if(!type.isCovariant(tp)) {
                return false;
            }
            java.util.List<ProducedType> bounds = tp.getSatisfiedTypes();
            ProducedType ta = entry.getValue();
            if ((bounds == null || bounds.isEmpty()) && !isAnything(ta)) {
                return false;
            }
            for (ProducedType bound : bounds) {
View Full Code Here

    }
   
    boolean hasConstrainedTypeParameters(ProducedType type) {
        TypeDeclaration declaration = type.getDeclaration();
        if(declaration instanceof TypeParameter){
            TypeParameter tp = (TypeParameter) declaration;
            return !tp.getSatisfiedTypes().isEmpty();
        }
        if(declaration instanceof UnionType){
            for(ProducedType m : declaration.getCaseTypes())
                if(hasConstrainedTypeParameters(m))
                    return true;
View Full Code Here

        }
   
        protected void compareTypeParameters(String name, List<TypeParameter> validTypeParameters, List<TypeParameter> modelTypeParameters) {
            Assert.assertEquals(name+" [type parameter count]", validTypeParameters.size(), modelTypeParameters.size());
            for(int i=0;i<validTypeParameters.size();i++){
                TypeParameter validTypeParameter = validTypeParameters.get(i);
                TypeParameter modelTypeParameter = modelTypeParameters.get(i);
                compareDeclarations(name+" [type param "+i+"]", validTypeParameter, modelTypeParameter);
            }
        }
View Full Code Here

        at(expr);
        if(!expr.getWantsDeclaration()){
            return makeTypeLiteralCall(expr.getType().getTypeModel(), true, expr.getTypeModel());
        }else if(expr.getDeclaration() instanceof TypeParameter){
            // we must get it from its container
            TypeParameter declaration = (TypeParameter)expr.getDeclaration();
            Node node = expr;
            return makeTypeParameterDeclaration(node, declaration);
        }else if(expr.getDeclaration() instanceof ClassOrInterface
                 || expr.getDeclaration() instanceof TypeAlias){
            // use the generated class to get to the declaration literal
View Full Code Here

TOP

Related Classes of com.redhat.ceylon.compiler.typechecker.model.TypeParameter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.