use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class FunctionHelper method generateParameterLists.
/**
* Generates the code for single or multiple parameter lists, with a callback function to generate the function blocks.
*/
static void generateParameterLists(final Node context, final List<Tree.ParameterList> plist, final Scope scope, final ParameterListCallback callback, final boolean emitFunctionKeyword, final GenerateJsVisitor gen) {
if (plist.size() == 1) {
if (emitFunctionKeyword) {
gen.out("function");
}
Tree.ParameterList paramList = plist.get(0);
paramList.visit(gen);
callback.completeFunction();
} else {
List<MplData> metas = new ArrayList<>(plist.size());
Function m = scope instanceof Function ? (Function) scope : null;
for (Tree.ParameterList paramList : plist) {
final MplData mpl = new MplData();
metas.add(mpl);
mpl.n = context;
if (metas.size() == 1) {
if (emitFunctionKeyword) {
gen.out("function");
}
} else {
mpl.name = gen.getNames().createTempVariable();
mpl.params = paramList;
gen.out("var ", mpl.name, "=function");
}
paramList.visit(gen);
if (metas.size() == 1) {
gen.beginBlock();
gen.initSelf(context);
Scope parent = scope == null ? null : scope.getContainer();
gen.initParameters(paramList, parent instanceof TypeDeclaration ? (TypeDeclaration) parent : null, m);
} else {
gen.out("{");
}
}
callback.completeFunction();
closeMPL(metas, m.getType(), gen);
gen.endBlock();
}
}
use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class GenerateJsVisitor method visit.
@Override
public void visit(final Tree.MethodDefinition that) {
// Don't even bother with nodes that have errors
if (errVisitor.hasErrors(that) || !TypeUtils.acceptNative(that))
return;
final Function d = that.getDeclarationModel();
if (!((opts.isOptimize() && d.isClassOrInterfaceMember()) || TypeUtils.isNativeExternal(d) && compiler.isCompilingLanguageModule())) {
comment(that);
initDefaultedParameters(that.getParameterLists().get(0), that);
FunctionHelper.methodDefinition(that, this, true, verboseStitcher);
// Add reference to metamodel
out(names.name(d), ".$crtmm$=");
TypeUtils.encodeMethodForRuntime(that, this);
endLine(true);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Function in project ceylon by eclipse.
the class GenerateJsVisitor method comment.
void comment(Tree.Declaration that) {
if (!opts.isComment() || opts.isMinify())
return;
endLine();
String dname = that.getNodeType();
if (dname.endsWith("Declaration") || dname.endsWith("Definition")) {
dname = dname.substring(0, dname.length() - 7);
}
if (that instanceof Tree.Constructor) {
Function dec = ((Tree.Constructor) that).getDeclarationModel();
String cname = ((Class) dec.getContainer()).getName();
out("//Constructor ", cname, ".", dec.getName() == null ? "<default>" : dec.getName());
} else {
out("//", dname, " ", that.getDeclarationModel().getName());
}
location(that);
endLine();
}
use of org.eclipse.ceylon.model.typechecker.model.Function 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.Function in project ceylon by eclipse.
the class GenerateJsVisitor method addMethodToPrototype.
private void addMethodToPrototype(TypeDeclaration outer, final Tree.MethodDefinition that) {
// Don't even bother with nodes that have errors
if (errVisitor.hasErrors(that))
return;
Function d = that.getDeclarationModel();
if (!opts.isOptimize() || !d.isClassOrInterfaceMember())
return;
comment(that);
initDefaultedParameters(that.getParameterLists().get(0), that);
if (d.isStatic()) {
out(names.name(outer), ".$st$.", names.name(d), "=");
} else {
out(names.self(outer), ".", names.name(d), "=");
}
FunctionHelper.methodDefinition(that, this, false, verboseStitcher);
// Add reference to metamodel
if (d.isStatic()) {
out(names.name(outer), ".$st$.", names.name(d), ".$crtmm$=");
} else {
out(names.self(outer), ".", names.name(d), ".$crtmm$=");
}
TypeUtils.encodeMethodForRuntime(that, this);
endLine(true);
}
Aggregations