Search in sources :

Example 1 with IdeDeclaration

use of net.jangaroo.jooc.ast.IdeDeclaration in project jangaroo-tools by CoreMedia.

the class DeclarationScope method lookupDeclaration.

@Override
public IdeDeclaration lookupDeclaration(Ide ide) {
    IdeDeclaration decl = null;
    if (ide instanceof QualifiedIde) {
        String qname = ide.getQualifiedNameStr();
        if (importsByQualifiedName.containsKey(qname)) {
            return resolveImport(importsByQualifiedName.get(qname));
        }
        if (ide.isQualifiedByThis()) {
            return getClassDeclaration().resolvePropertyDeclaration(ide.getName());
        }
        if (ide.isQualifiedBySuper()) {
            final IdeDeclaration superTypeDeclaration = getClassDeclaration().getSuperTypeDeclaration();
            return superTypeDeclaration == null ? null : superTypeDeclaration.resolvePropertyDeclaration(ide.getName());
        }
    } else {
        final String name = ide.getName();
        final List<ImportDirective> importsOfThisIde = importsByName.get(name);
        if (importsOfThisIde != null) {
            if (importsOfThisIde.size() > 1) {
                ambigousImport(ide, importsOfThisIde);
            }
            return resolveImport(importsOfThisIde.get(0));
        }
        decl = ides.get(ide.getName());
        if (decl == null && getDefiningNode() != null && getClassDeclaration() == getDefiningNode()) {
            decl = getClassDeclaration().resolvePropertyDeclaration(ide.getName());
            if (decl != null && !isInstanceScope && !decl.isStatic()) {
                decl = null;
            }
        }
    }
    return decl != null ? decl : super.lookupDeclaration(ide);
}
Also used : ImportDirective(net.jangaroo.jooc.ast.ImportDirective) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration)

Example 2 with IdeDeclaration

use of net.jangaroo.jooc.ast.IdeDeclaration in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method visitClassDeclaration.

@Override
public void visitClassDeclaration(ClassDeclaration classDeclaration) throws IOException {
    out.beginString();
    writeModifiers(out, classDeclaration);
    out.writeSymbol(classDeclaration.getSymClass());
    classDeclaration.getIde().visit(this);
    visitIfNotNull(classDeclaration.getOptExtends());
    visitIfNotNull(classDeclaration.getOptImplements());
    out.endString();
    out.write(",");
    out.write(classDeclaration.getInheritanceLevel() + ",");
    out.write("function($$private){");
    writeAliases();
    out.write("return[");
    generateClassInits(classDeclaration);
    classDeclaration.getBody().visit(this);
    if (classDeclaration.getConstructor() == null && !classDeclaration.getFieldsWithInitializer().isEmpty()) {
        // generate default constructor that calls field initializers:
        out.write("\"public function " + classDeclaration.getName() + "\",function " + classDeclaration.getName() + "$(){");
        new SuperCallCodeGenerator(classDeclaration).generate(out, true);
        out.write("},");
    }
    for (IdeDeclaration secondaryDeclaration : classDeclaration.getSecondaryDeclarations()) {
        secondaryDeclaration.visit(this);
        out.writeToken(",");
    }
    out.write("undefined];},");
    generateStaticMethodList(classDeclaration);
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration)

Example 3 with IdeDeclaration

use of net.jangaroo.jooc.ast.IdeDeclaration in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method generateQualifiedIdeCodeAsExpr.

private void generateQualifiedIdeCodeAsExpr(QualifiedIde qualifiedIde) throws IOException {
    boolean commentOutQualifierCode = false;
    IdeDeclaration memberDeclaration = null;
    IdeDeclaration qualifierDeclaration = qualifiedIde.getQualifier().getDeclaration(false);
    if (qualifierDeclaration != null && qualifierDeclaration.isConstructor()) {
        qualifierDeclaration = qualifierDeclaration.getClassDeclaration();
    }
    if (qualifierDeclaration != null && qualifierDeclaration.equals(qualifiedIde.getScope().getClassDeclaration())) {
        memberDeclaration = ((ClassDeclaration) qualifierDeclaration).getStaticMemberDeclaration(qualifiedIde.getName());
        commentOutQualifierCode = memberDeclaration != null && memberDeclaration.isPrivateStatic();
    }
    if (memberDeclaration == null) {
        final IdeDeclaration type = qualifiedIde.getQualifier().resolveDeclaration();
        memberDeclaration = Ide.resolveMember(type, qualifiedIde);
    }
    if (qualifiedIde.isBound()) {
        writeBoundMethodAccess(qualifiedIde, qualifiedIde.getQualifier(), qualifiedIde.getSymDot(), memberDeclaration);
        return;
    }
    if (commentOutQualifierCode) {
        // we will generate another qualifier in writeMemberAccess
        out.beginComment();
    }
    qualifiedIde.getQualifier().visit(this);
    if (commentOutQualifierCode) {
        out.endComment();
    }
    writeMemberAccess(memberDeclaration, qualifiedIde.getSymDot(), qualifiedIde, true);
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration)

