use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method getTypedReference.
TypedReference getTypedReference(TypedDeclaration decl) {
java.util.List<Type> typeArgs = Collections.<Type>emptyList();
if (decl instanceof Function) {
// For methods create type arguments for any type parameters it might have
Function m = (Function) decl;
if (!m.getTypeParameters().isEmpty()) {
typeArgs = new ArrayList<Type>(m.getTypeParameters().size());
for (TypeParameter p : m.getTypeParameters()) {
Type pt = p.getType();
typeArgs.add(pt);
}
}
}
if (decl.getContainer() instanceof TypeDeclaration) {
TypeDeclaration containerDecl = (TypeDeclaration) decl.getContainer();
return containerDecl.getType().getTypedMember(decl, typeArgs);
}
return decl.appliedTypedReference(null, typeArgs);
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method canOptimiseReifiedTypeTest.
/**
* Determine whether we can use a plain {@code instanceof} instead of
* a full {@code Util.isReified()} for a {@code is} test
*/
private boolean canOptimiseReifiedTypeTest(Type type) {
if (isJavaArray(type)) {
if (isJavaObjectArray(type)) {
MultidimensionalArray multiArray = getMultiDimensionalArrayInfo(type);
// we can test, even if not fully reified in Java
return multiArray.type.getDeclaration() instanceof ClassOrInterface;
} else {
// primitive array we can test
return true;
}
}
// we can optimise it if we've got a ClassOrInterface with only Anything type parameters
if (type.getDeclaration() instanceof ClassOrInterface == false)
return false;
for (Entry<TypeParameter, Type> entry : type.getTypeArguments().entrySet()) {
TypeParameter tp = entry.getKey();
if (!type.isCovariant(tp)) {
return false;
}
java.util.List<Type> bounds = tp.getSatisfiedTypes();
Type ta = entry.getValue();
if ((bounds == null || bounds.isEmpty()) && !isAnything(ta)) {
return false;
}
for (Type bound : bounds) {
if (!ta.isSupertypeOf(bound)) {
return false;
}
}
}
// they're all Anything (or supertypes of their upper bound) we can optimise, unless we have a container with type arguments
Type qualifyingType = type.getQualifyingType();
if (qualifyingType == null && // ignore qualifying types of static java declarations
(Decl.isCeylon(type.getDeclaration()) || !type.getDeclaration().isStaticallyImportable())) {
Declaration declaration = type.getDeclaration();
do {
// it may be contained in a function or value, and we want its type
Declaration enclosingDeclaration = getDeclarationContainer(declaration);
if (enclosingDeclaration instanceof TypedDeclaration) {
// must be in scope
if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
return false;
// look up the containers
declaration = enclosingDeclaration;
} else if (enclosingDeclaration instanceof TypeDeclaration) {
// we can't optimise if that container has type arguments as they are not provided
if (enclosingDeclaration instanceof Generic && !((Generic) enclosingDeclaration).getTypeParameters().isEmpty())
return false;
// look up the containers
declaration = enclosingDeclaration;
} else {
// that's fucked up
break;
}
// go up every containing typed declaration
} while (declaration != null);
// we can optimise!
return true;
} else if (qualifyingType != null) {
// we can only optimise if the qualifying type can also be optimised
return canOptimiseReifiedTypeTest(qualifyingType);
} else {
// we can optimise!
return true;
}
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AnnotationModelVisitor method startCollection.
private CollectionLiteralAnnotationTerm startCollection(Tree.Term t) {
Unit unit = t.getUnit();
// Continue the visit to collect the elements
Type iteratedType = unit.getIteratedType(parameter().getType());
LiteralAnnotationTerm factory;
if (iteratedType.isString()) {
factory = StringLiteralAnnotationTerm.FACTORY;
} else if (iteratedType.isInteger()) {
factory = IntegerLiteralAnnotationTerm.FACTORY;
} else if (iteratedType.isCharacter()) {
factory = CharacterLiteralAnnotationTerm.FACTORY;
} else if (iteratedType.isBoolean()) {
factory = BooleanLiteralAnnotationTerm.FACTORY;
} else if (iteratedType.isFloat()) {
factory = FloatLiteralAnnotationTerm.FACTORY;
} else if (Decl.isEnumeratedTypeWithAnonCases(iteratedType)) {
factory = ObjectLiteralAnnotationTerm.FACTORY;
} else if (Decl.isAnnotationClass(iteratedType.getDeclaration())) {
t.addError("compiler bug: iterables of annotation classes or annotation constructors not supported as literal " + (checkingDefaults ? "defaulted parameters" : "arguments"), Backend.Java);
return null;
} else if (iteratedType.isSubtypeOf(((TypeDeclaration) unit.getLanguageModuleDeclarationDeclaration("Declaration")).getType())) {
factory = DeclarationLiteralAnnotationTerm.FACTORY;
} else {
throw new RuntimeException();
}
CollectionLiteralAnnotationTerm result = this.elements;
this.elements = new CollectionLiteralAnnotationTerm(factory);
return result;
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method getSimpleNumParametersOfCallable.
private int getSimpleNumParametersOfCallable(Type args) {
// can be a defaulted tuple of Empty|Tuple
if (args.isUnion()) {
java.util.List<Type> caseTypes = args.getCaseTypes();
if (caseTypes == null || caseTypes.size() != 2)
return -1;
Type caseA = caseTypes.get(0);
TypeDeclaration caseADecl = caseA.getDeclaration();
Type caseB = caseTypes.get(1);
TypeDeclaration caseBDecl = caseB.getDeclaration();
if (caseADecl instanceof ClassOrInterface == false || caseBDecl instanceof ClassOrInterface == false)
return -1;
if (caseADecl.getQualifiedNameString().equals("ceylon.language::Empty") && caseBDecl.getQualifiedNameString().equals("ceylon.language::Tuple"))
return getSimpleNumParametersOfCallable(caseB);
if (caseBDecl.getQualifiedNameString().equals("ceylon.language::Empty") && caseADecl.getQualifiedNameString().equals("ceylon.language::Tuple"))
return getSimpleNumParametersOfCallable(caseA);
return -1;
}
// can be Tuple, Empty, Sequence or Sequential
if (!args.isClassOrInterface())
return -1;
TypeDeclaration declaration = args.getDeclaration();
String name = declaration.getQualifiedNameString();
if (name.equals("ceylon.language::Tuple")) {
Type rest = args.getTypeArgumentList().get(2);
int ret = getSimpleNumParametersOfCallable(rest);
if (ret == -1)
return -1;
return ret + 1;
}
if (name.equals("ceylon.language::Empty")) {
return 0;
}
if (name.equals("ceylon.language::Sequential") || name.equals("ceylon.language::Sequence")) {
return 1;
}
return -1;
}
use of com.redhat.ceylon.model.typechecker.model.TypeDeclaration in project ceylon-compiler by ceylon.
the class AbstractTransformer method willEraseToObject.
/**
* Determines if a type will be erased to java.lang.Object once converted to Java
* @param type
* @return
*/
boolean willEraseToObject(Type type) {
if (type == null)
return false;
type = simplifyType(type);
if (type.isUnion() || type.isIntersection()) {
// except for the ones that erase to Sequential
return true;
}
TypeDeclaration decl = type.getDeclaration();
// All the following types either are Object or erase to Object
if (Decl.equal(decl, typeFact.getObjectDeclaration()) || Decl.equal(decl, typeFact.getIdentifiableDeclaration()) || Decl.equal(decl, typeFact.getBasicDeclaration()) || Decl.equal(decl, typeFact.getNullDeclaration()) || Decl.equal(decl, typeFact.getNullValueDeclaration().getTypeDeclaration()) || Decl.equal(decl, typeFact.getAnythingDeclaration()) || type.isNothing()) {
return true;
}
return false;
}
Aggregations