use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class ExpressionVisitor method createAnonymousFunctionParameters.
/**
* Create the parameter list of the given
* anonymous function with a missing argument
* list, from the given parameter list of a
* functional parameter to which it is assigned.
*/
private void createAnonymousFunctionParameters(List<Parameter> parameters, Tree.Term term, Tree.FunctionArgument anon) {
Function model = anon.getDeclarationModel();
if (anon.getParameterLists().isEmpty()) {
Tree.ParameterList pl = new Tree.ParameterList(null);
ParameterList mpl = new ParameterList();
for (Parameter parameter : parameters) {
Tree.InitializerParameter ip = new Tree.InitializerParameter(null);
CommonToken token = new CommonToken(LIDENTIFIER, parameter.getName());
token.setStartIndex(term.getStartIndex());
Token tok = term.getToken();
token.setLine(tok.getLine());
token.setCharPositionInLine(tok.getCharPositionInLine());
Tree.Identifier id = new Tree.Identifier(token);
ip.setIdentifier(id);
pl.addParameter(ip);
ip.setUnit(unit);
ip.setScope(model);
id.setUnit(unit);
id.setScope(model);
Parameter mp = new Parameter();
mp.setDeclaration(model);
mp.setName(parameter.getName());
ip.setParameterModel(mp);
mpl.getParameters().add(mp);
pl.setModel(mpl);
}
pl.setUnit(unit);
pl.setScope(model);
model.addParameterList(mpl);
anon.addParameterList(pl);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class TreeUtil method buildAnnotations.
public static void buildAnnotations(Tree.AnnotationList al, List<Annotation> annotations) {
if (al != null) {
Tree.AnonymousAnnotation aa = al.getAnonymousAnnotation();
if (aa != null) {
Annotation ann = new Annotation();
ann.setName("doc");
Tree.StringLiteral lit = aa.getStringLiteral();
if (lit != null) {
String text = lit.getText();
ann.addPositionalArgument(text);
annotations.add(ann);
}
}
for (Tree.Annotation a : al.getAnnotations()) {
Annotation ann = new Annotation();
Tree.BaseMemberExpression bma = (Tree.BaseMemberExpression) a.getPrimary();
String name = bma.getIdentifier().getText();
ann.setName(name);
Tree.NamedArgumentList nal = a.getNamedArgumentList();
if (nal != null) {
for (Tree.NamedArgument na : nal.getNamedArguments()) {
if (na instanceof Tree.SpecifiedArgument) {
Tree.SpecifiedArgument sa = (Tree.SpecifiedArgument) na;
Tree.SpecifierExpression sie = sa.getSpecifierExpression();
Tree.Expression e = sie.getExpression();
if (e != null) {
Tree.Term t = e.getTerm();
Parameter p = sa.getParameter();
if (p != null) {
String text = toString(t);
if (text != null) {
ann.addNamedArgument(p.getName(), text);
}
}
}
}
}
}
Tree.PositionalArgumentList pal = a.getPositionalArgumentList();
if (pal != null) {
for (Tree.PositionalArgument pa : pal.getPositionalArguments()) {
if (pa instanceof Tree.ListedArgument) {
Tree.ListedArgument la = (Tree.ListedArgument) pa;
Tree.Term t = la.getExpression().getTerm();
String text = toString(t);
if (text != null) {
ann.addPositionalArgument(text);
}
}
}
}
annotations.add(ann);
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class PrintVisitor method print.
private void print(Node node) {
print(node.getText());
print(" [" + node.getNodeType() + "]");
if (node.getToken() != null) {
print(" (" + node.getLocation() + ")");
}
if (node instanceof Tree.Term) {
Type type = ((Tree.Term) node).getTypeModel();
if (type != null) {
print(" : " + type.asString() + "");
}
}
if (node instanceof Tree.ComprehensionClause) {
Type type = ((Tree.ComprehensionClause) node).getTypeModel();
if (type != null) {
print(" : " + type.asString() + "");
}
}
if (node instanceof Tree.Type) {
Type type = ((Tree.Type) node).getTypeModel();
if (type != null) {
print(" : " + type.asString() + "");
}
}
if (node instanceof Tree.TypeArguments) {
List<Type> types = ((Tree.TypeArguments) node).getTypeModels();
if (types != null && !types.isEmpty()) {
print(" : <");
int i = 0;
for (Type pt : types) {
if (pt != null) {
print(pt.asString());
}
if (++i != types.size()) {
print(", ");
}
}
print(">");
}
}
if (node instanceof Tree.MemberOrTypeExpression) {
Reference t = ((Tree.MemberOrTypeExpression) node).getTarget();
Declaration d = ((Tree.MemberOrTypeExpression) node).getDeclaration();
if (t != null) {
print(" : " + t.asString() + "");
}
if (d != null) {
print(" : " + d);
}
}
if (node instanceof Tree.Outer) {
Declaration d = ((Tree.Outer) node).getDeclarationModel();
if (d != null) {
print(" : " + d);
}
}
if (node instanceof Tree.SelfExpression) {
Declaration d = ((Tree.SelfExpression) node).getDeclarationModel();
if (d != null) {
print(" : " + d);
}
}
if (node instanceof Tree.Declaration) {
Declaration d = ((Tree.Declaration) node).getDeclarationModel();
if (d != null) {
if (d.isCaptured() || d.isJsCaptured()) {
print("[captured]");
}
print(" : " + d);
Declaration rd = d.getRefinedDeclaration();
if (rd != null && !rd.equals(d)) {
Declaration container = (Declaration) rd.getContainer();
print(" (refines " + container.getName() + "." + rd.getName() + ")");
}
}
}
if (node instanceof Tree.SpecifierStatement) {
Declaration d = ((Tree.SpecifierStatement) node).getDeclaration();
if (d != null) {
print(" : " + d);
}
if (((Tree.SpecifierStatement) node).getRefinement()) {
Declaration rd = d.getRefinedDeclaration();
if (rd != null && !rd.equals(d)) {
Declaration container = (Declaration) rd.getContainer();
print(" (refines " + container.getName() + "." + rd.getName() + ")");
}
}
}
if (node instanceof Tree.SimpleType) {
Declaration d = ((Tree.SimpleType) node).getDeclarationModel();
if (d != null) {
print(" : " + d);
}
}
if (node instanceof Tree.ImportMemberOrType) {
Declaration d = ((Tree.ImportMemberOrType) node).getDeclarationModel();
if (d != null) {
print(" : " + d);
}
}
if (node instanceof Tree.Return) {
Declaration d = ((Tree.Return) node).getDeclaration();
if (d != null) {
print(" : " + d);
}
}
if (node instanceof Tree.PositionalArgument) {
Parameter p = ((Tree.PositionalArgument) node).getParameter();
if (p != null) {
print(" : " + p);
}
}
if (node instanceof Tree.NamedArgument) {
Parameter p = ((Tree.NamedArgument) node).getParameter();
if (p != null) {
print(" : " + p);
}
}
if (node instanceof Tree.SequencedArgument) {
Parameter p = ((Tree.SequencedArgument) node).getParameter();
if (p != null) {
print(" : " + p);
}
}
if (!node.getErrors().isEmpty()) {
String icon = " [!]";
for (Message e : node.getErrors()) {
if (!(e instanceof UsageWarning)) {
icon = " [X]";
}
}
print(icon + node.getErrors());
}
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class ValueDeclarationImpl method $getJavaAnnotations$.
@Override
@Ignore
public java.lang.annotation.Annotation[] $getJavaAnnotations$() {
Class<?> javaClass = Metamodel.getJavaClass(declaration);
ArrayList<java.lang.annotation.Annotation> result = new ArrayList<>();
HashSet<Class<? extends java.lang.annotation.Annotation>> cls = new HashSet<>();
if (javaClass != null) {
// FIXME: pretty sure this doesn't work with interop and fields
Method declaredGetter = Reflection.getDeclaredGetter(javaClass, NamingBase.getGetterName(declaration));
if (declaredGetter != null) {
addToList(result, cls, declaredGetter.getAnnotations());
}
if (!((Value) declaration).isTransient()) {
// TODO only include those which are java annotations
Field field = Reflection.getDeclaredField(javaClass, declaration.getName());
if (field != null) {
Annotation[] fieldAnnos = field.getAnnotations();
addToList(result, cls, fieldAnnos);
}
Method setter = Reflection.getDeclaredSetter(javaClass, NamingBase.getSetterName(declaration.getName()));
if (setter != null) {
Annotation[] setterAnnos = setter.getAnnotations();
addToList(result, cls, setterAnnos);
}
}
}
// one last chance
if (parameter != null && !parameter.getModel().isShared()) {
// get the annotations from the parameter itself
Annotation[][] parameterAnnotations;
Scope container = parameter.getModel().getContainer();
if (container instanceof org.eclipse.ceylon.model.typechecker.model.Function) {
parameterAnnotations = Metamodel.getJavaMethod((org.eclipse.ceylon.model.typechecker.model.Function) container).getParameterAnnotations();
} else if (container instanceof org.eclipse.ceylon.model.typechecker.model.ClassAlias) {
parameterAnnotations = Reflection.findClassAliasInstantiator(Metamodel.getJavaClass((org.eclipse.ceylon.model.typechecker.model.Class) container), (org.eclipse.ceylon.model.typechecker.model.ClassAlias) container).getParameterAnnotations();
} else if (container instanceof org.eclipse.ceylon.model.typechecker.model.Class) {
// FIXME: pretty sure that's wrong because of synthetic params. See ReflectionMethod.getParameters
parameterAnnotations = Reflection.findConstructor(Metamodel.getJavaClass((org.eclipse.ceylon.model.typechecker.model.Class) container)).getParameterAnnotations();
} else {
throw Metamodel.newModelError("Unsupported parameter container");
}
// now find the right parameter
List<Parameter> parameters = ((org.eclipse.ceylon.model.typechecker.model.Functional) container).getFirstParameterList().getParameters();
int index = parameters.indexOf(parameter);
if (index == -1)
throw Metamodel.newModelError("Parameter " + parameter + " not found in container " + parameter.getModel().getContainer());
if (index >= parameterAnnotations.length)
throw Metamodel.newModelError("Parameter " + parameter + " index is greater than JVM parameters for " + parameter.getModel().getContainer());
addToList(result, cls, parameterAnnotations[index]);
}
// nope
return result.toArray(new java.lang.annotation.Annotation[result.size()]);
}
use of org.eclipse.ceylon.model.typechecker.model.Parameter in project ceylon by eclipse.
the class Metamodel method namedApply.
public static <Return> Return namedApply(Callable<? extends Return> function, DefaultValueProvider defaultValueProvider, org.eclipse.ceylon.model.typechecker.model.Functional declaration, ceylon.language.Iterable<? extends ceylon.language.Entry<? extends ceylon.language.String, ? extends java.lang.Object>, ? extends java.lang.Object> arguments, List<Type> parameterProducedTypes) {
// FIXME: throw for Java declarations
java.util.Map<java.lang.String, java.lang.Object> argumentMap = collectArguments(arguments);
java.util.List<Parameter> parameters = declaration.getFirstParameterList().getParameters();
// store the values in an array
Array<java.lang.Object> values = new Array<java.lang.Object>(Anything.$TypeDescriptor$, parameters.size(), (java.lang.Object) null);
int parameterIndex = 0;
for (Parameter parameter : parameters) {
// get the parameter value and remove it so we can keep track of those we used
java.lang.Object value;
if (argumentMap.containsKey(parameter.getName())) {
value = argumentMap.remove(parameter.getName());
// we have a value: check the type
Type argumentType = Metamodel.getProducedType(value);
Type parameterType = parameterProducedTypes.get(parameterIndex);
if (!argumentType.isSubtypeOf(parameterType))
throw new ceylon.language.meta.model.IncompatibleTypeException("Invalid argument " + parameter.getName() + ", expected type " + parameterType + " but got " + argumentType);
} else {
// make sure it has a default value
if (!parameter.isDefaulted())
throw new InvocationException("Missing value for non-defaulted parameter " + parameter.getName());
// we need to fetch the default value
value = defaultValueProvider.getDefaultParameterValue(parameter, values, parameterIndex);
argumentMap.remove(parameter.getName());
}
values.set(parameterIndex++, value);
}
// do we have extra unknown/unused parameters left?
if (!argumentMap.isEmpty()) {
for (String name : argumentMap.keySet()) {
throw new InvocationException("No such parameter " + name);
}
}
// FIXME: don't we need to spread any variadic param?
// now do a regular invocation
Sequential<? extends Object> argumentSequence = values.sequence();
// we can trust any variadic or pseudo-variadic since we checked parameter by parameter (no spreading possible)
return Util.apply(function, argumentSequence, null);
}
Aggregations