Search in sources :

Example 76 with Symbol

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

the class Lower method visitApply.

public void visitApply(JCMethodInvocation tree) {
    Symbol meth = TreeInfo.symbol(tree.meth);
    List<Type> argtypes = meth.type.getParameterTypes();
    if (allowEnums && meth.name == names.init && meth.owner == syms.enumSym)
        argtypes = argtypes.tail.tail;
    tree.args = boxArgs(argtypes, tree.args, tree.varargsElement);
    tree.varargsElement = null;
    Name methName = TreeInfo.name(tree.meth);
    if (meth.name == names.init) {
        // We are seeing a this(...) or super(...) constructor call.
        // If an access constructor is used, append null as a last argument.
        Symbol constructor = accessConstructor(tree.pos(), meth);
        if (constructor != meth) {
            tree.args = tree.args.append(makeNull());
            TreeInfo.setSymbol(tree.meth, constructor);
        }
        // If we are calling a constructor of a local class, add
        // free variables after explicit constructor arguments.
        ClassSymbol c = (ClassSymbol) constructor.owner;
        if (c.isLocal()) {
            tree.args = tree.args.appendList(loadFreevars(tree.pos(), freevars(c)));
        }
        // along the name and ordinal arguments
        if ((c.flags_field & ENUM) != 0 || c.getQualifiedName() == names.java_lang_Enum) {
            List<JCVariableDecl> params = currentMethodDef.params;
            if (currentMethodSym.owner.hasOuterInstance())
                // drop this$n
                params = params.tail;
            tree.args = tree.args.prepend(// ordinal
            make_at(tree.pos()).Ident(params.tail.head.sym)).prepend(// name
            make.Ident(params.head.sym));
        }
        // first argument.
        if (c.hasOuterInstance()) {
            JCExpression thisArg;
            if (tree.meth.hasTag(SELECT)) {
                thisArg = attr.makeNullCheck(translate(((JCFieldAccess) tree.meth).selected));
                tree.meth = make.Ident(constructor);
                ((JCIdent) tree.meth).name = methName;
            } else if (c.isLocal() || methName == names._this) {
                // local class or this() call
                thisArg = makeThis(tree.meth.pos(), c.type.getEnclosingType().tsym);
            } else {
                // super() call of nested class - never pick 'this'
                thisArg = makeOwnerThisN(tree.meth.pos(), c, false);
            }
            tree.args = tree.args.prepend(thisArg);
        }
    } else {
        // We are seeing a normal method invocation; translate this as usual.
        tree.meth = translate(tree.meth);
        // the method arguments to the arguments of the access method.
        if (tree.meth.hasTag(APPLY)) {
            JCMethodInvocation app = (JCMethodInvocation) tree.meth;
            app.args = tree.args.prependList(app.args);
            result = app;
            return;
        }
    }
    result = tree;
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 77 with Symbol

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

the class Lower method accessSymbol.

/**
 * Return access symbol for a private or protected symbol from an inner class.
 *  @param sym        The accessed private symbol.
 *  @param tree       The accessing tree.
 *  @param enclOp     The closest enclosing operation node of tree,
 *                    null if tree is not a subtree of an operation.
 *  @param protAccess Is access to a protected symbol in another
 *                    package?
 *  @param refSuper   Is access via a (qualified) C.super?
 */
MethodSymbol accessSymbol(Symbol sym, JCTree tree, JCTree enclOp, boolean protAccess, boolean refSuper) {
    ClassSymbol accOwner = refSuper && protAccess ? // access symbol on T.
    (ClassSymbol) ((JCFieldAccess) tree).selected.type.tsym : // class which is a subclass of the symbol's owner.
    accessClass(sym, protAccess, tree);
    Symbol vsym = sym;
    if (sym.owner != accOwner) {
        vsym = sym.clone(accOwner);
        actualSymbols.put(vsym, sym);
    }
    // The access number of the access method.
    Integer anum = accessNums.get(vsym);
    if (anum == null) {
        anum = accessed.length();
        accessNums.put(vsym, anum);
        accessSyms.put(vsym, new MethodSymbol[NCODES]);
        accessed.append(vsym);
    // System.out.println("accessing " + vsym + " in " + vsym.location());
    }
    // The access code of the access method.
    int acode;
    // The argument types of the access method.
    List<Type> argtypes;
    // The result type of the access method.
    Type restype;
    // The thrown exceptions of the access method.
    List<Type> thrown;
    switch(vsym.kind) {
        case VAR:
            acode = accessCode(tree, enclOp);
            if (acode >= FIRSTASGOPcode) {
                OperatorSymbol operator = binaryAccessOperator(acode);
                if (operator.opcode == string_add)
                    argtypes = List.of(syms.objectType);
                else
                    argtypes = operator.type.getParameterTypes().tail;
            } else if (acode == ASSIGNcode)
                argtypes = List.of(vsym.erasure(types));
            else
                argtypes = List.nil();
            restype = vsym.erasure(types);
            thrown = List.nil();
            break;
        case MTH:
            acode = DEREFcode;
            argtypes = vsym.erasure(types).getParameterTypes();
            restype = vsym.erasure(types).getReturnType();
            thrown = vsym.type.getThrownTypes();
            break;
        default:
            throw new AssertionError();
    }
    // making it odd.
    if (protAccess && refSuper)
        acode++;
    // containing the access method.
    if ((vsym.flags() & STATIC) == 0) {
        argtypes = argtypes.prepend(vsym.owner.erasure(types));
    }
    MethodSymbol[] accessors = accessSyms.get(vsym);
    MethodSymbol accessor = accessors[acode];
    if (accessor == null) {
        accessor = new MethodSymbol(STATIC | SYNTHETIC, accessName(anum.intValue(), acode), new MethodType(argtypes, restype, thrown, syms.methodClass), accOwner);
        enterSynthetic(tree.pos(), accessor, accOwner.members());
        accessors[acode] = accessor;
    }
    return accessor;
}
Also used : Type(org.eclipse.ceylon.langtools.tools.javac.code.Type) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 78 with Symbol

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

the class Lower method initField.

/**
 * Return tree simulating the assignment {@code this.name = name}, where
 *  name is the name of a free variable.
 */
JCStatement initField(int pos, Name name) {
    Scope.Entry e = proxies.lookup(name);
    Symbol rhs = e.sym;
    Assert.check(rhs.owner.kind == MTH);
    Symbol lhs = e.next().sym;
    Assert.check(rhs.owner.owner == lhs.owner);
    make.at(pos);
    return make.Exec(make.Assign(make.Select(make.This(lhs.owner.erasure(types)), lhs), make.Ident(rhs)).setType(lhs.erasure(types)));
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 79 with Symbol

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

the class Lower method accessDef.

/**
 * Construct definition of an access method.
 *  @param pos        The source code position of the definition.
 *  @param vsym       The private or protected symbol.
 *  @param accessor   The access method for the symbol.
 *  @param acode      The access code.
 */
JCTree accessDef(int pos, Symbol vsym, MethodSymbol accessor, int acode) {
    // System.err.println("access " + vsym + " with " + accessor);//DEBUG
    currentClass = vsym.owner.enclClass();
    make.at(pos);
    JCMethodDecl md = make.MethodDef(accessor, null);
    // Find actual symbol
    Symbol sym = actualSymbols.get(vsym);
    if (sym == null)
        sym = vsym;
    // The tree referencing the private symbol.
    JCExpression ref;
    // Any additional arguments to be passed along.
    List<JCExpression> args;
    if ((sym.flags() & STATIC) != 0) {
        ref = make.Ident(sym);
        args = make.Idents(md.params);
    } else {
        JCExpression site = make.Ident(md.params.head);
        if (acode % 2 != 0) {
            // odd access codes represent qualified super accesses - need to
            // emit reference to the direct superclass, even if the refered
            // member is from an indirect superclass (JLS 13.1)
            site.setType(types.erasure(types.supertype(vsym.owner.enclClass().type)));
        }
        ref = make.Select(site, sym);
        args = make.Idents(md.params.tail);
    }
    // The statement accessing the private symbol.
    JCStatement stat;
    if (sym.kind == VAR) {
        // Normalize out all odd access codes by taking floor modulo 2:
        int acode1 = acode - (acode & 1);
        // The access method's return value.
        JCExpression expr;
        switch(acode1) {
            case DEREFcode:
                expr = ref;
                break;
            case ASSIGNcode:
                expr = make.Assign(ref, args.head);
                break;
            case PREINCcode:
            case POSTINCcode:
            case PREDECcode:
            case POSTDECcode:
                expr = makeUnary(mapUnaryOpCodeToTag(acode1), ref);
                break;
            default:
                expr = make.Assignop(treeTag(binaryAccessOperator(acode1)), ref, args.head);
                ((JCAssignOp) expr).operator = binaryAccessOperator(acode1);
        }
        stat = make.Return(expr.setType(sym.type));
    } else {
        stat = make.Call(make.App(ref, args));
    }
    md.body = make.Block(0, List.of(stat));
    // are accessible.
    for (List<JCVariableDecl> l = md.params; l.nonEmpty(); l = l.tail) l.head.vartype = access(l.head.vartype);
    md.restype = access(md.restype);
    for (List<JCExpression> l = md.thrown; l.nonEmpty(); l = l.tail) l.head = access(l.head);
    return md;
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol)

Example 80 with Symbol

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

the class MemberEnter method visitTopLevel.

public void visitTopLevel(JCCompilationUnit tree) {
    if (tree.starImportScope.elems != null) {
        // we must have already processed this toplevel
        return;
    }
    // toplevel package
    if (checkClash && tree.pid != null) {
        Symbol p = tree.packge;
        while (p.owner != syms.rootPackage) {
            // enter all class members of p
            p.owner.complete();
            if (syms.classes.get(p.getQualifiedName()) != null) {
                log.error(tree.pos, "pkg.clashes.with.class.of.same.name", p);
            }
            p = p.owner;
        }
    }
    // process package annotations
    annotateLater(tree.packageAnnotations, env, tree.packge, null);
    DiagnosticPosition prevLintPos = deferredLintHandler.immediate();
    Lint prevLint = chk.setLint(lint);
    try {
        // Import-on-demand java.lang.
        importAll(tree.pos, reader.enterPackage(names.java_lang), env);
        // Process all import clauses.
        memberEnter(tree.defs, env);
    } finally {
        chk.setLint(prevLint);
        deferredLintHandler.setPos(prevLintPos);
    }
}
Also used : Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) DiagnosticPosition(org.eclipse.ceylon.langtools.tools.javac.util.JCDiagnostic.DiagnosticPosition)

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