Search in sources :

Example 1 with JCClassDecl

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl in project ceylon by eclipse.

the class CallableBuilder method buildTypeConstructor.

protected JCExpression buildTypeConstructor(Type callableType, JCNewClass callableInstance) {
    JCExpression result;
    // Wrap in an anonymous TypeConstructor subcla
    MethodDefinitionBuilder rawApply = MethodDefinitionBuilder.systemMethod(gen, Naming.Unfix.apply.toString());
    rawApply.modifiers(Flags.PUBLIC);
    rawApply.isOverride(true);
    // for (TypeParameter tp : typeModel.getDeclaration().getTypeParameters()) {
    // apply.typeParameter(tp);
    // }
    rawApply.resultType(new TransformedType(gen.makeJavaType(callableType, AbstractTransformer.JT_RAW)));
    {
        ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(gen, "applied");
        pdb.modifiers(Flags.FINAL);
        pdb.type(new TransformedType(gen.make().TypeArray(gen.make().Type(gen.syms().ceylonTypeDescriptorType))));
        rawApply.parameter(pdb);
    }
    rawApply.body(List.<JCStatement>of(gen.make().Return(gen.make().Apply(null, gen.naming.makeUnquotedIdent(Naming.Unfix.$apply$.toString()), List.<JCExpression>of(gen.naming.makeUnquotedIdent("applied"))))));
    MethodDefinitionBuilder typedApply = MethodDefinitionBuilder.systemMethod(gen, Naming.Unfix.$apply$.toString());
    typedApply.modifiers(Flags.PRIVATE);
    // for (TypeParameter tp : typeModel.getDeclaration().getTypeParameters()) {
    // apply.typeParameter(tp);
    // }
    typedApply.resultType(new TransformedType(gen.makeJavaType(callableType)));
    {
        ParameterDefinitionBuilder pdb = ParameterDefinitionBuilder.systemParameter(gen, "applied");
        pdb.modifiers(Flags.FINAL);
        pdb.type(new TransformedType(gen.make().TypeArray(gen.make().Type(gen.syms().ceylonTypeDescriptorType))));
        typedApply.parameter(pdb);
    }
    ListBuffer<JCTypeParameter> typeParameters = new ListBuffer<JCTypeParameter>();
    for (TypeParameter typeParameter : Strategy.getEffectiveTypeParameters(typeModel.getDeclaration())) {
        Type typeArgument = typeModel.getTypeArguments().get(typeParameter);
        typeParameters.add(gen.makeTypeParameter(typeParameter, null));
        typedApply.body(gen.makeVar(Flags.FINAL, gen.naming.getTypeArgumentDescriptorName(typeParameter), gen.make().Type(gen.syms().ceylonTypeDescriptorType), gen.make().Indexed(gen.makeUnquotedIdent("applied"), gen.make().Literal(typeModel.getTypeArgumentList().indexOf(typeArgument)))));
    }
    typedApply.body(gen.make().Return(callableInstance));
    // typedApply.body(body.toList());
    MethodDefinitionBuilder ctor = MethodDefinitionBuilder.constructor(gen, false);
    ctor.body(gen.make().Exec(gen.make().Apply(null, gen.naming.makeSuper(), List.<JCExpression>of(gen.make().Literal(typeModel.asString(true))))));
    SyntheticName n = gen.naming.synthetic(typeModel.getDeclaration().getName());
    JCClassDecl classDef = gen.make().ClassDef(gen.make().Modifiers(0, List.<JCAnnotation>nil()), // name,
    n.asName(), typeParameters.toList(), // extending
    gen.make().QualIdent(gen.syms().ceylonAbstractTypeConstructorType.tsym), // implementing,
    List.<JCExpression>nil(), List.<JCTree>of(ctor.build(), rawApply.build(), typedApply.build()));
    result = gen.make().LetExpr(List.<JCStatement>of(classDef), gen.make().NewClass(null, null, n.makeIdent(), List.<JCExpression>nil(), // List.<JCExpression>of(gen.make().Literal(typeModel.asString(true))),
    null));
    return result;
}
Also used : TypeParameter(org.eclipse.ceylon.model.typechecker.model.TypeParameter) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) SyntheticName(org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName) JCStatement(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement) JCTypeParameter(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCTypeParameter) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) JCAnnotation(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)

Example 2 with JCClassDecl

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl in project ceylon by eclipse.

the class CallableBuilder method build.

