Search in sources :

Example 1 with PrototypeInitCallback

use of org.eclipse.ceylon.compiler.js.GenerateJsVisitor.PrototypeInitCallback in project ceylon by eclipse.

the class TypeGenerator method initializeType.

/**
 * Generates a function to initialize the specified type.
 * @param initDeferrer
 */
static void initializeType(final Node type, final GenerateJsVisitor gen, InitDeferrer initDeferrer) {
    Tree.ExtendedType extendedType = null;
    Tree.SatisfiedTypes satisfiedTypes = null;
    final ClassOrInterface decl;
    final List<Tree.Statement> stmts;
    Value objDecl = null;
    if (type instanceof Tree.ClassDefinition) {
        Tree.ClassDefinition classDef = (Tree.ClassDefinition) type;
        extendedType = classDef.getExtendedType();
        satisfiedTypes = classDef.getSatisfiedTypes();
        decl = classDef.getDeclarationModel();
        Tree.Declaration nh = gen.getNativeHeader(decl);
        if (nh == null && NativeUtil.hasNativeMembers(decl)) {
            nh = classDef;
        }
        stmts = NativeUtil.mergeStatements(classDef.getClassBody(), nh, Backend.JavaScript);
    } else if (type instanceof Tree.InterfaceDefinition) {
        satisfiedTypes = ((Tree.InterfaceDefinition) type).getSatisfiedTypes();
        decl = ((Tree.InterfaceDefinition) type).getDeclarationModel();
        final Tree.InterfaceDefinition idef = (Tree.InterfaceDefinition) type;
        Tree.Declaration nh = gen.getNativeHeader(decl);
        if (nh == null && NativeUtil.hasNativeMembers(decl)) {
            nh = idef;
        }
        stmts = NativeUtil.mergeStatements(idef.getInterfaceBody(), nh, Backend.JavaScript);
    } else if (type instanceof Tree.ObjectDefinition) {
        Tree.ObjectDefinition objectDef = (Tree.ObjectDefinition) type;
        extendedType = objectDef.getExtendedType();
        satisfiedTypes = objectDef.getSatisfiedTypes();
        decl = (ClassOrInterface) objectDef.getDeclarationModel().getTypeDeclaration();
        objDecl = objectDef.getDeclarationModel();
        Tree.Declaration nh = gen.getNativeHeader(decl);
        if (nh == null && NativeUtil.hasNativeMembers(decl)) {
            nh = objectDef;
        }
        stmts = NativeUtil.mergeStatements(objectDef.getClassBody(), nh, Backend.JavaScript);
    } else if (type instanceof Tree.ObjectExpression) {
        Tree.ObjectExpression objectDef = (Tree.ObjectExpression) type;
        extendedType = objectDef.getExtendedType();
        satisfiedTypes = objectDef.getSatisfiedTypes();
        decl = (ClassOrInterface) objectDef.getAnonymousClass();
        stmts = objectDef.getClassBody().getStatements();
    } else if (type instanceof Tree.Enumerated) {
        Tree.Enumerated vc = (Tree.Enumerated) type;
        stmts = vc.getBlock().getStatements();
        decl = (ClassOrInterface) vc.getDeclarationModel().getTypeDeclaration().getContainer();
    } else {
        stmts = null;
        decl = null;
    }
    final PrototypeInitCallback callback = new PrototypeInitCallback() {

        @Override
        public void addToPrototypeCallback() {
            if (decl != null) {
                gen.addToPrototype(type, decl, stmts);
            }
        }
    };
    typeInitialization(extendedType, satisfiedTypes, decl, callback, gen, objDecl, initDeferrer);
}
Also used : ClassOrInterface(org.eclipse.ceylon.model.typechecker.model.ClassOrInterface) Value(org.eclipse.ceylon.model.typechecker.model.Value) Tree(org.eclipse.ceylon.compiler.typechecker.tree.Tree) TypedDeclaration(org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) TypeDeclaration(org.eclipse.ceylon.model.typechecker.model.TypeDeclaration) Declaration(org.eclipse.ceylon.model.typechecker.model.Declaration) PrototypeInitCallback(org.eclipse.ceylon.compiler.js.GenerateJsVisitor.PrototypeInitCallback)

Aggregations

PrototypeInitCallback (org.eclipse.ceylon.compiler.js.GenerateJsVisitor.PrototypeInitCallback)1 Tree (org.eclipse.ceylon.compiler.typechecker.tree.Tree)1 ClassOrInterface (org.eclipse.ceylon.model.typechecker.model.ClassOrInterface)1 Declaration (org.eclipse.ceylon.model.typechecker.model.Declaration)1 TypeDeclaration (org.eclipse.ceylon.model.typechecker.model.TypeDeclaration)1 TypedDeclaration (org.eclipse.ceylon.model.typechecker.model.TypedDeclaration)1 Value (org.eclipse.ceylon.model.typechecker.model.Value)1