use of org.eclipse.ceylon.model.typechecker.model.Value in project ceylon by eclipse.
the class GenerateJsVisitor method visit.
@Override
public void visit(final Tree.AttributeGetterDefinition that) {
if (errVisitor.hasErrors(that) || !TypeUtils.acceptNative(that))
return;
Value d = that.getDeclarationModel();
if (opts.isOptimize() && d.isClassOrInterfaceMember())
return;
comment(that);
if (AttributeGenerator.defineAsProperty(d)) {
defineAttribute(names.self((TypeDeclaration) d.getContainer()), names.name(d));
AttributeGenerator.getter(that, this, true);
final AttributeSetterDefinition setterDef = associatedSetterDefinition(d);
if (setterDef == null) {
out(",undefined");
} else {
out(",function(", names.name(setterDef.getDeclarationModel().getParameter()), ")");
AttributeGenerator.setter(setterDef, this);
}
out(",");
TypeUtils.encodeForRuntime(that, that.getDeclarationModel(), that.getAnnotationList(), this);
if (setterDef != null) {
out(",");
TypeUtils.encodeForRuntime(setterDef, setterDef.getDeclarationModel(), setterDef.getAnnotationList(), this);
}
out(");");
} else {
out(function, names.getter(d, false), "()");
if (TypeUtils.isNativeExternal(d)) {
out("{");
if (stitchNative(d, that)) {
if (verboseStitcher) {
spitOut("Stitching in native attribute " + d.getQualifiedNameString() + ", ignoring Ceylon declaration");
}
} else {
AttributeGenerator.getter(that, this, false);
}
out("}");
} else {
AttributeGenerator.getter(that, this, true);
}
endLine();
out(names.getter(d, false), ".$crtmm$=");
TypeUtils.encodeForRuntime(that, d, this);
if (!shareGetter(d)) {
out(";");
}
AttributeGenerator.generateAttributeMetamodel(that, true, false, this);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Value in project ceylon by eclipse.
the class GenerateJsVisitor method specifierStatement.
private void specifierStatement(final TypeDeclaration outer, final Tree.SpecifierStatement specStmt) {
final Tree.Expression expr = specStmt.getSpecifierExpression().getExpression();
final Tree.Term term = specStmt.getBaseMemberExpression();
final Tree.StaticMemberOrTypeExpression smte = term instanceof Tree.StaticMemberOrTypeExpression ? (Tree.StaticMemberOrTypeExpression) term : null;
if (isInDynamicBlock() && ModelUtil.isTypeUnknown(term.getTypeModel())) {
if (smte != null && smte.getDeclaration() == null) {
out(smte.getIdentifier().getText());
} else {
term.visit(this);
if (term instanceof BaseMemberExpression) {
Declaration dec = ((BaseMemberExpression) term).getDeclaration();
if (dec instanceof Value) {
Value v = (Value) dec;
if (v.isMember()) {
// Assignment to dynamic member
out("_");
}
}
}
}
out("=");
int box = boxUnboxStart(expr, term);
expr.visit(this);
if (box == 4)
out("/*TODO: callable targs 6.1*/");
boxUnboxEnd(box);
out(";");
return;
}
if (smte != null) {
final Declaration bmeDecl = smte.getDeclaration();
if (specStmt.getSpecifierExpression() instanceof LazySpecifierExpression) {
// attr => expr;
final boolean property = AttributeGenerator.defineAsProperty(bmeDecl);
if (property) {
defineAttribute(qualifiedPath(specStmt, bmeDecl), names.name(bmeDecl));
} else {
if (bmeDecl.isMember()) {
qualify(specStmt, bmeDecl);
} else {
out("var ");
}
out(names.getter(bmeDecl, false), "=function()");
}
beginBlock();
if (outer != null) {
initSelf(specStmt);
}
out("return ");
if (!isNaturalLiteral(specStmt.getSpecifierExpression().getExpression().getTerm())) {
specStmt.getSpecifierExpression().visit(this);
}
out(";");
endBlock();
if (property) {
out(",undefined,");
TypeUtils.encodeForRuntime(specStmt, bmeDecl, this);
out(")");
}
endLine(true);
directAccess.remove(bmeDecl);
} else if (outer != null) {
// since #451 we now generate an attribute here
if (outer instanceof Constructor || bmeDecl.isMember() && bmeDecl instanceof Value && bmeDecl.isActual()) {
assignment(outer, bmeDecl, expr);
}
} else if (bmeDecl instanceof FunctionOrValue) {
// "attr = expr;" in an initializer or method
final FunctionOrValue moval = (FunctionOrValue) bmeDecl;
if (moval.isVariable() || moval.isLate()) {
// simple assignment to a variable attribute
BmeGenerator.generateMemberAccess(smte, new GenerateCallback() {
@Override
public void generateValue() {
int boxType = boxUnboxStart(expr.getTerm(), moval);
if (isInDynamicBlock() && !ModelUtil.isTypeUnknown(moval.getType()) && ModelUtil.isTypeUnknown(expr.getTypeModel())) {
TypeUtils.generateDynamicCheck(expr, moval.getType(), GenerateJsVisitor.this, false, expr.getTypeModel().getTypeArguments());
} else {
expr.visit(GenerateJsVisitor.this);
}
if (boxType == 4) {
out(",");
if (moval instanceof Function) {
// Add parameters
TypeUtils.encodeParameterListForRuntime(true, specStmt, ((Function) moval).getFirstParameterList(), GenerateJsVisitor.this);
out(",");
} else {
// TODO extract parameters from Value
final Type ps = moval.getUnit().getCallableTuple(moval.getType());
if (ps == null || ps.isSubtypeOf(moval.getUnit().getEmptyType())) {
out("[],");
} else {
out("[/*VALUE Callable params ", ps.asString() + "*/],");
}
}
TypeUtils.printTypeArguments(expr, expr.getTypeModel().getTypeArguments(), GenerateJsVisitor.this, false, expr.getTypeModel().getVarianceOverrides());
}
boxUnboxEnd(boxType);
}
}, qualifiedPath(smte, moval), this);
out(";");
} else if (moval.isMember()) {
if (moval instanceof Function) {
// same as fat arrow
qualify(specStmt, bmeDecl);
if (expr.getTerm() instanceof Tree.FunctionArgument) {
((Tree.FunctionArgument) expr.getTerm()).getDeclarationModel().setRefinedDeclaration(moval);
out(names.name(moval), "=");
specStmt.getSpecifierExpression().visit(this);
out(";");
} else {
out(names.name(moval), "=function ", names.name(moval), "(");
// Build the parameter list, we'll use it several times
final StringBuilder paramNames = new StringBuilder();
final List<Parameter> params = ((Function) moval).getFirstParameterList().getParameters();
for (Parameter p : params) {
if (paramNames.length() > 0)
paramNames.append(",");
paramNames.append(names.name(p));
}
out(paramNames.toString());
out("){");
for (Parameter p : params) {
if (p.isDefaulted()) {
out("if(", names.name(p), "===undefined)", names.name(p), "=");
qualify(specStmt, moval);
out(names.name(moval), "$defs$", p.getName(), "(", paramNames.toString(), ")");
endLine(true);
}
}
out("return ");
if (!isNaturalLiteral(specStmt.getSpecifierExpression().getExpression().getTerm())) {
specStmt.getSpecifierExpression().visit(this);
}
out("(", paramNames.toString(), ");}");
endLine(true);
}
} else {
// declaration itself can be omitted), so generate the attribute.
if (opts.isOptimize()) {
// #451
out(names.self(ModelUtil.getContainingClassOrInterface(moval.getScope())), ".", names.valueName(moval), "=");
specStmt.getSpecifierExpression().visit(this);
endLine(true);
} else {
AttributeGenerator.generateAttributeGetter(null, moval, specStmt.getSpecifierExpression(), null, this, directAccess, verboseStitcher);
}
}
} else {
// Specifier for some other attribute, or for a method.
if (opts.isOptimize() || bmeDecl.isMember() && bmeDecl instanceof Function) {
qualify(specStmt, bmeDecl);
}
out(names.name(bmeDecl), "=");
if (isInDynamicBlock() && ModelUtil.isTypeUnknown(expr.getTypeModel()) && !ModelUtil.isTypeUnknown(((FunctionOrValue) bmeDecl).getType())) {
TypeUtils.generateDynamicCheck(expr, ((FunctionOrValue) bmeDecl).getType(), this, false, expr.getTypeModel().getTypeArguments());
} else {
if (expr.getTerm() instanceof Tree.FunctionArgument) {
Function fun = ((Tree.FunctionArgument) expr.getTerm()).getDeclarationModel();
if (fun.isAnonymous()) {
fun.setRefinedDeclaration(moval);
}
}
specStmt.getSpecifierExpression().visit(this);
}
out(";");
}
}
} else if ((term instanceof Tree.ParameterizedExpression) && (specStmt.getSpecifierExpression() != null)) {
final Tree.ParameterizedExpression paramExpr = (Tree.ParameterizedExpression) term;
if (paramExpr.getPrimary() instanceof BaseMemberExpression) {
// func(params) => expr;
final BaseMemberExpression bme2 = (BaseMemberExpression) paramExpr.getPrimary();
final Declaration bmeDecl = bme2.getDeclaration();
if (bmeDecl.isMember()) {
qualify(specStmt, bmeDecl);
} else {
out("var ");
}
out(names.name(bmeDecl), "=");
FunctionHelper.singleExprFunction(paramExpr.getParameterLists(), expr, bmeDecl instanceof Scope ? (Scope) bmeDecl : null, true, true, this);
out(";");
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Value in project ceylon by eclipse.
the class GenerateJsVisitor method outputCapturedValues.
void outputCapturedValues(Set<Value> caps) {
if (caps == null || caps.isEmpty()) {
return;
}
boolean first = true;
for (Value v : caps) {
if (first) {
out("var ");
} else {
out(",");
}
first = false;
final String cn = names.createTempVariable();
out(cn, "=", names.name(v));
names.forceName(v, cn);
}
out(";");
}
use of org.eclipse.ceylon.model.typechecker.model.Value in project ceylon by eclipse.
the class GenerateJsVisitor method addGetterToPrototype.
private void addGetterToPrototype(TypeDeclaration outer, final Tree.AttributeGetterDefinition that) {
Value d = that.getDeclarationModel();
if (!opts.isOptimize() || !d.isClassOrInterfaceMember())
return;
comment(that);
defineAttribute(d.isStatic() ? names.name(outer) + ".$st$" : names.self(outer), names.name(d));
if (TypeUtils.isNativeExternal(d)) {
out("{");
if (stitchNative(d, that)) {
if (verboseStitcher) {
spitOut("Stitching in native getter " + d.getQualifiedNameString() + ", ignoring Ceylon declaration");
}
} else {
AttributeGenerator.getter(that, this, false);
}
out("}");
} else {
AttributeGenerator.getter(that, this, true);
}
final AttributeSetterDefinition setterDef = associatedSetterDefinition(d);
if (setterDef == null) {
out(",undefined");
} else {
out(",function(", names.name(setterDef.getDeclarationModel().getParameter()), ")");
AttributeGenerator.setter(setterDef, this);
}
out(",");
TypeUtils.encodeForRuntime(that, that.getDeclarationModel(), that.getAnnotationList(), this);
if (setterDef != null) {
out(",");
TypeUtils.encodeForRuntime(setterDef, setterDef.getDeclarationModel(), setterDef.getAnnotationList(), this);
}
out(");");
}
use of org.eclipse.ceylon.model.typechecker.model.Value in project ceylon by eclipse.
the class MetamodelHelper method generateOpenType.
static void generateOpenType(final Tree.MetaLiteral that, final Declaration d, final GenerateJsVisitor gen, boolean compilingLanguageModule) {
final Module m = d.getUnit().getPackage().getModule();
final boolean isConstructor = ModelUtil.isConstructor(d) || that instanceof Tree.NewLiteral;
if (d instanceof TypeParameter == false) {
if (compilingLanguageModule) {
gen.out("$init$");
} else {
gen.out(gen.getClAlias());
}
}
if (d instanceof org.eclipse.ceylon.model.typechecker.model.Interface) {
gen.out("OpenInterface$jsint");
} else if (isConstructor) {
if (TypeUtils.getConstructor(d).isValueConstructor()) {
gen.out("OpenValueConstructor$jsint");
} else {
gen.out("OpenCallableConstructor$jsint");
}
} else if (d instanceof Class) {
gen.out("openClass$jsint");
} else if (d instanceof Function) {
gen.out("OpenFunction$jsint");
} else if (d instanceof Value) {
gen.out("OpenValue$jsint");
} else if (d instanceof org.eclipse.ceylon.model.typechecker.model.IntersectionType) {
gen.out("OpenIntersection");
} else if (d instanceof org.eclipse.ceylon.model.typechecker.model.UnionType) {
gen.out("OpenUnion");
} else if (d instanceof TypeParameter) {
generateOpenType(that, ((TypeParameter) d).getDeclaration(), gen, compilingLanguageModule);
gen.out(".getTypeParameterDeclaration('", d.getName(), "')");
return;
} else if (d instanceof org.eclipse.ceylon.model.typechecker.model.NothingType) {
gen.out("NothingType");
} else if (d instanceof TypeAlias) {
gen.out("OpenAlias$jsint(");
if (compilingLanguageModule) {
gen.out(")(");
}
if (d.isMember()) {
// Make the chain to the top-level container
ArrayList<Declaration> parents = new ArrayList<>(2);
Declaration pd = (Declaration) d.getContainer();
while (pd != null) {
parents.add(0, pd);
pd = pd.isMember() ? (Declaration) pd.getContainer() : null;
}
for (Declaration _d : parents) {
gen.out(gen.getNames().name(_d), ".$$.prototype.");
}
}
gen.out(gen.getNames().name(d), ")");
return;
}
// TODO optimize for local declarations
if (compilingLanguageModule) {
gen.out("()");
}
gen.out("(", gen.getClAlias());
final String pkgname = d.getUnit().getPackage().getNameAsString();
if (Objects.equals(that.getUnit().getPackage().getModule(), d.getUnit().getPackage().getModule())) {
gen.out("lmp$(ex$,'");
} else {
// TODO use $ for language module as well
gen.out("fmp$('", m.getNameAsString(), "','", m.getVersion(), "','");
}
gen.out("ceylon.language".equals(pkgname) ? "$" : pkgname, "'),");
if (d.isMember() || isConstructor) {
if (isConstructor) {
final Class actualClass;
final String constrName;
if (d instanceof Class) {
actualClass = (Class) d;
constrName = "$c$";
} else {
actualClass = (Class) d.getContainer();
if (d instanceof Constructor && ((Constructor) d).isValueConstructor()) {
constrName = gen.getNames().name(actualClass.getDirectMember(d.getName(), null, false));
} else {
constrName = gen.getNames().name(d);
}
}
if (gen.isImported(that.getUnit().getPackage(), actualClass)) {
gen.out(gen.getNames().moduleAlias(actualClass.getUnit().getPackage().getModule()), ".");
}
if (actualClass.isMember()) {
outputPathToDeclaration(that, actualClass, gen);
}
gen.out(gen.getNames().name(actualClass), gen.getNames().constructorSeparator(d), constrName, ")");
return;
} else {
outputPathToDeclaration(that, d, gen);
}
}
if (d instanceof Value || d.isParameter()) {
if (!d.isMember())
gen.qualify(that, d);
if (d.isStatic() && d instanceof Value && ((Value) d).getType().getDeclaration().isAnonymous()) {
gen.out(gen.getNames().name(d), ")");
} else {
gen.out(gen.getNames().getter(d, true), ")");
}
} else {
if (d.isAnonymous()) {
final String oname = gen.getNames().objectName(d);
if (d.isToplevel()) {
gen.qualify(that, d);
}
gen.out("$init$", oname);
if (!d.isToplevel()) {
gen.out("()");
}
} else {
if (!d.isMember())
gen.qualify(that, d);
gen.out(gen.getNames().name(d));
}
gen.out(")");
}
}
Aggregations