public JCExpression build() {
    // Generate a subclass of Callable
    ListBuffer<JCTree> classBody = new ListBuffer<JCTree>();
    gen.at(node);
    if (parameterDefaultValueMethods != null) {
        for (MethodDefinitionBuilder mdb : parameterDefaultValueMethods) {
            classBody.append(mdb.build());
        }
    }
    transformation.appendMethods(classBody);
    gen.at(node);
    JCClassDecl classDef = gen.make().AnonymousClassDef(gen.make().Modifiers(0, annotations != null ? annotations : List.<JCAnnotation>nil()), classBody.toList());
    int variadicIndex = isVariadic ? numParams - 1 : -1;
    Type callableType;
    if (typeModel.isTypeConstructor()) {
        callableType = typeModel.getDeclaration().getExtendedType();
    } else {
        callableType = typeModel;
    }
    JCNewClass callableInstance;
    if (functionalInterface == null) {
        callableInstance = gen.at(node).NewClass(null, null, gen.makeJavaType(callableType, JT_EXTENDS | JT_CLASS_NEW), List.<JCExpression>of(gen.makeReifiedTypeArgument(callableType.getTypeArgumentList().get(0)), gen.makeReifiedTypeArgument(callableType.getTypeArgumentList().get(1)), gen.make().Literal(callableType.asString(true)), gen.make().TypeCast(gen.syms().shortType, gen.makeInteger(variadicIndex))), classDef);
    } else {
        callableInstance = gen.make().NewClass(null, null, gen.makeJavaType(functionalInterface, JT_EXTENDS | JT_CLASS_NEW), List.<JCExpression>nil(), classDef);
    }
    JCExpression result;
    if (typeModel.isTypeConstructor()) {
        result = buildTypeConstructor(callableType, callableInstance);
    } else {
        result = callableInstance;
    }
    gen.at(null);
    if (instanceSubstitution != null) {
        instanceSubstitution.close();
    }
    return result;
}
Also used : JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) Type(org.eclipse.ceylon.model.typechecker.model.Type) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) ListBuffer(org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) JCNewClass(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCNewClass)

Example 3 with JCClassDecl

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl in project ceylon by eclipse.

the class JavacTrees method getElement.

public Symbol getElement(TreePath path) {
    JCTree tree = (JCTree) path.getLeaf();
    Symbol sym = TreeInfo.symbolFor(tree);
    if (sym == null) {
        if (TreeInfo.isDeclaration(tree)) {
            for (TreePath p = path; p != null; p = p.getParentPath()) {
                JCTree t = (JCTree) p.getLeaf();
                if (t.hasTag(JCTree.Tag.CLASSDEF)) {
                    JCClassDecl ct = (JCClassDecl) t;
                    if (ct.sym != null) {
                        if ((ct.sym.flags_field & Flags.UNATTRIBUTED) != 0) {
                            attr.attribClass(ct.pos(), ct.sym);
                            sym = TreeInfo.symbolFor(tree);
                        }
                        break;
                    }
                }
            }
        }
    }
    return sym;
}
Also used : JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) TreePath(org.eclipse.ceylon.langtools.source.util.TreePath) VarSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.VarSymbol) MethodSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)

Example 4 with JCClassDecl

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl in project ceylon by eclipse.

the class JavacTrees method getAttrContext.

