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);
}
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);
}
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);
}
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());
}
}
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);
}
Aggregations