Search in sources :

Example 86 with Symbol

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

the class Lower method classDollarSymBody.

/** Generate code for class$(String name). */
JCBlock classDollarSymBody(DiagnosticPosition pos, JCMethodDecl md) {
    MethodSymbol classDollarSym = md.sym;
    ClassSymbol outerCacheClass = (ClassSymbol) classDollarSym.owner;
    JCBlock returnResult;
    // which requires we cache the current loader in cl$
    if (target.classLiteralsNoInit()) {
        // clsym = "private static ClassLoader cl$"
        VarSymbol clsym = new VarSymbol(STATIC | SYNTHETIC, names.fromString("cl" + target.syntheticNameChar()), syms.classLoaderType, outerCacheClass);
        enterSynthetic(pos, clsym, outerCacheClass.members());
        // emit "private static ClassLoader cl$;"
        JCVariableDecl cldef = make.VarDef(clsym, null);
        JCClassDecl outerCacheClassDef = classDef(outerCacheClass);
        outerCacheClassDef.defs = outerCacheClassDef.defs.prepend(cldef);
        // newcache := "new cache$1[0]"
        JCNewArray newcache = make.NewArray(make.Type(outerCacheClass.type), List.<JCExpression>of(make.Literal(INT, 0).setType(syms.intType)), null);
        newcache.type = new ArrayType(types.erasure(outerCacheClass.type), syms.arrayClass);
        // forNameSym := java.lang.Class.forName(
        //     String s,boolean init,ClassLoader loader)
        Symbol forNameSym = lookupMethod(make_pos, names.forName, types.erasure(syms.classType), List.of(syms.stringType, syms.booleanType, syms.classLoaderType));
        // clvalue := "(cl$ == null) ?
        // $newcache.getClass().getComponentType().getClassLoader() : cl$"
        JCExpression clvalue = make.Conditional(makeBinary(JCTree.EQ, make.Ident(clsym), makeNull()), make.Assign(make.Ident(clsym), makeCall(makeCall(makeCall(newcache, names.getClass, List.<JCExpression>nil()), names.getComponentType, List.<JCExpression>nil()), names.getClassLoader, List.<JCExpression>nil())).setType(syms.classLoaderType), make.Ident(clsym)).setType(syms.classLoaderType);
        // returnResult := "{ return Class.forName(param1, false, cl$); }"
        List<JCExpression> args = List.of(make.Ident(md.params.head.sym), makeLit(syms.booleanType, 0), clvalue);
        returnResult = make.Block(0, List.<JCStatement>of(make.Call(// return
        make.App(make.Ident(forNameSym), args))));
    } else {
        // forNameSym := java.lang.Class.forName(String s)
        Symbol forNameSym = lookupMethod(make_pos, names.forName, types.erasure(syms.classType), List.of(syms.stringType));
        // returnResult := "{ return Class.forName(param1); }"
        returnResult = make.Block(0, List.of(make.Call(// return
        make.App(make.QualIdent(forNameSym), List.<JCExpression>of(make.Ident(md.params.head.sym))))));
    }
    // catchParam := ClassNotFoundException e1
    VarSymbol catchParam = new VarSymbol(0, make.paramName(1), syms.classNotFoundExceptionType, classDollarSym);
    JCStatement rethrow;
    if (target.hasInitCause()) {
        // rethrow = "throw new NoClassDefFoundError().initCause(e);
        JCTree throwExpr = makeCall(makeNewClass(syms.noClassDefFoundErrorType, List.<JCExpression>nil()), names.initCause, List.<JCExpression>of(make.Ident(catchParam)));
        rethrow = make.Throw(throwExpr);
    } else {
        // getMessageSym := ClassNotFoundException.getMessage()
        Symbol getMessageSym = lookupMethod(make_pos, names.getMessage, syms.classNotFoundExceptionType, List.<Type>nil());
        // rethrow = "throw new NoClassDefFoundError(e.getMessage());"
        rethrow = make.Throw(makeNewClass(syms.noClassDefFoundErrorType, List.<JCExpression>of(make.App(make.Select(make.Ident(catchParam), getMessageSym), List.<JCExpression>nil()))));
    }
    // rethrowStmt := "( $rethrow )"
    JCBlock rethrowStmt = make.Block(0, List.of(rethrow));
    // catchBlock := "catch ($catchParam) $rethrowStmt"
    JCCatch catchBlock = make.Catch(make.VarDef(catchParam, null), rethrowStmt);
    // tryCatch := "try $returnResult $catchBlock"
    JCStatement tryCatch = make.Try(returnResult, List.of(catchBlock), null);
    return make.Block(0, List.of(tryCatch));
}
Also used : Symbol(com.sun.tools.javac.code.Symbol) JCTree(com.sun.tools.javac.tree.JCTree)