private Env<AttrContext> getAttrContext(TreePath path) {
    if (// implicit null-check
    !(path.getLeaf() instanceof JCTree))
        throw new IllegalArgumentException();
    // will already have been entered.
    if (javacTaskImpl != null) {
        try {
            javacTaskImpl.enter(null);
        } catch (IOException e) {
            throw new Error("unexpected error while entering symbols: " + e);
        }
    }
    JCCompilationUnit unit = (JCCompilationUnit) path.getCompilationUnit();
    Copier copier = createCopier(treeMaker.forToplevel(unit));
    Env<AttrContext> env = null;
    JCMethodDecl method = null;
    JCVariableDecl field = null;
    List<Tree> l = List.nil();
    TreePath p = path;
    while (p != null) {
        l = l.prepend(p.getLeaf());
        p = p.getParentPath();
    }
    for (; l.nonEmpty(); l = l.tail) {
        Tree tree = l.head;
        switch(tree.getKind()) {
            case COMPILATION_UNIT:
                // System.err.println("COMP: " + ((JCCompilationUnit)tree).sourcefile);
                env = enter.getTopLevelEnv((JCCompilationUnit) tree);
                break;
            case ANNOTATION_TYPE:
            case CLASS:
            case ENUM:
            case INTERFACE:
                // System.err.println("CLASS: " + ((JCClassDecl)tree).sym.getSimpleName());
                env = enter.getClassEnv(((JCClassDecl) tree).sym);
                break;
            case METHOD:
                // System.err.println("METHOD: " + ((JCMethodDecl)tree).sym.getSimpleName());
                method = (JCMethodDecl) tree;
                env = memberEnter.getMethodEnv(method, env);
                break;
            case VARIABLE:
                // System.err.println("FIELD: " + ((JCVariableDecl)tree).sym.getSimpleName());
                field = (JCVariableDecl) tree;
                break;
            case BLOCK:
                {
                    // System.err.println("BLOCK: ");
                    if (method != null) {
                        try {
                            Assert.check(method.body == tree);
                            method.body = copier.copy((JCBlock) tree, (JCTree) path.getLeaf());
                            env = attribStatToTree(method.body, env, copier.leafCopy);
                        } finally {
                            method.body = (JCBlock) tree;
                        }
                    } else {
                        JCBlock body = copier.copy((JCBlock) tree, (JCTree) path.getLeaf());
                        env = attribStatToTree(body, env, copier.leafCopy);
                    }
                    return env;
                }
            default:
                // System.err.println("DEFAULT: " + tree.getKind());
                if (field != null && field.getInitializer() == tree) {
                    env = memberEnter.getInitEnv(field, env);
                    JCExpression expr = copier.copy((JCExpression) tree, (JCTree) path.getLeaf());
                    env = attribExprToTree(expr, env, copier.leafCopy);
                    return env;
                }
        }
    }
    return (field != null) ? memberEnter.getInitEnv(field, env) : env;
}
Also used : JCCompilationUnit(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit) JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) JCBlock(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock) JCMethodDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodDecl) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) IOException(java.io.IOException) AttrContext(org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext) JCVariableDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl) JCExpression(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression) TreePath(org.eclipse.ceylon.langtools.source.util.TreePath) TreeCopier(org.eclipse.ceylon.langtools.tools.javac.tree.TreeCopier) CatchTree(org.eclipse.ceylon.langtools.source.tree.CatchTree) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) CompilationUnitTree(org.eclipse.ceylon.langtools.source.tree.CompilationUnitTree) Tree(org.eclipse.ceylon.langtools.source.tree.Tree)

Example 5 with JCClassDecl

use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl in project ceylon by eclipse.

the class JavacTrees method getTree.

public JCTree getTree(Element element) {
    Symbol symbol = (Symbol) element;
    TypeSymbol enclosing = symbol.enclClass();
    Env<AttrContext> env = enter.getEnv(enclosing);
    if (env == null)
        return null;
    JCClassDecl classNode = env.enclClass;
    if (classNode != null) {
        if (TreeInfo.symbolFor(classNode) == element)
            return classNode;
        for (JCTree node : classNode.getMembers()) if (TreeInfo.symbolFor(node) == element)
            return node;
    }
    return null;
}
Also used : JCClassDecl(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl) VarSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.VarSymbol) MethodSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) ClassSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol) Symbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol) JCTree(org.eclipse.ceylon.langtools.tools.javac.tree.JCTree) TypeSymbol(org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol) AttrContext(org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext)

Aggregations

JCClassDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCClassDecl)6 JCTree (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree)5 JCExpression (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCExpression)3 IOException (java.io.IOException)2 TreePath (org.eclipse.ceylon.langtools.source.util.TreePath)2 Symbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol)2 ClassSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.ClassSymbol)2 MethodSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.MethodSymbol)2 TypeSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.TypeSymbol)2 VarSymbol (org.eclipse.ceylon.langtools.tools.javac.code.Symbol.VarSymbol)2 AttrContext (org.eclipse.ceylon.langtools.tools.javac.comp.AttrContext)2 JCAnnotation (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation)2 JCBlock (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCBlock)2 JCCompilationUnit (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCCompilationUnit)2 JCMethodDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCMethodDecl)2 JCVariableDecl (org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCVariableDecl)2 ListBuffer (org.eclipse.ceylon.langtools.tools.javac.util.ListBuffer)2 Type (org.eclipse.ceylon.model.typechecker.model.Type)2 CharArrayWriter (java.io.CharArrayWriter)1 SyntheticName (org.eclipse.ceylon.compiler.java.codegen.Naming.SyntheticName)1