use of org.eclipse.ceylon.model.typechecker.model.Constructor in project ceylon by eclipse.
the class GenerateJsVisitor method memberAccessBase.
String memberAccessBase(Node node, Declaration decl, boolean setter, String lhs) {
final StringBuilder sb = new StringBuilder(getMember(node, lhs));
final boolean isConstructor = decl instanceof Constructor;
if (sb.length() > 0) {
if (node instanceof Tree.BaseMemberOrTypeExpression) {
Declaration bmd = ((Tree.BaseMemberOrTypeExpression) node).getDeclaration();
if (bmd.isParameter() && bmd.getContainer() instanceof ClassAlias) {
return sb.toString();
}
}
sb.append(isConstructor ? names.constructorSeparator(decl) : ".");
}
boolean metaGetter = false;
Scope scope = getSuperMemberScope(node);
if (opts.isOptimize() && scope != null && !isConstructor) {
sb.append("getT$all()['").append(scope.getQualifiedNameString()).append("']");
if (AttributeGenerator.defineAsProperty(decl)) {
return getClAlias() + (setter ? "attrSetter(" : "attrGetter(") + sb.toString() + ",'" + names.name(decl) + "')";
}
sb.append(".$$.prototype.");
metaGetter = true;
}
final String member = accessThroughGetter(decl) && !accessDirectly(decl) ? (setter ? names.setter(decl) : names.getter(decl, metaGetter)) : names.name(decl);
if (!isConstructor && ModelUtil.isConstructor(decl)) {
sb.append(names.name((Declaration) decl.getContainer())).append(names.constructorSeparator(decl));
}
sb.append(member);
if (!opts.isOptimize() && (scope != null)) {
sb.append(names.scopeSuffix(scope));
}
return sb.toString();
}
use of org.eclipse.ceylon.model.typechecker.model.Constructor in project ceylon by eclipse.
the class GenerateJsVisitor method generateConstructorStatements.
/**
* Like visitStatements but for constructors, because we need to check if a statement is
* a specifier expression and generate it regardless of lexical/prototype mode.
*/
void generateConstructorStatements(final Tree.Declaration cnstr, List<? extends Tree.Statement> stmts) {
final List<String> oldRetainedVars = retainedVars.reset(null);
final List<? extends Statement> prevStatements = currentStatements;
currentStatements = stmts;
ReturnConstructorVisitor rcv = new ReturnConstructorVisitor(cnstr);
if (rcv.isReturns()) {
out("(function(){");
}
for (Tree.Statement s2 : stmts) {
if (s2 instanceof Tree.SpecifierStatement) {
Tree.SpecifierStatement ss = (Tree.SpecifierStatement) s2;
if (cnstr instanceof Tree.Constructor) {
specifierStatement(((Tree.Constructor) cnstr).getConstructor(), ss);
} else if (cnstr instanceof Tree.Enumerated) {
specifierStatement(((Tree.Enumerated) cnstr).getEnumerated(), ss);
}
} else {
s2.visit(this);
}
if (!opts.isMinify())
beginNewLine();
retainedVars.emitRetainedVars(this);
// every constructor adds metamodel to that attribute
if (!opts.isOptimize() && cnstr != null && s2 instanceof Tree.AttributeDeclaration) {
generatedAttributes.remove(((Tree.AttributeDeclaration) s2).getDeclarationModel());
}
}
if (rcv.isReturns()) {
out("}());");
}
retainedVars.reset(oldRetainedVars);
currentStatements = prevStatements;
}
use of org.eclipse.ceylon.model.typechecker.model.Constructor in project ceylon by eclipse.
the class GenerateJsVisitor method visit.
@Override
public void visit(final Tree.QualifiedMemberExpression that) {
if (errVisitor.hasErrors(that))
return;
// Big TODO: make sure the member is actually
// refined by the current class!
final Declaration d = that.getDeclaration();
if (that.getMemberOperator() instanceof Tree.SafeMemberOp) {
Operators.generateSafeOp(that, this);
} else if (that.getMemberOperator() instanceof Tree.SpreadOp) {
SequenceGenerator.generateSpread(that, this);
} else if (d instanceof Function && that.getSignature() == null) {
// TODO right now this causes that all method invocations are done this way
// we need to filter somehow to only use this pattern when the result is supposed to be a callable
// looks like checking for signature is a good way (not THE way though; named arg calls don't have signature)
FunctionHelper.generateCallable(that, null, this);
} else if (that.getStaticMethodReference() && d != null) {
if (d instanceof Value && ModelUtil.isConstructor(d)) {
Constructor cnst = (Constructor) ((Value) d).getTypeDeclaration();
if (cnst.getTypescriptEnum() != null && cnst.getTypescriptEnum().matches("[0-9.-]+")) {
out(cnst.getTypescriptEnum());
} else {
// TODO this won't work anymore I think
boolean wrap = false;
if (that.getPrimary() instanceof Tree.QualifiedMemberOrTypeExpression) {
Tree.QualifiedMemberOrTypeExpression prim = (Tree.QualifiedMemberOrTypeExpression) that.getPrimary();
if (prim.getStaticMethodReference()) {
wrap = true;
out("function(_$){return _$");
} else {
prim.getPrimary().visit(this);
}
out(".");
} else {
if (d.getContainer() instanceof Declaration) {
qualify(that.getPrimary(), (Declaration) d.getContainer());
} else if (d.getContainer() instanceof Package) {
out(names.moduleAlias(((Package) d.getContainer()).getModule()));
}
}
if (cnst.getTypescriptEnum() != null) {
out(names.name((TypeDeclaration) d.getContainer()), ".", cnst.getTypescriptEnum());
} else {
out(names.name((TypeDeclaration) d.getContainer()), names.constructorSeparator(d), names.name(d), "()");
}
if (wrap) {
out(";}");
}
}
} else if (d instanceof Function) {
Function fd = (Function) d;
if (fd.getTypeDeclaration() instanceof Constructor) {
that.getPrimary().visit(this);
out(names.constructorSeparator(fd), names.name(fd.getTypeDeclaration()));
} else if (fd.isStatic()) {
BmeGenerator.generateStaticReference(that, fd, this);
} else {
out("function($O$){return ");
if (BmeGenerator.hasTypeParameters(that)) {
BmeGenerator.printGenericMethodReference(this, that, "$O$", "$O$." + names.name(d));
} else {
out(getClAlias(), "jsc$3($O$,$O$.", names.name(d), ")");
}
out(";}");
}
} else {
if (d.isStatic()) {
BmeGenerator.generateStaticReference(that, d, this);
} else {
out("function($O$){return $O$.", names.name(d), ";}");
}
}
} else {
final boolean isDynamic = that.getPrimary() instanceof Tree.Dynamic;
String lhs = generateToString(new GenerateCallback() {
@Override
public void generateValue() {
if (isDynamic) {
out("(");
}
GenerateJsVisitor.super.visit(that);
if (isDynamic) {
out(")");
}
}
});
if (d != null && d.isStatic()) {
BmeGenerator.generateStaticReference(that, d, this);
} else {
out(memberAccess(that, lhs));
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Constructor in project ceylon by eclipse.
the class GenerateJsVisitor method stitchNative.
/**
* Reads a file with hand-written snippet and outputs it to the current writer.
*/
boolean stitchNative(final Declaration d, final Tree.Declaration n) {
final VirtualFile f = compiler.getStitchedFile(d, ".js");
if (f != null && f.exists()) {
if (compiler.isCompilingLanguageModule()) {
jsout.outputFile(f);
}
if (d.isClassOrInterfaceMember()) {
if (d instanceof Value || n instanceof Tree.Constructor) {
// Constructor metamodel is done in TypeGenerator.classConstructor
return true;
}
out(names.self((TypeDeclaration) d.getContainer()), ".");
} else if (n instanceof Tree.AttributeDeclaration || n instanceof Tree.AttributeGetterDefinition) {
return true;
}
out(names.name(d), ".$crtmm$=");
TypeUtils.encodeForRuntime(n, d, n.getAnnotationList(), this);
endLine(true);
return true;
} else {
if (!(d instanceof ClassOrInterface || n instanceof Tree.MethodDefinition || (n instanceof Tree.MethodDeclaration && ((Tree.MethodDeclaration) n).getSpecifierExpression() != null) || n instanceof Tree.AttributeGetterDefinition || (n instanceof Tree.AttributeDeclaration && ((Tree.AttributeDeclaration) n).getSpecifierOrInitializerExpression() != null))) {
String missingDeclarationName = d.getName();
if (missingDeclarationName == null && d instanceof Constructor) {
missingDeclarationName = "default constructor";
} else {
missingDeclarationName = "'" + missingDeclarationName + "'";
}
final String err = "no native implementation for backend: native " + missingDeclarationName + " is not implemented for the 'js' backend";
n.addError(err, Backend.JavaScript);
out("/*", err, "*/");
}
return false;
}
}
use of org.eclipse.ceylon.model.typechecker.model.Constructor in project ceylon by eclipse.
the class GenerateJsVisitor method visit.
public void visit(final Tree.ExtendedTypeExpression that) {
if (errVisitor.hasErrors(that))
return;
final Declaration d = that.getDeclaration();
if (d instanceof Constructor) {
qualify(that, (Declaration) d.getContainer());
out(names.name((Declaration) d.getContainer()), names.constructorSeparator(d));
} else {
qualify(that, d);
}
out(names.name(d));
}
Aggregations