Search in sources :

Example 81 with Symbol

use of com.sun.tools.javac.code.Symbol in project ceylon-compiler by ceylon.

the class Resolve method findField.

/* ***************************************************************************
 *  Symbol lookup
 *  the following naming conventions for arguments are used
 *
 *       env      is the environment where the symbol was mentioned
 *       site     is the type of which the symbol is a member
 *       name     is the symbol's name
 *                if no arguments are given
 *       argtypes are the value arguments, if we search for a method
 *
 *  If no symbol was found, a ResolveError detailing the problem is returned.
 ****************************************************************************/
/** Find field. Synthetic fields are always skipped.
     *  @param env     The current environment.
     *  @param site    The original type from where the selection takes place.
     *  @param name    The name of the field.
     *  @param c       The class to search for the field. This is always
     *                 a superclass or implemented interface of site's class.
     */
Symbol findField(Env<AttrContext> env, Type site, Name name, TypeSymbol c) {
    while (c.type.tag == TYPEVAR) c = c.type.getUpperBound().tsym;
    Symbol bestSoFar = varNotFound;
    Symbol sym;
    Scope.Entry e = c.members().lookup(name);
    while (e.scope != null) {
        if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
            return isAccessible(env, site, e.sym) ? e.sym : new AccessError(env, site, e.sym);
        }
        e = e.next();
    }
    Type st = types.supertype(c.type);
    if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
        sym = findField(env, site, name, st.tsym);
        if (sym.kind < bestSoFar.kind)
            bestSoFar = sym;
    }
    for (List<Type> l = types.interfaces(c.type); bestSoFar.kind != AMBIGUOUS && l.nonEmpty(); l = l.tail) {
        sym = findField(env, site, name, l.head.tsym);
        if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
            bestSoFar = new AmbiguityError(bestSoFar, sym);
        else if (sym.kind < bestSoFar.kind)
            bestSoFar = sym;
    }
    return bestSoFar;
}
Also used : DiagnosticType(com.sun.tools.javac.util.JCDiagnostic.DiagnosticType) Type(com.sun.tools.javac.code.Type) Symbol(com.sun.tools.javac.code.Symbol)

Example 82 with Symbol

use of com.sun.tools.javac.code.Symbol in project ceylon-compiler by ceylon.

the class Resolve method findVar.

/** Find unqualified variable or field with given name.
     *  Synthetic fields always skipped.
     *  @param env     The current environment.
     *  @param name    The name of the variable or field.
     */
Symbol findVar(Env<AttrContext> env, Name name) {
    Symbol bestSoFar = varNotFound;
    Symbol sym;
    Env<AttrContext> env1 = env;
    boolean staticOnly = false;
    while (env1.outer != null) {
        if (isStatic(env1))
            staticOnly = true;
        Scope.Entry e = env1.info.scope.lookup(name);
        while (e.scope != null && (e.sym.kind != VAR || (e.sym.flags_field & SYNTHETIC) != 0)) e = e.next();
        sym = (e.scope != null) ? e.sym : findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
        if (sym.exists()) {
            if (staticOnly && sym.kind == VAR && sym.owner.kind == TYP && (sym.flags() & STATIC) == 0)
                return new StaticError(sym);
            else
                return sym;
        } else if (sym.kind < bestSoFar.kind) {
            bestSoFar = sym;
        }
        if ((env1.enclClass.sym.flags() & STATIC) != 0)
            staticOnly = true;
        env1 = env1.outer;
    }
    sym = findField(env, syms.predefClass.type, name, syms.predefClass);
    if (sym.exists())
        return sym;
    if (bestSoFar.exists())
        return bestSoFar;
    Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
    for (; e.scope != null; e = e.next()) {
        sym = e.sym;
        Type origin = e.getOrigin().owner.type;
        if (sym.kind == VAR) {
            if (e.sym.owner.type != origin)
                sym = sym.clone(e.getOrigin().owner);
            return isAccessible(env, origin, sym) ? sym : new AccessError(env, origin, sym);
        }
    }
    Symbol origin = null;
    e = env.toplevel.starImportScope.lookup(name);
    for (; e.scope != null; e = e.next()) {
        sym = e.sym;
        if (sym.kind != VAR)
            continue;
        // invariant: sym.kind == VAR
        if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
            return new AmbiguityError(bestSoFar, sym);
        else if (bestSoFar.kind >= VAR) {
            origin = e.getOrigin().owner;
            bestSoFar = isAccessible(env, origin.type, sym) ? sym : new AccessError(env, origin.type, sym);
        }
    }
    if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
        return bestSoFar.clone(origin);
    else
        return bestSoFar;
}
Also used : DiagnosticType(com.sun.tools.javac.util.JCDiagnostic.DiagnosticType) Type(com.sun.tools.javac.code.Type) Symbol(com.sun.tools.javac.code.Symbol)

