Search in sources :

Example 11 with Symbol

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

the class Lower method visitAssignop.

public void visitAssignop(final JCAssignOp tree) {
    JCTree lhsAccess = access(TreeInfo.skipParens(tree.lhs));
    final boolean boxingReq = !tree.lhs.type.isPrimitive() && tree.operator.type.getReturnType().isPrimitive();
    if (boxingReq || lhsAccess.hasTag(APPLY)) {
        // boxing required; need to rewrite as x = (unbox typeof x)(x op y);
        // or if x == (typeof x)z then z = (unbox typeof x)((typeof x)z op y)
        // (but without recomputing x)
        JCTree newTree = abstractLval(tree.lhs, new TreeBuilder() {

            public JCTree build(final JCTree lhs) {
                JCTree.Tag newTag = tree.getTag().noAssignOp();
                // Erasure (TransTypes) can change the type of
                // tree.lhs.  However, we can still get the
                // unerased type of tree.lhs as it is stored
                // in tree.type in Attr.
                Symbol newOperator = rs.resolveBinaryOperator(tree.pos(), newTag, attrEnv, tree.type, tree.rhs.type);
                JCExpression expr = (JCExpression) lhs;
                if (expr.type != tree.type)
                    expr = make.TypeCast(tree.type, expr);
                JCBinary opResult = make.Binary(newTag, expr, tree.rhs);
                opResult.operator = newOperator;
                opResult.type = newOperator.type.getReturnType();
                JCExpression newRhs = boxingReq ? make.TypeCast(types.unboxedType(tree.type), opResult) : opResult;
                return make.Assign((JCExpression) lhs, newRhs).setType(tree.type);
            }
        });
        result = translate(newTree);
        return;
    }
    tree.lhs = translate(tree.lhs, tree);
    tree.rhs = translate(tree.rhs, tree.operator.type.getParameterTypes().tail.head);
    // right hand side as last argument of the access method.
    if (tree.lhs.hasTag(APPLY)) {
        JCMethodInvocation app = (JCMethodInvocation) tree.lhs;
        // if operation is a += on strings,
        // make sure to convert argument to string
        JCExpression rhs = (((OperatorSymbol) tree.operator).opcode == string_add) ? makeString(tree.rhs) : tree.rhs;
        app.args = List.of(rhs).prependList(app.args);
        result = app;
    } else {
        result = tree;
    }
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) TypeTag(org.eclipse.ceylon.langtools.tools.javac.code.TypeTag) Tag(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.Tag)

Example 12 with Symbol

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

the class MemberEnter method importNamedStatic.

/**
 * Import statics types of a given name.  Non-types are handled in Attr.
 *  @param pos           Position to be used for error reporting.
 *  @param tsym          The class from which the name is imported.
 *  @param name          The (simple) name being imported.
 *  @param env           The environment containing the named import
 *                  scope to add to.
 */
private void importNamedStatic(final DiagnosticPosition pos, final TypeSymbol tsym, final Name name, final Env<AttrContext> env) {
    if (tsym.kind != TYP) {
        log.error(DiagnosticFlag.RECOVERABLE, pos, "static.imp.only.classes.and.interfaces");
        return;
    }
    final Scope toScope = env.toplevel.namedImportScope;
    final PackageSymbol packge = env.toplevel.packge;
    final TypeSymbol origin = tsym;
    // enter imported types immediately
    new Object() {

        Set<Symbol> processed = new HashSet<Symbol>();

        void importFrom(TypeSymbol tsym) {
            if (tsym == null || !processed.add(tsym))
                return;
            // also import inherited names
            importFrom(types.supertype(tsym.type).tsym);
            for (Type t : types.interfaces(tsym.type)) importFrom(t.tsym);
            for (Scope.Entry e = tsym.members().lookup(name); e.scope != null; e = e.next()) {
                Symbol sym = e.sym;
                if (sym.isStatic() && sym.kind == TYP && staticImportAccessible(sym, packge) && sym.isMemberOf(origin, types) && chk.checkUniqueStaticImport(pos, sym, toScope))
                    toScope.enter(sym, sym.owner.members(), origin.members(), true);
            }
        }
    }.importFrom(tsym);
    // enter non-types before annotations that might use them
    annotate.earlier(new Annotate.Worker() {

        Set<Symbol> processed = new HashSet<Symbol>();

        boolean found = false;

        public String toString() {
            return "import static " + tsym + "." + name;
        }

        void importFrom(TypeSymbol tsym) {
            if (tsym == null || !processed.add(tsym))
                return;
            // also import inherited names
            importFrom(types.supertype(tsym.type).tsym);
            for (Type t : types.interfaces(tsym.type)) importFrom(t.tsym);
            for (Scope.Entry e = tsym.members().lookup(name); e.scope != null; e = e.next()) {
                Symbol sym = e.sym;
                if (sym.isStatic() && staticImportAccessible(sym, packge) && sym.isMemberOf(origin, types)) {
                    found = true;
                    if (sym.kind != TYP) {
                        toScope.enter(sym, sym.owner.members(), origin.members(), true);
                    }
                }
            }
        }

        public void run() {
            JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
            try {
                importFrom(tsym);
                if (!found) {
                    log.error(pos, "cant.resolve.location", KindName.STATIC, name, List.<Type>nil(), List.<Type>nil(), Kinds.typeKindName(tsym.type), tsym.type);
                }
            } finally {
                log.useSource(prev);
            }
        }
    });
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) HashSet(java.util.HashSet)

