Search in sources :

Example 61 with Type

use of org.eclipse.ceylon.langtools.tools.javac.code.Type in project ceylon by eclipse.

the class Check method firstIncompatibility.

/**
 * Return the first method which is defined with same args
 *  but different return types in two given interfaces, or null if none
 *  exists.
 *  @param t1     The first type.
 *  @param t2     The second type.
 *  @param site   The most derived type.
 *  @returns symbol from t2 that conflicts with one in t1.
 */
private Symbol firstIncompatibility(DiagnosticPosition pos, Type t1, Type t2, Type site) {
    Map<TypeSymbol, Type> interfaces1 = new HashMap<TypeSymbol, Type>();
    closure(t1, interfaces1);
    Map<TypeSymbol, Type> interfaces2;
    if (t1 == t2)
        interfaces2 = interfaces1;
    else
        closure(t2, interfaces1, interfaces2 = new HashMap<TypeSymbol, Type>());
    for (Type t3 : interfaces1.values()) {
        for (Type t4 : interfaces2.values()) {
            Symbol s = firstDirectIncompatibility(pos, t3, t4, site);
            if (s != null)
                return s;
        }
    }
    return null;
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 62 with Type

use of org.eclipse.ceylon.langtools.tools.javac.code.Type in project ceylon by eclipse.

the class Check method validateValue.

private void validateValue(TypeSymbol container, TypeSymbol contained, DiagnosticPosition pos) {
    Scope.Entry e = container.members().lookup(names.value);
    if (e.scope != null && e.sym.kind == MTH) {
        MethodSymbol m = (MethodSymbol) e.sym;
        Type ret = m.getReturnType();
        if (!(ret.hasTag(ARRAY) && types.isSameType(((ArrayType) ret).elemtype, contained.type))) {
            log.error(pos, "invalid.repeatable.annotation.value.return", container, ret, types.makeArrayType(contained.type));
        }
    } else {
        log.error(pos, "invalid.repeatable.annotation.no.value", container);
    }
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type)

Example 63 with Type

use of org.eclipse.ceylon.langtools.tools.javac.code.Type in project ceylon by eclipse.

the class Check method checkCommonOverriderIn.

// WHERE
boolean checkCommonOverriderIn(Symbol s1, Symbol s2, Type site) {
    Map<TypeSymbol, Type> supertypes = new HashMap<TypeSymbol, Type>();
    Type st1 = types.memberType(site, s1);
    Type st2 = types.memberType(site, s2);
    closure(site, supertypes);
    for (Type t : supertypes.values()) {
        for (Scope.Entry e = t.tsym.members().lookup(s1.name); e.scope != null; e = e.next()) {
            Symbol s3 = e.sym;
            if (s3 == s1 || s3 == s2 || s3.kind != MTH || (s3.flags() & (BRIDGE | SYNTHETIC)) != 0)
                continue;
            Type st3 = types.memberType(site, s3);
            if (types.overrideEquivalent(st3, st1) && types.overrideEquivalent(st3, st2) && types.returnTypeSubstitutable(st3, st1) && types.returnTypeSubstitutable(st3, st2)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 64 with Type

use of org.eclipse.ceylon.langtools.tools.javac.code.Type in project ceylon by eclipse.

the class Check method checkPotentiallyAmbiguousOverloads.

/**
 * Report warnings for potentially ambiguous method declarations. Two declarations
 * are potentially ambiguous if they feature two unrelated functional interface
 * in same argument position (in which case, a call site passing an implicit
 * lambda would be ambiguous).
 */
void checkPotentiallyAmbiguousOverloads(DiagnosticPosition pos, Type site, MethodSymbol msym1, MethodSymbol msym2) {
    if (msym1 != msym2 && allowDefaultMethods && lint.isEnabled(LintCategory.OVERLOADS) && (msym1.flags() & POTENTIALLY_AMBIGUOUS) == 0 && (msym2.flags() & POTENTIALLY_AMBIGUOUS) == 0) {
        Type mt1 = types.memberType(site, msym1);
        Type mt2 = types.memberType(site, msym2);
        // if both generic methods, adjust type variables
        if (mt1.hasTag(FORALL) && mt2.hasTag(FORALL) && types.hasSameBounds((ForAll) mt1, (ForAll) mt2)) {
            mt2 = types.subst(mt2, ((ForAll) mt2).tvars, ((ForAll) mt1).tvars);
        }
        // expand varargs methods if needed
        int maxLength = Math.max(mt1.getParameterTypes().length(), mt2.getParameterTypes().length());
        List<Type> args1 = rs.adjustArgs(mt1.getParameterTypes(), msym1, maxLength, true);
        List<Type> args2 = rs.adjustArgs(mt2.getParameterTypes(), msym2, maxLength, true);
        // if arities don't match, exit
        if (args1.length() != args2.length())
            return;
        boolean potentiallyAmbiguous = false;
        while (args1.nonEmpty() && args2.nonEmpty()) {
            Type s = args1.head;
            Type t = args2.head;
            if (!types.isSubtype(t, s) && !types.isSubtype(s, t)) {
                if (types.isFunctionalInterface(s) && types.isFunctionalInterface(t) && types.findDescriptorType(s).getParameterTypes().length() > 0 && types.findDescriptorType(s).getParameterTypes().length() == types.findDescriptorType(t).getParameterTypes().length()) {
                    potentiallyAmbiguous = true;
                } else {
                    break;
                }
            }
            args1 = args1.tail;
            args2 = args2.tail;
        }
        if (potentiallyAmbiguous) {
            // we found two incompatible functional interfaces with same arity
            // this means a call site passing an implicit lambda would be ambigiuous
            msym1.flags_field |= POTENTIALLY_AMBIGUOUS;
            msym2.flags_field |= POTENTIALLY_AMBIGUOUS;
            log.warning(LintCategory.OVERLOADS, pos, "potentially.ambiguous.overload", msym1, msym1.location(), msym2, msym2.location());
            return;
        }
    }
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type)

Example 65 with Type

use of org.eclipse.ceylon.langtools.tools.javac.code.Type in project ceylon by eclipse.

the class Types method appendTyparamString.

private void appendTyparamString(TypeVar t, StringBuilder buf) {
    buf.append(t);
    if (t.bound == null || t.bound.tsym.getQualifiedName() == names.java_lang_Object)
        return;
    // Java syntax; no need for i18n
    buf.append(" extends ");
    Type bound = t.bound;
    if (!bound.isCompound()) {
        buf.append(bound);
    } else if ((erasure(t).tsym.flags() & INTERFACE) == 0) {
        buf.append(supertype(t));
        for (Type intf : interfaces(t)) {
            buf.append('&');
            buf.append(intf);
        }
    } else {
        // No superclass was given in bounds.
        // In this case, supertype is Object, erasure is first interface.
        boolean first = true;
        for (Type intf : interfaces(t)) {
            if (!first)
                buf.append('&');
            first = false;
            buf.append(intf);
        }
    }
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type)

Aggregations

Type (org.eclipse.ceylon.langtools.tools.javac.code.Type)164 Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)46 DeferredType (org.eclipse.ceylon.langtools.tools.javac.comp.DeferredAttr.DeferredType)13 DiagnosticType (org.eclipse.ceylon.langtools.tools.javac.util.JCDiagnostic.DiagnosticType)13 ArrayType (org.eclipse.ceylon.langtools.tools.javac.code.Type.ArrayType)10 MethodType (org.eclipse.ceylon.langtools.tools.javac.code.Type.MethodType)10 ClassSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol)9 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)9 MethodSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol)8 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)7 TypeSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol)7 DiagnosticPosition (org.eclipse.ceylon.langtools.tools.javac.util.JCDiagnostic.DiagnosticPosition)7 JavacType (org.eclipse.ceylon.compiler.java.loader.mirror.JavacType)5 PackageSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol)5 FunctionalInterfaceType (org.eclipse.ceylon.model.loader.mirror.FunctionalInterfaceType)5 UnknownType (org.eclipse.ceylon.model.typechecker.model.UnknownType)5 HashSet (java.util.HashSet)4 CompletionFailure (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.CompletionFailure)4 VarSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.VarSymbol)4 FreeTypeListener (org.eclipse.ceylon.langtools.tools.javac.comp.Infer.FreeTypeListener)4