Example 4 with IdeDeclaration

use of net.jangaroo.jooc.ast.IdeDeclaration in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method generateSuperConstructorCallCode.

private void generateSuperConstructorCallCode(ClassDeclaration classDeclaration, ParenthesizedExpr<CommaSeparatedList<Expr>> args) throws IOException {
    String superClassQName = classDeclaration.getSuperTypeDeclaration().getQualifiedNameStr();
    if ("Error".equals(superClassQName)) {
        // built-in Error constructor called as function unfortunately always creates a new Error object, so we have to use emulation provided by Jangaroo Runtime:
        out.write("joo.Error");
    } else {
        Ide superClassIde = classDeclaration.getSuperType().getIde();
        out.writeSymbolWhitespace(superClassIde.getSymbol());
        IdeDeclaration superClassDeclaration = superClassIde.getDeclaration();
        String packageName = superClassDeclaration.getPackageDeclaration().getQualifiedNameStr();
        String qName = superClassDeclaration.getName();
        if (packageName.length() > 0) {
            String packageAuxVar = compilationUnit.getAuxVarForPackage(packageName);
            qName = CompilerUtils.qName(packageAuxVar, qName);
        }
        out.write(qName);
    }
    out.writeToken(".call");
    if (args == null) {
        out.writeToken("(this)");
    } else {
        out.writeSymbol(args.getLParen());
        out.writeToken("this");
        CommaSeparatedList<Expr> arguments = args.getExpr();
        if (arguments != null) {
            if (arguments.getHead() != null) {
                out.writeToken(",");
            }
            arguments.visit(this);
        }
        out.writeSymbol(args.getRParen());
    }
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration) IdeExpr(net.jangaroo.jooc.ast.IdeExpr) ParenthesizedExpr(net.jangaroo.jooc.ast.ParenthesizedExpr) DotExpr(net.jangaroo.jooc.ast.DotExpr) AssignmentOpExpr(net.jangaroo.jooc.ast.AssignmentOpExpr) Expr(net.jangaroo.jooc.ast.Expr) FunctionExpr(net.jangaroo.jooc.ast.FunctionExpr) ArrayIndexExpr(net.jangaroo.jooc.ast.ArrayIndexExpr) InfixOpExpr(net.jangaroo.jooc.ast.InfixOpExpr) NewExpr(net.jangaroo.jooc.ast.NewExpr) AsExpr(net.jangaroo.jooc.ast.AsExpr) ApplyExpr(net.jangaroo.jooc.ast.ApplyExpr) Ide(net.jangaroo.jooc.ast.Ide) NamespacedIde(net.jangaroo.jooc.ast.NamespacedIde) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde)

Example 5 with IdeDeclaration

use of net.jangaroo.jooc.ast.IdeDeclaration in project jangaroo-tools by CoreMedia.

the class JsCodeGenerator method visitForInStatement.