Example 87 with Symbol

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

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);
    // Import-on-demand java.lang or ceylon.language.
    if (tree.isCeylonProgram)
        importAll(tree.pos, reader.enterPackage(names.ceylon_language), env);
    else
        importAll(tree.pos, reader.enterPackage(names.java_lang), env);
    // Process all import clauses.
    memberEnter(tree.defs, env);
}
Also used : Symbol(com.sun.tools.javac.code.Symbol)

Example 88 with Symbol

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

the class Lower method assertFlagTest.

/**************************************************************************
 * Code for enabling/disabling assertions.
 *************************************************************************/
// This code is not particularly robust if the user has
// previously declared a member named '$assertionsDisabled'.
// The same faulty idiom also appears in the translation of
// class literals above.  We should report an error if a
// previous declaration is not synthetic.
private JCExpression assertFlagTest(DiagnosticPosition pos) {
    // Outermost class may be either true class or an interface.
    ClassSymbol outermostClass = outermostClassDef.sym;
    // note that this is a class, as an interface can't contain a statement.
    ClassSymbol container = currentClass;
    VarSymbol assertDisabledSym = (VarSymbol) lookupSynthetic(dollarAssertionsDisabled, container.members());
    if (assertDisabledSym == null) {
        assertDisabledSym = new VarSymbol(STATIC | FINAL | SYNTHETIC, dollarAssertionsDisabled, syms.booleanType, container);
        enterSynthetic(pos, assertDisabledSym, container.members());
        Symbol desiredAssertionStatusSym = lookupMethod(pos, names.desiredAssertionStatus, types.erasure(syms.classType), List.<Type>nil());
        JCClassDecl containerDef = classDef(container);
        make_at(containerDef.pos());
        JCExpression notStatus = makeUnary(JCTree.NOT, make.App(make.Select(classOfType(types.erasure(outermostClass.type), containerDef.pos()), desiredAssertionStatusSym)));
        JCVariableDecl assertDisabledDef = make.VarDef(assertDisabledSym, notStatus);
        containerDef.defs = containerDef.defs.prepend(assertDisabledDef);
    }
    make_at(pos);
    return makeUnary(JCTree.NOT, make.Ident(assertDisabledSym));
}
Also used : Symbol(com.sun.tools.javac.code.Symbol)

Example 89 with Symbol

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

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.owner.kind & (VAR | MTH)) != 0) {
            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.getTag() == JCTree.SELECT) {
                thisArg = attr.makeNullCheck(translate(((JCFieldAccess) tree.meth).selected));
                tree.meth = make.Ident(constructor);
                ((JCIdent) tree.meth).name = methName;
            } else if ((c.owner.kind & (MTH | VAR)) != 0 || 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.getTag() == JCTree.APPLY) {
            JCMethodInvocation app = (JCMethodInvocation) tree.meth;
            app.args = tree.args.prependList(app.args);
            result = app;
            return;
        }
    }
    result = tree;
}
Also used : Type(com.sun.tools.javac.code.Type) Symbol(com.sun.tools.javac.code.Symbol)

Example 90 with Symbol

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

the class Lower method makeCall.

// where
/** Create an attributed tree of the form left.name(). */
private JCMethodInvocation makeCall(JCExpression left, Name name, List<JCExpression> args) {
    Assert.checkNonNull(left.type);
    Symbol funcsym = lookupMethod(make_pos, name, left.type, TreeInfo.types(args));
    return make.App(make.Select(left, funcsym), args);
}
Also used : 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