use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class ClassOrInterfaceDeclarationImpl method memberApply.
@SuppressWarnings("rawtypes")
@TypeInfo("ceylon.language.meta.model::Member<Container,ceylon.language.meta.model::ClassOrInterface<Type>>&ceylon.language.meta.model::ClassOrInterface<Type>")
@TypeParameters({ @TypeParameter("Container"), @TypeParameter("Type") })
@Override
public <Container, Type extends Object> java.lang.Object memberApply(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Name("containerType") ceylon.language.meta.model.Type<? extends Object> containerType, @Name("typeArguments") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
if (getToplevel())
throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a toplevel declaration to a container type: use apply");
ceylon.language.meta.model.Member<? extends Container, ceylon.language.meta.model.ClassOrInterface<?>> member = getAppliedClassOrInterface(null, null, typeArguments, containerType);
// This is all very ugly but we're trying to make it cheaper and friendlier than just checking the full type and showing
// implementation types to the user, such as AppliedMemberClass
TypeDescriptor actualReifiedContainer;
if (member instanceof MemberClassImpl)
actualReifiedContainer = ((MemberClassImpl) member).$reifiedContainer;
else
actualReifiedContainer = ((MemberInterfaceImpl) member).$reifiedContainer;
org.eclipse.ceylon.model.typechecker.model.Type actualType = Metamodel.getModel((ceylon.language.meta.model.Type<?>) member);
Metamodel.checkReifiedTypeArgument("memberApply", "Member<$1,ClassOrInterface<$2>>&ClassOrInterface<$2>", Variance.IN, Metamodel.getProducedType(actualReifiedContainer), $reifiedContainer, Variance.OUT, actualType, $reifiedType);
return member;
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class FunctionDeclarationImpl method apply.
@Override
@TypeInfo("ceylon.language.meta.model::Function<Return,Arguments>")
@TypeParameters({ @TypeParameter("Return"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
public <Return extends Object, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Function<Return, Arguments> apply(@Ignore TypeDescriptor $reifiedReturn, @Ignore TypeDescriptor $reifiedArguments, @Name("typeArguments") @TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Type<ceylon.language::Anything>>") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
if (!getToplevel()) {
String msg;
if (getStatic()) {
msg = "Cannot apply a static declaration with no container type: use staticApply";
} else {
msg = "Cannot apply a member declaration with no container type: use memberApply";
}
throw new ceylon.language.meta.model.TypeApplicationException(msg);
}
List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
Metamodel.checkTypeArguments(null, declaration, producedTypes);
org.eclipse.ceylon.model.typechecker.model.Reference appliedFunction = declaration.appliedReference(null, producedTypes);
TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForFunction(appliedFunction);
TypeDescriptor reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction);
Metamodel.checkReifiedTypeArgument("apply", "Function<$1,$2>", Variance.OUT, declaration.getUnit().getCallableReturnType(appliedFunction.getFullType()), $reifiedReturn, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction), $reifiedArguments);
return new FunctionImpl<Return, Arguments>(reifiedType, reifiedArguments, appliedFunction, this, null, null);
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class FunctionDeclarationImpl method staticApply.
@Override
public <Return extends Object, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Function<Return, Arguments> staticApply(@Ignore TypeDescriptor $reifiedReturn, @Ignore TypeDescriptor $reifiedArguments, @Name("containerType") ceylon.language.meta.model.Type<? extends Object> containerType, @Name("typeArguments") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
if (!getStatic())
throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a " + (getToplevel() ? "toplevel" : "member") + " declaration to a container type: use " + (getToplevel() ? "apply" : "memberApply"));
List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
Metamodel.checkTypeArguments(null, declaration, producedTypes);
org.eclipse.ceylon.model.typechecker.model.Reference appliedFunction = declaration.appliedReference(((ClassOrInterfaceImpl<?>) containerType).producedType, producedTypes);
TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForFunction(appliedFunction);
TypeDescriptor reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction);
if (getStatic()) {
producedTypes.addAll(0, Metamodel.getModel(containerType).getTypeArgumentList());
}
Metamodel.checkReifiedTypeArgument("apply", "Function<$1,$2>", Variance.OUT, declaration.getUnit().getCallableReturnType(appliedFunction.getFullType()), $reifiedReturn, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction), $reifiedArguments);
return new FunctionImpl<Return, Arguments>(reifiedType, reifiedArguments, appliedFunction, this, containerType, null);
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class PartialImpl method initializeObject.
protected <Id> void initializeObject(TypeDescriptor $reified$Id, DeserializationContextImpl<Id> context, Serializable instance) {
NativeMap<ReachableReference, Id> state = (NativeMap<ReachableReference, Id>) getState();
// TODO If it were a map of java.lang.String we'd avoid pointless extra boxing
java.util.Collection<ReachableReference> reachables = instance.$references$();
int numLate = 0;
for (ReachableReference r : reachables) {
if (r instanceof Member && ((Member) r).getAttribute().getLate()) {
numLate++;
} else if (r instanceof Outer) {
numLate++;
}
}
if (state.getSize() < reachables.size() - numLate) {
HashSet<ReachableReference> missingNames = new HashSet<ReachableReference>();
java.util.Iterator<ReachableReference> it = reachables.iterator();
while (it.hasNext()) {
missingNames.add(it.next());
}
ceylon.language.Iterator<? extends ReachableReference> it2 = state.getKeys().iterator();
Object next;
while (((next = it2.next()) instanceof ReachableReference)) {
missingNames.remove(next);
}
throw insufficiantState(missingNames);
}
for (ReachableReference reference : reachables) {
if (reference instanceof Member) {
Member member = (Member) reference;
if (member.getAttribute().getLate() && !state.contains(member) || state.get(member) == uninitializedLateValue_.get_()) {
continue;
}
TypeDescriptor.Class classTypeDescriptor = getClassTypeDescriptor();
Entry<TypeDescriptor.Class, String> cacheKey = new Entry<TypeDescriptor.Class, String>(TypeDescriptor.klass(TypeDescriptor.Class.class), String.$TypeDescriptor$, classTypeDescriptor, String.instance(member.getAttribute().getQualifiedName()));
Type memberType = (Type) context.getMemberTypeCache().get(cacheKey);
if (memberType == null) {
Type pt = Metamodel.getModuleManager().getCachedType(classTypeDescriptor);
while (!pt.getDeclaration().getQualifiedNameString().equals(((ClassDeclaration) member.getAttribute().getContainer()).getQualifiedName())) {
pt = pt.getExtendedType();
}
FunctionOrValue attributeDeclaration = (FunctionOrValue) ((TypeDeclaration) pt.getDeclaration()).getMember(member.getAttribute().getName(), null, false);
TypedReference attributeType = pt.getTypedMember(attributeDeclaration, Collections.<Type>emptyList(), true);
memberType = attributeType.getType();
context.getMemberTypeCache().put(cacheKey, memberType);
}
Object referredInstance = getReferredInstance(context, state, member);
if (referredInstance instanceof Tuple) {
// Because tuples are special wrt reified types...
Id referredId = state.get(member);
Object r = context.leakInstance(referredId);
if (r instanceof PartialImpl) {
((PartialImpl) r).initialize($reified$Id, context);
}
}
Type instanceType = Metamodel.getModuleManager().getCachedType(Metamodel.getTypeDescriptor(referredInstance));
if (!instanceType.isSubtypeOf(memberType)) {
throw notAssignable(member, memberType, instanceType);
}
instance.$set$(member, referredInstance);
// the JVM will check the assignability, but we need to
// check assignability at the ceylon level, so we need to know
// / type of the attribute an the type that we're assigning.
// XXX this check is really expensive!
// we should cache the attribute type on the context
// when can we avoid this check.
// XXX we can cache MethodHandle setters on the context!
} else if (reference instanceof Outer) {
// instantiating member classes
continue;
} else {
throw new AssertionError("unexpected ReachableReference " + reference);
}
}
}
use of org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor in project ceylon by eclipse.
the class Predicates method isDeclarationOfKind.
/**
* Returns a Predicate for Declarations being of the given kind
* (Class, Interface, Function, Value etc)
* @param kind A TypeDescriptor for the sought declaration kind.
*/
public static Predicate<Declaration> isDeclarationOfKind(TypeDescriptor kind) {
if (kind instanceof TypeDescriptor.Class) {
Class<?> declarationClass = (Class<?>) ((TypeDescriptor.Class) kind).getKlass();
if (declarationClass == ceylon.language.meta.declaration.ValueDeclaration.class) {
return DECLARATION_IS_VALUE;
} else if (declarationClass == ceylon.language.meta.declaration.FunctionDeclaration.class) {
return DECLARATION_IS_FUNCTION;
} else if (declarationClass == ceylon.language.meta.declaration.FunctionOrValueDeclaration.class) {
return DECLARATION_IS_FUNCTION_OR_VALUE;
} else if (declarationClass == ceylon.language.meta.declaration.ClassDeclaration.class) {
return DECLARATION_IS_CLASS;
} else if (declarationClass == ceylon.language.meta.declaration.ClassWithInitializerDeclaration.class) {
return DECLARATION_IS_CLASS_WITH_INIT;
} else if (declarationClass == ceylon.language.meta.declaration.ClassWithConstructorsDeclaration.class) {
return DECLARATION_IS_CLASS_WITH_CTORS;
} else if (declarationClass == ceylon.language.meta.declaration.InterfaceDeclaration.class) {
return DECLARATION_IS_INTERFACE;
} else if (declarationClass == ceylon.language.meta.declaration.ClassOrInterfaceDeclaration.class) {
return DECLARATION_IS_CLASS_OR_INTERFACE;
} else if (declarationClass == ceylon.language.meta.declaration.AliasDeclaration.class) {
return DECLARATION_IS_ALIAS;
} else if (declarationClass == ceylon.language.meta.declaration.ConstructorDeclaration.class) {
return DECLARATION_IS_CONSTRUCTOR;
} else if (declarationClass == ceylon.language.meta.declaration.CallableConstructorDeclaration.class) {
return DECLARATION_IS_CALLABLE_CONSTRUCTOR;
} else if (declarationClass == ceylon.language.meta.declaration.ValueConstructorDeclaration.class) {
return DECLARATION_IS_VALUE_CONSTRUCTOR;
} else if (declarationClass == ceylon.language.meta.declaration.NestableDeclaration.class) {
return true_();
}
throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive");
} else if (kind instanceof TypeDescriptor.Union) {
TypeDescriptor[] members = ((TypeDescriptor.Union) kind).getMembers();
@SuppressWarnings("unchecked") Predicate<Declaration>[] preds = new Predicate[members.length];
int ii = 0;
for (TypeDescriptor member : members) {
preds[ii++] = isDeclarationOfKind(member);
}
return or(preds);
} else if (kind instanceof TypeDescriptor.Intersection) {
TypeDescriptor[] members = ((TypeDescriptor.Intersection) kind).getMembers();
@SuppressWarnings("unchecked") Predicate<Declaration>[] preds = new Predicate[members.length];
int ii = 0;
for (TypeDescriptor member : members) {
preds[ii++] = isDeclarationOfKind(member);
}
return and(preds);
} else if (kind == TypeDescriptor.NothingType) {
return false_();
}
throw new EnumeratedTypeError("Supposedly exhaustive switch was not exhaustive");
}
Aggregations