Example 83 with Symbol

use of com.sun.tools.javac.code.Symbol in project ceylon-compiler by ceylon.

the class Lower method addEnumToString.

private MethodSymbol addEnumToString(JCClassDecl cdef, VarSymbol nameSymbol) {
    Symbol toStringSym = lookupMethod(cdef.pos(), names.toString, cdef.type, List.<Type>nil());
    JCTree toStringDecl = null;
    if (toStringSym != null)
        toStringDecl = TreeInfo.declarationFor(toStringSym, cdef);
    if (toStringDecl != null)
        return (MethodSymbol) toStringSym;
    JCStatement ret = make.Return(make.Ident(nameSymbol));
    JCTree resTypeTree = make.Type(syms.stringType);
    MethodType toStringType = new MethodType(List.<Type>nil(), syms.stringType, List.<Type>nil(), cdef.sym);
    toStringSym = new MethodSymbol(PUBLIC, names.toString, toStringType, cdef.type.tsym);
    toStringDecl = make.MethodDef((MethodSymbol) toStringSym, make.Block(0L, List.of(ret)));
    cdef.defs = cdef.defs.prepend(toStringDecl);
    cdef.sym.members().enter(toStringSym);
    return (MethodSymbol) toStringSym;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 84 with Symbol

use of com.sun.tools.javac.code.Symbol in project ceylon-compiler by ceylon.

the class Lower method addEnumCompareTo.

private MethodSymbol addEnumCompareTo(JCClassDecl cdef, VarSymbol ordinalSymbol) {
    Symbol compareToSym = lookupMethod(cdef.pos(), names.compareTo, cdef.type, List.of(cdef.sym.type));
    Assert.check(compareToSym instanceof MethodSymbol);
    JCMethodDecl compareToDecl = (JCMethodDecl) TreeInfo.declarationFor(compareToSym, cdef);
    ListBuffer<JCStatement> blockStatements = new ListBuffer<JCStatement>();
    JCModifiers mod1 = make.Modifiers(0L);
    Name oName = names.fromString("o");
    JCVariableDecl par1 = make.Param(oName, cdef.type, compareToSym);
    JCIdent paramId1 = make.Ident(names.java_lang_Object);
    paramId1.type = cdef.type;
    paramId1.sym = par1.sym;
    ((MethodSymbol) compareToSym).params = List.of(par1.sym);
    JCIdent par1UsageId = make.Ident(par1.sym);
    JCIdent castTargetIdent = make.Ident(cdef.sym);
    JCTypeCast cast = make.TypeCast(castTargetIdent, par1UsageId);
    cast.setType(castTargetIdent.type);
    Name otherName = names.fromString("other");
    VarSymbol otherVarSym = new VarSymbol(mod1.flags, otherName, cdef.type, compareToSym);
    JCVariableDecl otherVar = make.VarDef(otherVarSym, cast);
    blockStatements.append(otherVar);
    JCIdent id1 = make.Ident(ordinalSymbol);
    JCIdent fLocUsageId = make.Ident(otherVarSym);
    JCExpression sel = make.Select(fLocUsageId, ordinalSymbol);
    JCBinary bin = makeBinary(JCTree.MINUS, id1, sel);
    JCReturn ret = make.Return(bin);
    blockStatements.append(ret);
    JCMethodDecl compareToMethod = make.MethodDef((MethodSymbol) compareToSym, make.Block(0L, blockStatements.toList()));
    compareToMethod.params = List.of(par1);
    cdef.defs = cdef.defs.append(compareToMethod);
    return (MethodSymbol) compareToSym;
}
Also used : Symbol(com.sun.tools.javac.code.Symbol)

Example 85 with Symbol

use of com.sun.tools.javac.code.Symbol in project ceylon-compiler by ceylon.

the class Lower method visitNewClass.

public void visitNewClass(JCNewClass tree) {
    ClassSymbol c = (ClassSymbol) tree.constructor.owner;
    // Box arguments, if necessary
    boolean isEnum = (tree.constructor.owner.flags() & ENUM) != 0;
    List<Type> argTypes = tree.constructor.type.getParameterTypes();
    if (isEnum)
        argTypes = argTypes.prepend(syms.intType).prepend(syms.stringType);
    tree.args = boxArgs(argTypes, tree.args, tree.varargsElement);
    tree.varargsElement = null;
    // explicit constructor arguments.
    if ((c.owner.kind & (VAR | MTH)) != 0) {
        tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
    }
    // If an access constructor is used, append null as a last argument.
    Symbol constructor = accessConstructor(tree.pos(), tree.constructor);
    if (constructor != tree.constructor) {
        tree.args = tree.args.append(makeNull());
        tree.constructor = constructor;
    }
    // correct outer instance as first argument.
    if (c.hasOuterInstance()) {
        JCExpression thisArg;
        if (tree.encl != null) {
            thisArg = attr.makeNullCheck(translate(tree.encl));
            thisArg.type = tree.encl.type;
        } else if ((c.owner.kind & (MTH | VAR)) != 0) {
            // local class
            thisArg = makeThis(tree.pos(), c.type.getEnclosingType().tsym);
        } else {
            // nested class
            thisArg = makeOwnerThis(tree.pos(), c, false);
        }
        tree.args = tree.args.prepend(thisArg);
    }
    tree.encl = null;
    // than the class or interface following new.
    if (tree.def != null) {
        translate(tree.def);
        tree.clazz = access(make_at(tree.clazz.pos()).Ident(tree.def.sym));
        tree.def = null;
    } else {
        tree.clazz = access(c, tree.clazz, enclOp, false);
    }
    result = tree;
}
Also used : Type(com.sun.tools.javac.code.Type) Symbol(com.sun.tools.javac.code.Symbol)

Aggregations

Symbol (com.sun.tools.javac.code.Symbol)195 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)56 Type (com.sun.tools.javac.code.Type)54 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)53 VarSymbol (com.sun.tools.javac.code.Symbol.VarSymbol)45 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)36 PackageSymbol (com.sun.tools.javac.code.Symbol.PackageSymbol)29 JCTree (com.sun.tools.javac.tree.JCTree)28 ClassType (com.sun.tools.javac.code.Type.ClassType)18 Tree (com.sun.source.tree.Tree)17 ExpressionTree (com.sun.source.tree.ExpressionTree)15 DynamicMethodSymbol (com.sun.tools.javac.code.Symbol.DynamicMethodSymbol)15 OperatorSymbol (com.sun.tools.javac.code.Symbol.OperatorSymbol)15 ClassTree (com.sun.source.tree.ClassTree)14 MethodTree (com.sun.source.tree.MethodTree)14 Name (com.sun.tools.javac.util.Name)14 IdentifierTree (com.sun.source.tree.IdentifierTree)13 ArrayType (com.sun.tools.javac.code.Type.ArrayType)12 MethodType (com.sun.tools.javac.code.Type.MethodType)12 UnionClassType (com.sun.tools.javac.code.Type.UnionClassType)12