@Override
public void visitForInStatement(final ForInStatement forInStatement) throws IOException {
    final Ide exprAuxIde = forInStatement.getExprAuxIde();
    IdeDeclaration exprType = forInStatement.getExpr().getType();
    String exprTypeName = exprType != null ? exprType.getQualifiedNameStr() : "";
    boolean iterateArrayMode = "Array".equals(exprTypeName) || "Vector$object".equals(exprTypeName);
    if (exprAuxIde != null && !iterateArrayMode) {
        new SemicolonTerminatedStatement(new VariableDeclaration(SYM_VAR, exprAuxIde, null, null), SYM_SEMICOLON).visit(this);
    }
    out.writeSymbol(forInStatement.getSymKeyword());
    final boolean isForEach = forInStatement.getSymEach() != null;
    if (isForEach) {
        out.beginComment();
        out.writeSymbol(forInStatement.getSymEach());
        out.endComment();
    }
    out.writeSymbol(forInStatement.getLParen());
    if (isForEach || iterateArrayMode) {
        new VariableDeclaration(SYM_VAR, forInStatement.getAuxIde(), null, null).visit(this);
    } else {
        if (forInStatement.getDecl() != null) {
            forInStatement.getDecl().visit(this);
        } else {
            forInStatement.getLValue().visit(this);
        }
    }
    if (iterateArrayMode) {
        String indexVarName = forInStatement.getAuxIde().getName();
        out.write("=0");
        if (exprAuxIde != null) {
            out.write(",");
            out.writeToken(exprAuxIde.getName());
            out.writeToken("=");
            out.beginComment();
            out.writeSymbol(forInStatement.getSymIn());
            out.endComment();
            forInStatement.getExpr().visit(this);
        }
        out.write(";");
        out.write(indexVarName);
        out.write("<");
        if (exprAuxIde != null) {
            out.writeToken(exprAuxIde.getName());
        } else {
            out.beginComment();
            out.writeSymbol(forInStatement.getSymIn());
            out.endComment();
            forInStatement.getExpr().visit(this);
        }
        out.write(".length;");
        out.write("++" + indexVarName);
    } else {
        out.writeSymbol(forInStatement.getSymIn());
        if (exprAuxIde != null) {
            // assign the expression value to the auxiliary expression value variable once:
            out.writeToken(exprAuxIde.getName());
            out.writeToken("=");
        }
        forInStatement.getExpr().visit(this);
    }
    out.writeSymbol(forInStatement.getRParen());
    if (isForEach || iterateArrayMode) {
        // inject synthesized statement into loop body:
        if (!(forInStatement.getBody() instanceof BlockStatement)) {
            forInStatement.setBody(new BlockStatement(SYM_LBRACE, Arrays.<Directive>asList(forInStatement.getBody()), SYM_RBRACE));
        }
        ((BlockStatement) forInStatement.getBody()).addBlockStartCodeGenerator(new CodeGenerator() {

            @Override
            public void generate(JsWriter out, boolean first) throws IOException {
                // synthesize assigning the correct index to the variable given in the original for each statement:
                if (forInStatement.getDecl() != null) {
                    forInStatement.getDecl().visit(JsCodeGenerator.this);
                } else {
                    forInStatement.getLValue().visit(JsCodeGenerator.this);
                }
                out.writeToken("=");
                if (!isForEach) {
                    out.write("String(" + forInStatement.getAuxIde().getName() + ")");
                } else {
                    if (exprAuxIde == null) {
                        forInStatement.getExpr().visit(JsCodeGenerator.this);
                    } else {
                        out.write(exprAuxIde.getName());
                    }
                    out.write("[" + forInStatement.getAuxIde().getName() + "]");
                }
                out.write(";");
            }
        });
    }
    forInStatement.getBody().visit(this);
}
Also used : IdeDeclaration(net.jangaroo.jooc.ast.IdeDeclaration) TypedIdeDeclaration(net.jangaroo.jooc.ast.TypedIdeDeclaration) VariableDeclaration(net.jangaroo.jooc.ast.VariableDeclaration) BlockStatement(net.jangaroo.jooc.ast.BlockStatement) Ide(net.jangaroo.jooc.ast.Ide) NamespacedIde(net.jangaroo.jooc.ast.NamespacedIde) QualifiedIde(net.jangaroo.jooc.ast.QualifiedIde) CodeGenerator(net.jangaroo.jooc.CodeGenerator) IOException(java.io.IOException) SemicolonTerminatedStatement(net.jangaroo.jooc.ast.SemicolonTerminatedStatement) UseNamespaceDirective(net.jangaroo.jooc.ast.UseNamespaceDirective) Directive(net.jangaroo.jooc.ast.Directive) ImportDirective(net.jangaroo.jooc.ast.ImportDirective) JsWriter(net.jangaroo.jooc.JsWriter)

Aggregations

IdeDeclaration (net.jangaroo.jooc.ast.IdeDeclaration)8 TypedIdeDeclaration (net.jangaroo.jooc.ast.TypedIdeDeclaration)5 Ide (net.jangaroo.jooc.ast.Ide)3 QualifiedIde (net.jangaroo.jooc.ast.QualifiedIde)3 ImportDirective (net.jangaroo.jooc.ast.ImportDirective)2 NamespacedIde (net.jangaroo.jooc.ast.NamespacedIde)2 VariableDeclaration (net.jangaroo.jooc.ast.VariableDeclaration)2 IOException (java.io.IOException)1 CodeGenerator (net.jangaroo.jooc.CodeGenerator)1 JooSymbol (net.jangaroo.jooc.JooSymbol)1 JsWriter (net.jangaroo.jooc.JsWriter)1 ApplyExpr (net.jangaroo.jooc.ast.ApplyExpr)1 ArrayIndexExpr (net.jangaroo.jooc.ast.ArrayIndexExpr)1 AsExpr (net.jangaroo.jooc.ast.AsExpr)1 AssignmentOpExpr (net.jangaroo.jooc.ast.AssignmentOpExpr)1 BlockStatement (net.jangaroo.jooc.ast.BlockStatement)1 Directive (net.jangaroo.jooc.ast.Directive)1 DotExpr (net.jangaroo.jooc.ast.DotExpr)1 Expr (net.jangaroo.jooc.ast.Expr)1 FunctionExpr (net.jangaroo.jooc.ast.FunctionExpr)1