use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class TypeParserTests method testTuple1Abbrev.
@Test
public void testTuple1Abbrev() {
Type type = new TypeParser(MockLoader.instance).decodeType("[a]", null, mockDefaultModule, mockPkgUnit);
Assert.assertNotNull(type);
TypeDeclaration declaration = type.getDeclaration();
Assert.assertNotNull(declaration);
Assert.assertTrue(declaration instanceof Class);
Assert.assertEquals("ceylon.language::Tuple", declaration.getQualifiedNameString());
Assert.assertEquals("[a]", type.asString());
Assert.assertNull(type.getQualifyingType());
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method makeJavaType.
JCExpression makeJavaType(final Type ceylonType, final int flags) {
Type type = ceylonType;
if (type == null || type.isUnknown())
return make().Erroneous();
if (type.getDeclaration() instanceof Constructor) {
type = type.getExtendedType();
}
// resolve aliases
if ((flags & JT_CLASS_LITERAL) == 0)
type = type.resolveAliases();
if ((flags & __JT_RAW_TP_BOUND) != 0 && type.isTypeParameter()) {
type = type.getExtendedType();
}
if (type.isUnion()) {
for (Type pt : type.getCaseTypes()) {
if (pt.getDeclaration().isAnonymous()) {
// found one, let's try to make it simpler
Type simplerType = typeFact().denotableType(type);
if (!simplerType.isNothing() && !simplerType.isUnion()) {
type = simplerType;
} else if (isCeylonBoolean(simplifyType(simplerType))) {
type = simplerType;
}
break;
}
}
}
if (type.getDeclaration().isJavaEnum()) {
type = type.getExtendedType();
}
if (type.isTypeConstructor()) {
return make().QualIdent(syms().ceylonAbstractTypeConstructorType.tsym);
}
// ERASURE
if ((flags & JT_CLASS_LITERAL) == 0 && // literals to the alias class
willEraseToObject(type)) {
// - The Ceylon type U|V results in the Java type Object
if ((flags & JT_SATISFIES) != 0) {
return null;
} else {
return make().Type(syms().objectType);
}
} else if (willEraseToAnnotation(type)) {
return make().Type(syms().annotationType);
} else if (willEraseToException(type)) {
if ((flags & JT_CLASS_NEW) != 0 || (flags & JT_EXTENDS) != 0) {
return makeIdent(syms().ceylonExceptionType);
} else {
return make().Type(syms().exceptionType);
}
} else if (willEraseToThrowable(type)) {
if ((flags & JT_CLASS_NEW) != 0 || (flags & JT_EXTENDS) != 0) {
return makeIdent(syms().throwableType);
} else {
return make().Type(syms().throwableType);
}
} else if (willEraseToSequence(type)) {
if ((flags & (JT_CLASS_NEW | JT_EXTENDS | JT_IS)) == 0) {
Type typeArg = simplifyType(type).getTypeArgumentList().get(0);
Type seqType = typeFact.getSequenceType(typeArg);
if (typeFact.isOptionalType(type)) {
type = typeFact.getOptionalType(seqType);
} else {
type = seqType;
}
}
} else if ((flags & (JT_SATISFIES | JT_EXTENDS | JT_NO_PRIMITIVES | JT_CLASS_NEW)) == 0 && ((isCeylonBasicType(type) && !isOptional(type)) || isJavaString(type))) {
if (isCeylonString(type) || isJavaString(type)) {
return make().Type(syms().stringType);
} else if (isCeylonBoolean(type)) {
return make().TypeIdent(TypeTags.BOOLEAN);
} else if (isCeylonInteger(type)) {
if ("short".equals(type.getUnderlyingType())) {
return make().TypeIdent(TypeTags.SHORT);
} else if ((flags & JT_SMALL) != 0 || "int".equals(type.getUnderlyingType())) {
return make().TypeIdent(TypeTags.INT);
} else {
return make().TypeIdent(TypeTags.LONG);
}
} else if (isCeylonFloat(type)) {
if ((flags & JT_SMALL) != 0 || "float".equals(type.getUnderlyingType())) {
return make().TypeIdent(TypeTags.FLOAT);
} else {
return make().TypeIdent(TypeTags.DOUBLE);
}
} else if (isCeylonCharacter(type)) {
if ("char".equals(type.getUnderlyingType())) {
return make().TypeIdent(TypeTags.CHAR);
} else {
return make().TypeIdent(TypeTags.INT);
}
} else if (isCeylonByte(type)) {
return make().TypeIdent(TypeTags.BYTE);
}
} else if (isCeylonBoolean(type) && !isTypeParameter(type)) {
//&& (flags & TYPE_ARGUMENT) == 0){
// special case to get rid of $true and $false types
type = typeFact.getBooleanType();
} else if ((flags & JT_VALUE_TYPE) == 0 && isJavaArray(type)) {
return getJavaArrayElementType(type, flags);
}
JCExpression jt = null;
Type simpleType;
if ((flags & JT_CLASS_LITERAL) == 0)
simpleType = simplifyType(type);
else
simpleType = type;
// see if we need to cross methods when looking up container types
// this is required to properly collect all the type parameters for local interfaces
// which we pull up to the toplevel and capture all the container type parameters
boolean needsQualifyingTypeArgumentsFromLocalContainers = Decl.isCeylon(simpleType.getDeclaration()) && simpleType.getDeclaration() instanceof Interface && // this is only valid for interfaces, not for their companion which stay where they are
(flags & JT_COMPANION) == 0;
java.util.List<Reference> qualifyingTypes = null;
Reference qType = simpleType;
boolean hasTypeParameters = false;
while (qType != null) {
hasTypeParameters |= !qType.getTypeArguments().isEmpty();
if (qualifyingTypes != null)
qualifyingTypes.add(qType);
Declaration typeDeclaration = qType.getDeclaration();
// all the containing type parameters that it captures
if (// local or anonymous
(Decl.isLocal(typeDeclaration) || !typeDeclaration.isNamed()) && needsQualifyingTypeArgumentsFromLocalContainers && typeDeclaration instanceof ClassOrInterface) {
Declaration container = Decl.getDeclarationScope(typeDeclaration.getContainer());
while (container instanceof Function) {
qType = ((Function) container).getReference();
if (qualifyingTypes == null) {
qualifyingTypes = new java.util.ArrayList<Reference>();
qualifyingTypes.add(simpleType);
}
hasTypeParameters = true;
qualifyingTypes.add(qType);
container = Decl.getDeclarationScope(container.getContainer());
}
if (container instanceof TypeDeclaration) {
qType = ((TypeDeclaration) container).getType();
} else {
qType = null;
}
} else if (typeDeclaration.isNamed()) {
// avoid anonymous types which may pretend that they have a qualifying type
qType = qType.getQualifyingType();
if (qType != null && qType.getDeclaration() instanceof ClassOrInterface == false) {
// sometimes the typechecker throws qualifying intersections at us and
// we can't make anything of them, since some members may be unrelated to
// the qualified declaration. This happens with "extends super.Foo()"
// for example. See https://github.com/ceylon/ceylon-compiler/issues/1478
qType = ((Type) qType).getSupertype((TypeDeclaration) typeDeclaration.getContainer());
}
} else {
// skip local declaration containers
qType = null;
}
// delayed allocation if we have a qualifying type
if (qualifyingTypes == null && qType != null) {
qualifyingTypes = new java.util.ArrayList<Reference>();
qualifyingTypes.add(simpleType);
}
}
int firstQualifyingTypeWithTypeParameters = qualifyingTypes != null ? qualifyingTypes.size() - 1 : 0;
// find the first static one, from the right to the left
if (qualifyingTypes != null) {
for (Reference pt : qualifyingTypes) {
Declaration declaration = pt.getDeclaration();
if (declaration instanceof TypeDeclaration && Decl.isStatic((TypeDeclaration) declaration)) {
break;
}
firstQualifyingTypeWithTypeParameters--;
}
if (firstQualifyingTypeWithTypeParameters < 0)
firstQualifyingTypeWithTypeParameters = 0;
// put them in outer->inner order
Collections.reverse(qualifyingTypes);
}
if (((flags & JT_RAW) == 0) && hasTypeParameters && !rawSupertype(ceylonType, flags)) {
// special case for interfaces because we pull them into toplevel types
if (Decl.isCeylon(simpleType.getDeclaration()) && qualifyingTypes != null && qualifyingTypes.size() > 1 && simpleType.getDeclaration() instanceof Interface && // this is only valid for interfaces, not for their companion which stay where they are
(flags & JT_COMPANION) == 0) {
JCExpression baseType;
TypeDeclaration tdecl = simpleType.getDeclaration();
// collect all the qualifying type args we'd normally have
java.util.List<TypeParameter> qualifyingTypeParameters = new java.util.ArrayList<TypeParameter>();
java.util.Map<TypeParameter, Type> qualifyingTypeArguments = new java.util.HashMap<TypeParameter, Type>();
collectQualifyingTypeArguments(qualifyingTypeParameters, qualifyingTypeArguments, qualifyingTypes);
ListBuffer<JCExpression> typeArgs = makeTypeArgs(isCeylonCallable(simpleType), flags, qualifyingTypeArguments, qualifyingTypeParameters, simpleType);
if (isCeylonCallable(type) && (flags & JT_CLASS_NEW) != 0) {
baseType = makeIdent(syms().ceylonAbstractCallableType);
} else {
baseType = naming.makeDeclarationName(tdecl, DeclNameFlag.QUALIFIED);
}
if (typeArgs != null && typeArgs.size() > 0) {
jt = make().TypeApply(baseType, typeArgs.toList());
} else {
jt = baseType;
}
} else if ((flags & JT_NON_QUALIFIED) == 0) {
int index = 0;
if (qualifyingTypes != null) {
for (Reference qualifyingType : qualifyingTypes) {
jt = makeParameterisedType(qualifyingType.getType(), type, flags, jt, qualifyingTypes, firstQualifyingTypeWithTypeParameters, index);
index++;
}
} else {
jt = makeParameterisedType(simpleType, type, flags, jt, qualifyingTypes, firstQualifyingTypeWithTypeParameters, index);
}
} else {
jt = makeParameterisedType(type, type, flags, jt, qualifyingTypes, 0, 0);
}
} else {
TypeDeclaration tdecl = simpleType.getDeclaration();
// - The Ceylon type T results in the Java type T
if (isCeylonCallable(type) && (flags & JT_CLASS_NEW) != 0) {
jt = makeIdent(syms().ceylonAbstractCallableType);
} else if (tdecl instanceof TypeParameter)
jt = makeQuotedIdent(tdecl.getName());
else // don't use underlying type if we want no primitives
if ((flags & (JT_SATISFIES | JT_NO_PRIMITIVES)) != 0 || simpleType.getUnderlyingType() == null) {
jt = naming.makeDeclarationName(tdecl, jtFlagsToDeclNameOpts(flags));
} else
jt = makeQuotedFQIdent(simpleType.getUnderlyingType());
}
return (jt != null) ? jt : makeErroneous(null, "compiler bug: the java type corresponding to " + ceylonType + " could not be computed");
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method isUnknownTuple.
private boolean isUnknownTuple(Type args) {
if (args.isTypeParameter()) {
return true;
} else if (args.isUnion()) {
/* Callable<R,A>&Callable<R,B> is the same as Callable<R,A|B> so
* for a union if either A or B is known then the union is known
*/
java.util.List<Type> caseTypes = args.getCaseTypes();
if (caseTypes == null || caseTypes.size() < 2)
return true;
for (int ii = 0; ii < caseTypes.size(); ii++) {
if (!isUnknownTuple(caseTypes.get(ii))) {
return false;
}
}
// all unknown
return true;
} else if (args.isIntersection()) {
/* Callable<R,A>|Callable<R,B> is the same as Callable<R,A&B> so
* for an intersection if both A and B are known then the intersection is known
*/
java.util.List<Type> caseTypes = args.getSatisfiedTypes();
if (caseTypes == null || caseTypes.size() < 2)
return true;
for (int ii = 0; ii < caseTypes.size(); ii++) {
if (isUnknownTuple(caseTypes.get(ii))) {
return true;
}
}
return false;
} else if (args.isNothing()) {
return true;
} else if (args.isClassOrInterface()) {
TypeDeclaration declaration = args.getDeclaration();
String name = declaration.getQualifiedNameString();
if (name.equals("ceylon.language::Tuple")) {
Type rest = args.getTypeArgumentList().get(2);
return isUnknownTuple(rest);
}
if (name.equals("ceylon.language::Empty")) {
return false;
}
if (name.equals("ceylon.language::Sequential") || name.equals("ceylon.language::Sequence")) {
return false;
}
} else if (args.isTypeAlias()) {
return isUnknownTuple(args.resolveAliases());
}
return true;
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method willEraseToSequence.
boolean willEraseToSequence(Type type) {
type = simplifyType(type);
TypeDeclaration decl = type.getDeclaration();
return Decl.equal(decl, typeFact.getTupleDeclaration());
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method willEraseToThrowable.
boolean willEraseToThrowable(Type type) {
type = simplifyType(type);
TypeDeclaration decl = type.getDeclaration();
return Decl.equal(decl, typeFact.getThrowableDeclaration());
}
Aggregations