use of org.eclipse.ceylon.model.typechecker.model.Type in project ceylon by eclipse.
the class AbstractTransformer method getJavaArrayElementType.
private JCExpression getJavaArrayElementType(Type type, int flags) {
if (type == null)
return makeErroneous(null, "compiler bug: " + type + " is not a java array");
type = simplifyType(type);
if (type == null || type.getDeclaration() instanceof Class == false)
return makeErroneous(null, "compiler bug: " + type + " is not a java array");
Class c = (Class) type.getDeclaration();
String name = c.getQualifiedNameString();
if (name.equals("java.lang::ObjectArray")) {
// fetch its type parameter
if (type.getTypeArgumentList().size() != 1)
return makeErroneous(null, "compiler bug: " + type + " is missing parameter type to java ObjectArray");
Type elementType = type.getTypeArgumentList().get(0);
if (elementType == null)
return makeErroneous(null, "compiler bug: " + type + " has null parameter type to java ObjectArray");
elementType = simplifyType(elementType);
int newFlags = flags;
if ((flags & JT_NO_PRIMITIVES) != 0)
newFlags |= JT_TYPE_ARGUMENT;
return make().TypeArray(makeJavaType(elementType, newFlags));
} else if (name.equals("java.lang::ByteArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.BYTE));
} else if (name.equals("java.lang::ShortArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.SHORT));
} else if (name.equals("java.lang::IntArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.INT));
} else if (name.equals("java.lang::LongArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.LONG));
} else if (name.equals("java.lang::FloatArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.FLOAT));
} else if (name.equals("java.lang::DoubleArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.DOUBLE));
} else if (name.equals("java.lang::BooleanArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.BOOLEAN));
} else if (name.equals("java.lang::CharArray")) {
return make().TypeArray(make().TypeIdent(TypeTag.CHAR));
} else {
return makeErroneous(null, "compiler bug: " + type + " is an unknown java array type");
}
}
use of org.eclipse.ceylon.model.typechecker.model.Type in project ceylon by eclipse.
the class AbstractTransformer method makeOptimizedTypeTest.
JCExpression makeOptimizedTypeTest(JCExpression firstTimeExpr, Naming.CName varName, Type testedType, Type expressionType) {
// TypeDeclaration widerDeclaration = expressionType.getDeclaration();
if (!isTypeTestCheap(firstTimeExpr, varName, testedType, expressionType)) {
// if (widerDeclaration instanceof UnionType
// || widerDeclaration instanceof ClassOrInterface) {
// we've got a X|Y and we're testing for X
// or parhaps a A|B|C|D and we're testing for C|D
java.util.List<Type> cases = expressionType.getCaseTypes();
if (cases != null) {
java.util.List<Type> copiedCases = new ArrayList<>(cases.size());
copiedCases.addAll(cases);
cases = copiedCases;
if ((testedType.isClassOrInterface() || testedType.isTypeParameter()) && cases.remove(testedType)) {
} else if (testedType.isUnion()) {
for (Type ct : testedType.getCaseTypes()) {
if (!cases.remove(ct)) {
cases = null;
break;
}
}
} else {
cases = null;
}
if (cases != null) {
Type complementType = typeFact().getNothingType();
for (Type ct : cases) {
complementType = org.eclipse.ceylon.model.typechecker.model.ModelUtil.unionType(complementType, ct, typeFact());
}
if (/*typeFact().getLanguageModuleDeclaration("Finished").equals(complementType.getDeclaration())
||*/
org.eclipse.ceylon.model.typechecker.model.ModelUtil.intersectionType(complementType, testedType, typeFact()).isNothing()) {
return make().Unary(JCTree.Tag.NOT, makeTypeTest(firstTimeExpr, varName, complementType, expressionType));
}
}
}
// }
}
return makeTypeTest(firstTimeExpr, varName, testedType, expressionType);
}
use of org.eclipse.ceylon.model.typechecker.model.Type in project ceylon by eclipse.
the class AbstractTransformer method getTypeArguments.
private java.util.List<Type> getTypeArguments(Reference producedReference) {
java.util.List<TypeParameter> typeParameters = getTypeParameters(producedReference);
java.util.List<Type> typeArguments = new ArrayList<Type>(typeParameters.size());
for (TypeParameter tp : typeParameters) {
Type ta;
Reference ref = producedReference;
do {
ta = ref.getTypeArguments().get(tp);
ref = ref.getQualifyingType();
} while (ta == null && ref != null);
typeArguments.add(ta);
}
return typeArguments;
}
use of org.eclipse.ceylon.model.typechecker.model.Type in project ceylon by eclipse.
the class AbstractTransformer method makeTypeParameter.
JCTypeParameter makeTypeParameter(TypeParameter declarationModel, java.util.List<Type> satisfiedTypesForBounds) {
TypeParameter typeParameterForBounds = declarationModel;
if (satisfiedTypesForBounds == null) {
satisfiedTypesForBounds = declarationModel.getSatisfiedTypes();
}
// special case for method refinenement where Java doesn't let us refine the parameter bounds
if (declarationModel.getContainer() instanceof Function) {
Function method = (Function) declarationModel.getContainer();
Function refinedMethod = (Function) method.getRefinedDeclaration();
if (!Decl.equal(method, refinedMethod)) {
// find the param index
int index = method.getTypeParameters().indexOf(declarationModel);
if (index == -1) {
log.error("Failed to find type parameter index: " + declarationModel.getName());
} else if (refinedMethod.getTypeParameters().size() > index) {
// ignore smaller index than size since the typechecker would have found the error
TypeParameter refinedTP = refinedMethod.getTypeParameters().get(index);
if (!haveSameBounds(declarationModel, refinedTP)) {
// find the right instantiation of that type parameter
TypeDeclaration methodContainer = (TypeDeclaration) method.getContainer();
TypeDeclaration refinedMethodContainer = (TypeDeclaration) refinedMethod.getContainer();
// find the supertype that gave us that method and its type arguments
Type supertype = methodContainer.getType().getSupertype(refinedMethodContainer);
satisfiedTypesForBounds = new ArrayList<Type>(refinedTP.getSatisfiedTypes().size());
for (Type satisfiedType : refinedTP.getSatisfiedTypes()) {
// substitute the refined type parameter bounds with the right type arguments
satisfiedTypesForBounds.add(satisfiedType.substitute(supertype));
}
typeParameterForBounds = refinedTP;
}
}
}
}
return makeTypeParameter(declarationModel.getName(), satisfiedTypesForBounds, typeParameterForBounds.isCovariant(), typeParameterForBounds.isContravariant());
}
use of org.eclipse.ceylon.model.typechecker.model.Type in project ceylon by eclipse.
the class AbstractTransformer method getRefinedTypedReference.
private TypedReference getRefinedTypedReference(TypedReference typedReference, TypedDeclaration refinedDeclaration) {
TypeDeclaration refinedContainer = (TypeDeclaration) refinedDeclaration.getContainer();
Type refinedContainerType = typedReference.getQualifyingType().getSupertype(refinedContainer);
ArrayList<Type> typeArgs = new ArrayList<Type>();
for (TypeParameter tp : typedReference.getDeclaration().getTypeParameters()) {
typeArgs.add(typedReference.getTypeArguments().get(tp));
}
return refinedDeclaration.appliedTypedReference(refinedContainerType, typeArgs);
}
Aggregations