Example 13 with Symbol

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

the class MemberEnter method importStaticAll.

/**
 * Import all static members of a class or package on demand.
 *  @param pos           Position to be used for error reporting.
 *  @param tsym          The class or package the members of which are imported.
 *  @param env           The env in which the imported classes will be entered.
 */
private void importStaticAll(int pos, final TypeSymbol tsym, Env<AttrContext> env) {
    final JavaFileObject sourcefile = env.toplevel.sourcefile;
    final Scope toScope = env.toplevel.starImportScope;
    final PackageSymbol packge = env.toplevel.packge;
    final TypeSymbol origin = tsym;
    // enter imported types immediately
    new Object() {

        Set<Symbol> processed = new HashSet<Symbol>();

        void importFrom(TypeSymbol tsym) {
            if (tsym == null || !processed.add(tsym))
                return;
            // also import inherited names
            importFrom(types.supertype(tsym.type).tsym);
            for (Type t : types.interfaces(tsym.type)) importFrom(t.tsym);
            final Scope fromScope = tsym.members();
            for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
                Symbol sym = e.sym;
                if (sym.kind == TYP && (sym.flags() & STATIC) != 0 && staticImportAccessible(sym, packge) && sym.isMemberOf(origin, types) && !toScope.includes(sym))
                    toScope.enter(sym, fromScope, origin.members(), true);
            }
        }
    }.importFrom(tsym);
    // enter non-types before annotations that might use them
    annotate.earlier(new Annotate.Worker() {

        Set<Symbol> processed = new HashSet<Symbol>();

        public String toString() {
            return "import static " + tsym + ".*" + " in " + sourcefile;
        }

        void importFrom(TypeSymbol tsym) {
            if (tsym == null || !processed.add(tsym))
                return;
            // also import inherited names
            importFrom(types.supertype(tsym.type).tsym);
            for (Type t : types.interfaces(tsym.type)) importFrom(t.tsym);
            final Scope fromScope = tsym.members();
            for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
                Symbol sym = e.sym;
                if (sym.isStatic() && sym.kind != TYP && staticImportAccessible(sym, packge) && !toScope.includes(sym) && sym.isMemberOf(origin, types)) {
                    toScope.enter(sym, fromScope, origin.members(), true);
                }
            }
        }

        public void run() {
            importFrom(tsym);
        }
    });
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) JavaFileObject(org.eclipse.ceylon.javax.tools.JavaFileObject) HashSet(java.util.HashSet)

Example 14 with Symbol

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

the class Resolve method lookupMethod.

Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol location, MethodResolutionContext resolveContext, LookupHelper lookupHelper) {
    MethodResolutionContext prevResolutionContext = currentResolutionContext;
    try {
        Symbol bestSoFar = methodNotFound;
        currentResolutionContext = resolveContext;
        for (MethodResolutionPhase phase : methodResolutionSteps) {
            if (!phase.isApplicable(boxingEnabled, varargsEnabled) || lookupHelper.shouldStop(bestSoFar, phase))
                break;
            MethodResolutionPhase prevPhase = currentResolutionContext.step;
            Symbol prevBest = bestSoFar;
            currentResolutionContext.step = phase;
            Symbol sym = lookupHelper.lookup(env, phase);
            lookupHelper.debug(pos, sym);
            bestSoFar = phase.mergeResults(bestSoFar, sym);
            env.info.pendingResolutionPhase = (prevBest == bestSoFar) ? prevPhase : phase;
        }
        return lookupHelper.access(env, pos, location, bestSoFar);
    } finally {
        currentResolutionContext = prevResolutionContext;
    }
}
Also used : MethodResolutionPhase(org.eclipse.ceylon.langtools.tools.javac.comp.Resolve.MethodResolutionPhase) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 15 with Symbol

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

the class Resolve method getMemberReference.

Symbol getMemberReference(DiagnosticPosition pos, Env<AttrContext> env, JCMemberReference referenceTree, Type site, Name name) {
    site = types.capture(site);
    ReferenceLookupHelper lookupHelper = makeReferenceLookupHelper(referenceTree, site, name, List.<Type>nil(), null, VARARITY);
    Env<AttrContext> newEnv = env.dup(env.tree, env.info.dup());
    Symbol sym = lookupMethod(newEnv, env.tree.pos(), site.tsym, nilMethodCheck, lookupHelper);
    env.info.pendingResolutionPhase = newEnv.info.pendingResolutionPhase;
    return sym;
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) DeferredAttrContext(org.eclipse.ceylon.langtools.tools.javac.comp.DeferredAttr.DeferredAttrContext)

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