use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class TypeDescriptor method flattenUnionOrIntersection.
/**
* Remove {@link Union}s or {@link Intersection}s from the given {@code members}, flattening
* the members of those Unions/Intersections found
* (plus the Union/Intersection members of <em>those</em>, etc) into the
* returned array:
* <pre>
* A&(B&(C&D)) => A&B&C&D
* A|(B|(C|D)) => A|B|C|D
* </pre>
* @param members The members of the union or intersection.
* @param union true to flatten Union within the members, false to flatten intersections.
*/
private static TypeDescriptor[] flattenUnionOrIntersection(TypeDescriptor[] members, boolean union) {
TypeDescriptor[] iterating = members;
for (int ii = 0; ii < iterating.length; ii++) {
TypeDescriptor td = iterating[ii];
if (td instanceof Union && union || td instanceof Intersection && !union) {
TypeDescriptor[] extra = ((Composite) td).members;
TypeDescriptor[] n = new TypeDescriptor[iterating.length - 1 + extra.length];
System.arraycopy(iterating, 0, n, 0, ii);
System.arraycopy(extra, 0, n, ii, extra.length);
if (ii + 1 < iterating.length) {
System.arraycopy(iterating, ii + 1, n, ii + extra.length, iterating.length - ii - 1);
}
iterating = n;
}
}
return iterating;
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class FunctionDeclarationImpl method getAppliedMethod.
<Container, Type, Arguments extends ceylon.language.Sequential<? extends Object>> ceylon.language.meta.model.Method<Container, Type, Arguments> getAppliedMethod(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Ignore TypeDescriptor $reifiedArguments, Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments, ceylon.language.meta.model.Type<? extends Object> container) {
List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
org.eclipse.ceylon.model.typechecker.model.Type containerType = Metamodel.getModel(container);
Metamodel.checkQualifyingType(containerType, declaration);
Metamodel.checkTypeArguments(containerType, declaration, producedTypes);
// find the proper qualifying type
org.eclipse.ceylon.model.typechecker.model.Type memberQualifyingType = containerType.getSupertype((TypeDeclaration) declaration.getContainer());
final TypedReference appliedFunction = ((org.eclipse.ceylon.model.typechecker.model.TypedDeclaration) declaration).appliedTypedReference(memberQualifyingType, producedTypes);
TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForFunction(appliedFunction);
TypeDescriptor reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction);
TypeDescriptor reifiedContainer = Metamodel.getTypeDescriptorForProducedType(containerType);
if (getStatic()) {
producedTypes.addAll(0, containerType.getTypeArgumentList());
}
Metamodel.checkReifiedTypeArgument("memberApply", "Method<$1,$2,$3>", Variance.IN, containerType, $reifiedContainer, Variance.OUT, appliedFunction.getType(), $reifiedType, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction), $reifiedArguments);
return new MethodImpl<Container, Type, Arguments>(reifiedContainer, reifiedType, reifiedArguments, appliedFunction, this, container);
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class Metamodel method apply.
public static <Return> Return apply(Callable<? extends Return> function, Sequential<?> arguments, List<Type> parameterProducedTypes, int firstDefaulted, int variadicIndex) {
int argumentCount = Util.toInt(arguments.getSize());
int parameters = parameterProducedTypes.size();
// check minimum
if (firstDefaulted == -1) {
if (argumentCount < parameters)
throw new InvocationException("Not enough arguments to function. Expected " + parameters + " but got only " + argumentCount);
} else if (argumentCount < firstDefaulted)
throw new InvocationException("Not enough arguments to function. Expected at least " + firstDefaulted + " but got only " + argumentCount);
// check maximum
if (variadicIndex == -1) {
if (argumentCount > parameters)
throw new InvocationException("To many arguments to function. Expected at most " + parameters + " but got " + argumentCount);
}
// if we're variadic we accept any number
// now check their types
Iterator<?> it = arguments.iterator();
Object arg;
int i = 0;
Type variadicElement = null;
if (variadicIndex != -1)
// it must be a Sequential<T>
variadicElement = parameterProducedTypes.get(variadicIndex).getTypeArgumentList().get(0);
while ((arg = it.next()) != finished_.get_()) {
Type parameterType = variadicIndex == -1 || i < variadicIndex ? // normal param
parameterProducedTypes.get(i) : // variadic param
variadicElement;
Type argumentType = Metamodel.getProducedType(arg);
if (!argumentType.isSubtypeOf(parameterType))
throw new IncompatibleTypeException("Invalid argument " + i + ", expected type " + parameterType + " but got " + argumentType);
i++;
}
// they are all good, let's call it
TypeDescriptor variadicElementType = variadicElement != null ? Metamodel.getTypeDescriptorForProducedType(variadicElement) : null;
return Util.apply(function, arguments, variadicElementType);
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class Metamodel method getMetamodelSequential.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Sequential<? extends ceylon.language.meta.declaration.OpenType> getMetamodelSequential(List<Type> types) {
if (types.isEmpty())
return (Sequential<? extends ceylon.language.meta.declaration.OpenType>) (Sequential) empty_.get_();
ceylon.language.meta.declaration.OpenType[] ret = new ceylon.language.meta.declaration.OpenType[types.size()];
int i = 0;
TypeDescriptor td = TypeDescriptor.NothingType;
for (Type pt : types) {
OpenType mm = Metamodel.getMetamodel(pt);
td = TypeDescriptor.union(((ReifiedType) mm).$getType$());
ret[i++] = mm;
}
return Util.sequentialWrapper(td, ret);
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class Wrappings method elementMapping.
static <Java, Ceylon> Wrapping<Java, Ceylon> elementMapping(TypeDescriptor $reified$Element) {
boolean allowNull = false;
if ($reified$Element.containsNull()) {
allowNull = true;
if ($reified$Element instanceof TypeDescriptor.Union && ((TypeDescriptor.Union) $reified$Element).getMembers().length == 2) {
Union union = (TypeDescriptor.Union) $reified$Element;
if (union.getMembers()[0].containsNull()) {
$reified$Element = union.getMembers()[1];
} else if (union.getMembers()[1].containsNull()) {
$reified$Element = union.getMembers()[0];
}
}
}
Wrapping<Java, Ceylon> elementWrapping;
if ($reified$Element == ceylon.language.Integer.$TypeDescriptor$) {
elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_INTEGER_OR_NULL : TO_CEYLON_INTEGER);
} else if ($reified$Element == ceylon.language.Float.$TypeDescriptor$) {
elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_FLOAT_OR_NULL : TO_CEYLON_FLOAT);
} else if ($reified$Element == ceylon.language.Byte.$TypeDescriptor$) {
elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_BYTE_OR_NULL : TO_CEYLON_BYTE);
} else if ($reified$Element == ceylon.language.Boolean.$TypeDescriptor$) {
elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_BOOLEAN_OR_NULL : TO_CEYLON_BOOLEAN);
} else if ($reified$Element == ceylon.language.Character.$TypeDescriptor$) {
elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_CHARACTER_OR_NULL : TO_CEYLON_CHARACTER);
} else if ($reified$Element == ceylon.language.String.$TypeDescriptor$) {
elementWrapping = (Wrapping) (allowNull ? TO_CEYLON_STRING_OR_NULL : TO_CEYLON_STRING);
} else if ($reified$Element instanceof TypeDescriptor.Class) {
TypeDescriptor.Class classDescriptor = (TypeDescriptor.Class) $reified$Element;
if (classDescriptor.getKlass() == ceylon.language.List.class) {
elementWrapping = (Wrapping) toCeylonList(classDescriptor.getTypeArgument(0), allowNull);
} else if (classDescriptor.getKlass() == ceylon.language.Set.class) {
elementWrapping = (Wrapping) toCeylonSet(classDescriptor.getTypeArgument(0), allowNull);
} else if (classDescriptor.getKlass() == ceylon.language.Map.class) {
elementWrapping = (Wrapping) toCeylonMap(classDescriptor.getTypeArgument(0), classDescriptor.getTypeArgument(1), allowNull);
} else {
elementWrapping = allowNull ? MAYBE_IDENTITY : DEFINITE_IDENTITY;
}
} else {
elementWrapping = allowNull ? MAYBE_IDENTITY : DEFINITE_IDENTITY;
}
return elementWrapping;
}
Aggregations