use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class FunctionalUtil method getParameters.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Sequential<FunctionOrValueDeclaration> getParameters(Functional declaration) {
ParameterList parameterList = ((Functional) declaration).getFirstParameterList();
if (parameterList == null)
return (Sequential) empty_.get_();
List<Parameter> modelParameters = parameterList.getParameters();
ceylon.language.meta.declaration.FunctionOrValueDeclaration[] parameters = new ceylon.language.meta.declaration.FunctionOrValueDeclaration[modelParameters.size()];
int i = 0;
for (Parameter modelParameter : modelParameters) {
parameters[i] = (ceylon.language.meta.declaration.FunctionOrValueDeclaration) Metamodel.getOrCreateMetamodel(modelParameter.getModel());
i++;
}
return Util.sequentialWrapper(ceylon.language.meta.declaration.FunctionOrValueDeclaration.$TypeDescriptor$, parameters);
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class Metamodel method getJavaInstantiators.
public static List<java.lang.reflect.Method> getJavaInstantiators(org.eclipse.ceylon.model.typechecker.model.Constructor declaration) {
org.eclipse.ceylon.model.typechecker.model.Class classModel = (org.eclipse.ceylon.model.typechecker.model.Class) declaration.getContainer();
// Class<?> javaClass = getJavaClass(classModel);
Class<?> outerJavaClass = getJavaClass((Declaration) classModel.getContainer());
// java.lang.reflect.Method[] ctors = outerJavaClass.getDeclaredMethods();
ArrayList<java.lang.reflect.Method> result = new ArrayList<java.lang.reflect.Method>();
// find the appropriate ultimate constructor
String methodName = classModel.getName() + "$new$";
java.lang.reflect.Method ultimate = getJavaInstantiator(declaration, methodName);
result.add(ultimate);
List<Parameter> parameters = declaration.getFirstParameterList().getParameters();
Class<?>[] javapl = ultimate.getParameterTypes();
// according to the parameter list
for (int ii = parameters.size() - 1, jj = javapl.length; ii >= 0; ii--, jj--) {
Parameter p = parameters.get(ii);
if (p.isDefaulted() || (p.isSequenced() && !p.isAtLeastOne())) {
Class<?>[] sig = Arrays.copyOfRange(javapl, 0, jj - 1);
try {
java.lang.reflect.Method overloaded = outerJavaClass.getDeclaredMethod(methodName, sig);
result.add(overloaded);
} catch (NoSuchMethodException e) {
throw Metamodel.newModelError("Could not find overloaded constructor with signature " + Arrays.toString(sig), e);
}
}
}
return result;
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class GenerateJsVisitor method initDefaultedParameters.
/**
* Create special functions with the expressions for defaulted parameters in a parameter list.
*/
void initDefaultedParameters(final Tree.ParameterList params, Tree.AnyMethod container) {
if (!(container instanceof Tree.MethodDeclaration || container.getDeclarationModel().isMember())) {
return;
}
final boolean isMember = container.getDeclarationModel().isMember();
for (final Tree.Parameter param : params.getParameters()) {
Parameter pd = param.getParameterModel();
if (pd.isDefaulted()) {
final SpecifierOrInitializerExpression expr = getDefaultExpression(param);
if (expr == null) {
continue;
}
if (isMember) {
qualify(params, container.getDeclarationModel());
out(names.name(container.getDeclarationModel()), "$defs$", pd.getName(), "=function");
} else {
out("function ", names.name(container.getDeclarationModel()), "$defs$", pd.getName());
}
params.visit(this);
out("{");
initSelf(expr);
out("return ");
if (param instanceof Tree.ParameterDeclaration) {
Tree.TypedDeclaration node = ((Tree.ParameterDeclaration) param).getTypedDeclaration();
if (node instanceof Tree.MethodDeclaration) {
// function parameter defaulted using "=>"
FunctionHelper.singleExprFunction(((Tree.MethodDeclaration) node).getParameterLists(), expr.getExpression(), null, true, true, this);
} else if (!isNaturalLiteral(expr.getExpression().getTerm())) {
expr.visit(this);
}
} else if (!isNaturalLiteral(expr.getExpression().getTerm())) {
expr.visit(this);
}
out(";}");
endLine(true);
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class GenerateJsVisitor method classDeclaration.
private void classDeclaration(final Tree.ClassDeclaration that) {
// Don't even bother with nodes that have errors
if (errVisitor.hasErrors(that))
return;
comment(that);
final Class d = that.getDeclarationModel();
final String aname = names.name(d);
final Tree.ClassSpecifier ext = that.getClassSpecifier();
out(function, aname, "(");
// Generate each parameter because we need to append one at the end
for (Tree.Parameter p : that.getParameterList().getParameters()) {
p.visit(this);
out(", ");
}
if (d.getTypeParameters() != null && !d.getTypeParameters().isEmpty()) {
out("$$targs$$,");
}
out(names.self(d), "){");
initSelf(that);
initParameters(that.getParameterList(), d, null);
out("return ");
TypeDeclaration aliased = ext.getType().getDeclarationModel();
final String aliasedName;
aliasedName = names.name(aliased);
qualify(that, aliased);
Scope superscope = getSuperMemberScope(ext.getType());
if (superscope != null) {
out("getT$all()['");
out(superscope.getQualifiedNameString());
out("'].$$.prototype.", aliasedName, ".call(", names.self(prototypeOwner), ",");
} else {
out(aliasedName, "(");
}
Tree.PositionalArgumentList posArgs = ext.getInvocationExpression().getPositionalArgumentList();
if (posArgs != null) {
posArgs.visit(this);
if (!posArgs.getPositionalArguments().isEmpty()) {
out(",");
}
} else {
out("/*PENDIENTE NAMED ARG CLASS DECL */");
}
Map<TypeParameter, Type> invargs = ext.getType().getTypeModel().getTypeArguments();
if (invargs != null && !invargs.isEmpty()) {
TypeUtils.printTypeArguments(that, invargs, this, true, ext.getType().getTypeModel().getVarianceOverrides());
out(",");
}
out(names.self(d), ");}");
endLine();
out(aname, ".$$=");
qualify(that, aliased);
out(aliasedName, ".$$");
endLine(true);
out(aname, ".$crtmm$=");
TypeUtils.encodeForRuntime(that, d, this);
endLine(true);
share(d);
if (aliased instanceof Class && ((Class) aliased).hasConstructors() || aliased instanceof Constructor) {
Class ac = aliased instanceof Constructor ? (Class) ((Constructor) aliased).getContainer() : (Class) aliased;
for (Declaration cm : ac.getMembers()) {
if (cm instanceof Constructor && cm != ac.getDefaultConstructor() && cm.isShared()) {
Constructor cons = (Constructor) cm;
final String constructorName = aname + names.constructorSeparator(cons) + names.name(cons);
out("function ", constructorName, "(");
List<Parameter> parameters = cons.getFirstParameterList().getParameters();
ArrayList<String> pnames = new ArrayList<>(parameters.size() + 1);
boolean first = true;
for (int i = 0; i < parameters.size(); i++) {
final String pname = names.createTempVariable();
pnames.add(pname);
if (first) {
first = false;
} else {
out(",");
}
out(pname);
}
out("){return ");
qualify(that, cons);
out(names.name(cons), "(");
first = true;
for (String pname : pnames) {
if (first) {
first = false;
} else {
out(",");
}
out(pname);
}
out(");}");
if (ac.isShared()) {
sharePrefix(ac, true);
out(constructorName, "=", constructorName, ";");
endLine();
}
}
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class GenerateJsVisitor method addToPrototype.
private void addToPrototype(ClassOrInterface d, final Tree.Statement s, List<Parameter> params, InitDeferrer initDeferrer) {
ClassOrInterface oldPrototypeOwner = prototypeOwner;
prototypeOwner = d;
if (s instanceof Tree.MethodDefinition) {
addMethodToPrototype(d, (Tree.MethodDefinition) s);
} else if (s instanceof Tree.MethodDeclaration) {
// Don't even bother with nodes that have errors
if (errVisitor.hasErrors(s))
return;
FunctionHelper.methodDeclaration(d, (Tree.MethodDeclaration) s, this, verboseStitcher);
} else if (s instanceof Tree.AttributeGetterDefinition) {
addGetterToPrototype(d, (Tree.AttributeGetterDefinition) s);
} else if (s instanceof Tree.AttributeDeclaration) {
AttributeGenerator.addGetterAndSetterToPrototype(d, (Tree.AttributeDeclaration) s, this, verboseStitcher);
} else if (s instanceof Tree.ClassDefinition) {
addClassToPrototype(d, (Tree.ClassDefinition) s, initDeferrer);
} else if (s instanceof Tree.InterfaceDefinition) {
addInterfaceToPrototype(d, (Tree.InterfaceDefinition) s, initDeferrer);
} else if (s instanceof Tree.ObjectDefinition) {
addObjectToPrototype(d, (Tree.ObjectDefinition) s, initDeferrer);
} else if (s instanceof Tree.ClassDeclaration) {
addClassDeclarationToPrototype(d, (Tree.ClassDeclaration) s);
} else if (s instanceof Tree.InterfaceDeclaration) {
addInterfaceDeclarationToPrototype(d, (Tree.InterfaceDeclaration) s);
} else if (s instanceof Tree.SpecifierStatement) {
addSpecifierToPrototype(d, (Tree.SpecifierStatement) s);
} else if (s instanceof Tree.TypeAliasDeclaration) {
addAliasDeclarationToPrototype(d, (Tree.TypeAliasDeclaration) s);
}
// This fixes #231 for prototype style
if (params != null && s instanceof Tree.Declaration) {
Declaration m = ((Tree.Declaration) s).getDeclarationModel();
for (Iterator<Parameter> iter = params.iterator(); iter.hasNext(); ) {
Parameter _p = iter.next();
if (m.getName() != null && m.getName().equals(_p.getName())) {
iter.remove();
break;
}
}
}
prototypeOwner = oldPrototypeOwner;
}
Aggregations