Search in sources :

Example 56 with JCTree

use of com.sun.tools.javac.tree.JCTree 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 57 with JCTree

use of com.sun.tools.javac.tree.JCTree 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 58 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class TransTypes method visitTypeApply.

/** Visitor method for parameterized types.
     */
public void visitTypeApply(JCTypeApply tree) {
    JCTree clazz = translate(tree.clazz, null);
    result = clazz;
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree)

Example 59 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class MemberEnter method visitImport.

// process the non-static imports and the static imports of types.
public void visitImport(JCImport tree) {
    JCTree imp = tree.qualid;
    Name name = TreeInfo.name(imp);
    TypeSymbol p;
    // Create a local environment pointing to this tree to disable
    // effects of other imports in Resolve.findGlobalType
    Env<AttrContext> localEnv = env.dup(tree);
    // Attribute qualifying package or class.
    JCFieldAccess s = (JCFieldAccess) imp;
    p = attr.attribTree(s.selected, localEnv, tree.staticImport ? TYP : (TYP | PCK), Type.noType).tsym;
    if (name == names.asterisk) {
        // Import on demand.
        chk.checkCanonical(s.selected);
        if (tree.staticImport)
            importStaticAll(tree.pos, p, env);
        else
            importAll(tree.pos, p, env);
    } else {
        // Named type import.
        if (tree.staticImport) {
            importNamedStatic(tree.pos(), p, name, localEnv);
            chk.checkCanonical(s.selected);
        } else {
            TypeSymbol c = attribImportType(imp, localEnv).tsym;
            chk.checkCanonical(imp);
            importNamed(tree.pos(), c, env);
        }
    }
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree)

Example 60 with JCTree

use of com.sun.tools.javac.tree.JCTree in project ceylon-compiler by ceylon.

the class Lower method makeTwrBlock.

private JCBlock makeTwrBlock(List<JCTree> resources, JCBlock block, int depth) {
    if (resources.isEmpty())
        return block;
    // Add resource declaration or expression to block statements
    ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>();
    JCTree resource = resources.head;
    JCExpression expr = null;
    if (resource instanceof JCVariableDecl) {
        JCVariableDecl var = (JCVariableDecl) resource;
        expr = make.Ident(var.sym).setType(resource.type);
        stats.add(var);
    } else {
        Assert.check(resource instanceof JCExpression);
        VarSymbol syntheticTwrVar = new VarSymbol(SYNTHETIC | FINAL, makeSyntheticName(names.fromString("twrVar" + depth), twrVars), (resource.type.tag == TypeTags.BOT) ? syms.autoCloseableType : resource.type, currentMethodSym);
        twrVars.enter(syntheticTwrVar);
        JCVariableDecl syntheticTwrVarDecl = make.VarDef(syntheticTwrVar, (JCExpression) resource);
        expr = (JCExpression) make.Ident(syntheticTwrVar);
        stats.add(syntheticTwrVarDecl);
    }
    // Add primaryException declaration
    VarSymbol primaryException = new VarSymbol(SYNTHETIC, makeSyntheticName(names.fromString("primaryException" + depth), twrVars), syms.throwableType, currentMethodSym);
    twrVars.enter(primaryException);
    JCVariableDecl primaryExceptionTreeDecl = make.VarDef(primaryException, makeNull());
    stats.add(primaryExceptionTreeDecl);
    // Create catch clause that saves exception and then rethrows it
    VarSymbol param = new VarSymbol(FINAL | SYNTHETIC, names.fromString("t" + target.syntheticNameChar()), syms.throwableType, currentMethodSym);
    JCVariableDecl paramTree = make.VarDef(param, null);
    JCStatement assign = make.Assignment(primaryException, make.Ident(param));
    JCStatement rethrowStat = make.Throw(make.Ident(param));
    JCBlock catchBlock = make.Block(0L, List.<JCStatement>of(assign, rethrowStat));
    JCCatch catchClause = make.Catch(paramTree, catchBlock);
    int oldPos = make.pos;
    make.at(TreeInfo.endPos(block));
    JCBlock finallyClause = makeTwrFinallyClause(primaryException, expr);
    make.at(oldPos);
    JCTry outerTry = make.Try(makeTwrBlock(resources.tail, block, depth + 1), List.<JCCatch>of(catchClause), finallyClause);
    stats.add(outerTry);
    return make.Block(0L, stats.toList());
}
Also used : JCTree(com.sun.tools.javac.tree.JCTree)

Aggregations

JCTree (com.sun.tools.javac.tree.JCTree)183 JCExpression (com.sun.tools.javac.tree.JCTree.JCExpression)28 Symbol (com.sun.tools.javac.code.Symbol)22 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)22 Type (com.sun.tools.javac.code.Type)19 Tree (com.redhat.ceylon.compiler.typechecker.tree.Tree)17 Tree (com.sun.source.tree.Tree)15 ExpressionTree (com.sun.source.tree.ExpressionTree)14 JCFieldAccess (com.sun.tools.javac.tree.JCTree.JCFieldAccess)14 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)11 JCClassDecl (com.sun.tools.javac.tree.JCTree.JCClassDecl)11 JCStatement (com.sun.tools.javac.tree.JCTree.JCStatement)10 ArrayList (java.util.ArrayList)10 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)9 JCMethodDecl (com.sun.tools.javac.tree.JCTree.JCMethodDecl)9 JCNewClass (com.sun.tools.javac.tree.JCTree.JCNewClass)8 ListBuffer (com.sun.tools.javac.util.ListBuffer)8 Type (com.redhat.ceylon.model.typechecker.model.Type)7 ClassTree (com.sun.source.tree.ClassTree)7 MemberSelectTree (com.sun.source.tree.MemberSelectTree)7