Search in sources :

Example 36 with Symbol

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

the class Attr method visitSwitch.

public void visitSwitch(JCSwitch tree) {
    Type seltype = attribExpr(tree.selector, env);
    Env<AttrContext> switchEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
    try {
        boolean enumSwitch = allowEnums && (seltype.tsym.flags() & Flags.ENUM) != 0;
        boolean stringSwitch = false;
        if (types.isSameType(seltype, syms.stringType)) {
            if (allowStringsInSwitch) {
                stringSwitch = true;
            } else {
                log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName);
            }
        }
        if (!enumSwitch && !stringSwitch)
            seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
        // Attribute all cases and
        // check that there are no duplicate case labels or default clauses.
        // The set of case labels.
        Set<Object> labels = new HashSet<Object>();
        // Is there a default label?
        boolean hasDefault = false;
        for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
            JCCase c = l.head;
            Env<AttrContext> caseEnv = switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
            try {
                if (c.pat != null) {
                    if (enumSwitch) {
                        Symbol sym = enumConstant(c.pat, seltype);
                        if (sym == null) {
                            log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum");
                        } else if (!labels.add(sym)) {
                            log.error(c.pos(), "duplicate.case.label");
                        }
                    } else {
                        Type pattype = attribExpr(c.pat, switchEnv, seltype);
                        if (!pattype.hasTag(ERROR)) {
                            if (pattype.constValue() == null) {
                                log.error(c.pat.pos(), (stringSwitch ? "string.const.req" : "const.expr.req"));
                            } else if (labels.contains(pattype.constValue())) {
                                log.error(c.pos(), "duplicate.case.label");
                            } else {
                                labels.add(pattype.constValue());
                            }
                        }
                    }
                } else if (hasDefault) {
                    log.error(c.pos(), "duplicate.default.label");
                } else {
                    hasDefault = true;
                }
                attribStats(c.stats, caseEnv);
            } finally {
                caseEnv.info.scope.leave();
                addVars(c.stats, switchEnv.info.scope);
            }
        }
        result = null;
    } finally {
        switchEnv.info.scope.leave();
    }
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject)

Example 37 with Symbol

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

the class Attr method enumConstant.

// where
/**
 * Return the selected enumeration constant symbol, or null.
 */
private Symbol enumConstant(JCTree tree, Type enumType) {
    if (!tree.hasTag(IDENT)) {
        log.error(tree.pos(), "enum.label.must.be.unqualified.enum");
        return syms.errSymbol;
    }
    JCIdent ident = (JCIdent) tree;
    Name name = ident.name;
    for (Scope.Entry e = enumType.tsym.members().lookup(name); e.scope != null; e = e.next()) {
        if (e.sym.kind == VAR) {
            Symbol s = ident.sym = e.sym;
            // ensure initializer is evaluated
            ((VarSymbol) s).getConstValue();
            ident.type = s.type;
            return ((s.flags_field & Flags.ENUM) == 0) ? null : s;
        }
    }
    return null;
}
Also used : DelegatedScope(org.eclipse.ceylon.langtools.tools.javac.code.Scope.DelegatedScope) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 38 with Symbol

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

the class Attr method checkAutoCloseable.

void checkAutoCloseable(DiagnosticPosition pos, Env<AttrContext> env, Type resource) {
    if (!resource.isErroneous() && types.asSuper(resource, syms.autoCloseableType.tsym) != null && !types.isSameType(resource, syms.autoCloseableType)) {
        // Don't emit warning for AutoCloseable itself
        Symbol close = syms.noSymbol;
        Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log);
        try {
            close = rs.resolveQualifiedMethod(pos, env, resource, names.close, List.<Type>nil(), List.<Type>nil());
        } finally {
            log.popDiagnosticHandler(discardHandler);
        }
        if (close.kind == MTH && close.overrides(syms.autoCloseableClose, resource.tsym, types, true) && chk.isHandled(syms.interruptedExceptionType, types.memberType(resource, close).getThrownTypes()) && env.info.lint.isEnabled(LintCategory.TRY)) {
            log.warning(LintCategory.TRY, pos, "try.resource.throws.interrupted.exc", resource);
        }
    }
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 39 with Symbol

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

the class Attr method checkLambdaCandidate.

private void checkLambdaCandidate(JCNewClass tree, ClassSymbol csym, Type clazztype) {
    if (allowLambda && identifyLambdaCandidate && clazztype.hasTag(CLASS) && !pt().hasTag(NONE) && types.isFunctionalInterface(clazztype.tsym)) {
        Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym);
        int count = 0;
        boolean found = false;
        for (Symbol sym : csym.members().getElements()) {
            if ((sym.flags() & SYNTHETIC) != 0 || sym.isConstructor())
                continue;
            count++;
            if (sym.kind != MTH || !sym.name.equals(descriptor.name))
                continue;
            Type mtype = types.memberType(clazztype, sym);
            if (types.overrideEquivalent(mtype, types.memberType(clazztype, descriptor))) {
                found = true;
            }
        }
        if (found && count == 1) {
            log.note(tree.def, "potential.lambda.found");
        }
    }
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 40 with Symbol

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

the class Attr method visitParens.

public void visitParens(JCParens tree) {
    Type owntype = attribTree(tree.expr, env, resultInfo);
    result = check(tree, owntype, pkind(), resultInfo);
    Symbol sym = TreeInfo.symbol(tree);
    if (sym != null && (sym.kind & (TYP | PCK)) != 0)
        log.error(tree.pos(), "illegal.start.of.type");
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Aggregations

Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)115 Type (org.eclipse.ceylon.langtools.tools.javac.code.Type)42 ClassSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol)16 MethodSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol)15 VarSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.VarSymbol)12 TypeSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol)11 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)11 DeferredAttrContext (org.eclipse.ceylon.langtools.tools.javac.comp.DeferredAttr.DeferredAttrContext)8 DeferredType (org.eclipse.ceylon.langtools.tools.javac.comp.DeferredAttr.DeferredType)8 DiagnosticType (org.eclipse.ceylon.langtools.tools.javac.util.JCDiagnostic.DiagnosticType)8 DynamicMethodSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.DynamicMethodSymbol)6 MethodType (org.eclipse.ceylon.langtools.tools.javac.code.Type.MethodType)6 JavaFileObject (org.eclipse.ceylon.javax.tools.JavaFileObject)5 DiagnosticPosition (org.eclipse.ceylon.langtools.tools.javac.util.JCDiagnostic.DiagnosticPosition)4 ErrorType (org.eclipse.ceylon.javax.lang.model.type.ErrorType)3 Scope (org.eclipse.ceylon.langtools.tools.javac.code.Scope)3 PackageSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.PackageSymbol)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 DeclaredType (org.eclipse.ceylon.javax.lang.model.type.DeclaredType)2