use of org.eclipse.ceylon.model.typechecker.model.Reference 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.Reference in project ceylon by eclipse.
the class AbstractTransformer method collectQualifyingTypeArguments.
/**
* Collects all the type parameters and arguments required for an interface that's been pulled up to the
* toplevel, including its containing type and method type parameters.
*/
private void collectQualifyingTypeArguments(java.util.List<TypeParameter> qualifyingTypeParameters, Map<TypeParameter, Type> qualifyingTypeArguments, java.util.List<Reference> qualifyingTypes) {
// make sure we only add type parameters with the same name once, as duplicates are erased from the target interface
// since they cannot be accessed
Set<String> names = new HashSet<String>();
// walk the qualifying types backwards to make sure we only add a TP with the same name once and the outer one wins
for (int i = qualifyingTypes.size() - 1; i >= 0; i--) {
Reference qualifiedType = qualifyingTypes.get(i);
Map<TypeParameter, Type> tas = qualifiedType.getTypeArguments();
java.util.List<TypeParameter> tps = qualifiedType.getDeclaration().getTypeParameters();
// add any type params for this type
if (tps != null) {
int index = 0;
for (TypeParameter tp : tps) {
// add it only once
if (names.add(tp.getName())) {
// start putting all these type parameters at 0 and then in order
// so that outer type params end up before inner type params but
// order is preserved within each type
qualifyingTypeParameters.add(index++, tp);
qualifyingTypeArguments.put(tp, tas.get(tp));
}
}
}
// add any container method TP
Declaration declaration = qualifiedType.getDeclaration();
if (Decl.isLocal(declaration)) {
Scope scope = declaration.getContainer();
// collect every container method until the next type or package
java.util.List<Function> methods = new LinkedList<Function>();
while (scope != null && scope instanceof ClassOrInterface == false && scope instanceof Package == false) {
if (scope instanceof Function) {
methods.add((Function) scope);
}
scope = scope.getContainer();
}
// methods are sorted inner to outer, which is the order we're following here for types
for (Function method : methods) {
java.util.List<TypeParameter> methodTypeParameters = method.getTypeParameters();
if (methodTypeParameters != null) {
int index = 0;
for (TypeParameter tp : methodTypeParameters) {
// add it only once
if (names.add(tp.getName())) {
// start putting all these type parameters at 0 and then in order
// so that outer type params end up before inner type params but
// order is preserved within each type
qualifyingTypeParameters.add(index++, tp);
qualifyingTypeArguments.put(tp, tp.getType());
}
}
}
}
}
}
}
use of org.eclipse.ceylon.model.typechecker.model.Reference in project ceylon by eclipse.
the class AbstractTransformer method nonWideningType.
Type nonWideningType(TypedReference declaration, TypedReference refinedDeclaration) {
final Reference pr;
if (declaration.equals(refinedDeclaration)) {
pr = declaration;
} else {
Type refinedType = refinedDeclaration.getType();
// since it may have changed name
if (refinedType.getDeclaration() instanceof TypeParameter && refinedType.getDeclaration().getContainer() instanceof Function) {
// find its index in the refined declaration
TypeParameter refinedTypeParameter = (TypeParameter) refinedType.getDeclaration();
Function refinedMethod = (Function) refinedTypeParameter.getContainer();
int i = 0;
for (TypeParameter tp : refinedMethod.getTypeParameters()) {
if (tp.getName().equals(refinedTypeParameter.getName()))
break;
i++;
}
if (i >= refinedMethod.getTypeParameters().size()) {
throw new BugException("can't find type parameter " + refinedTypeParameter.getName() + " in its container " + refinedMethod.getName());
}
// the refining method type parameter should be at the same index
if (declaration.getDeclaration() instanceof Function == false)
throw new BugException("refining declaration is not a method: " + declaration);
Function refiningMethod = (Function) declaration.getDeclaration();
if (i >= refiningMethod.getTypeParameters().size()) {
throw new BugException("refining method does not have enough type parameters to refine " + refinedMethod.getName());
}
pr = refiningMethod.getTypeParameters().get(i).getType();
} else {
pr = refinedType;
}
}
if (pr.getDeclaration() instanceof Functional && Decl.isMpl((Functional) pr.getDeclaration())) {
// the innermost Callable.
return getReturnTypeOfCallable(pr.getFullType());
}
return getPinnedType(declaration, pr.getType());
}
use of org.eclipse.ceylon.model.typechecker.model.Reference in project ceylon by eclipse.
the class BoxingVisitor method visit.
@Override
public void visit(QualifiedMemberExpression that) {
super.visit(that);
// handle errors gracefully
if (that.getDeclaration() == null)
return;
if (that.getMemberOperator() instanceof Tree.SafeMemberOp) {
TypedDeclaration decl = (TypedDeclaration) that.getDeclaration();
if (CodegenUtil.isRaw(decl))
CodegenUtil.markRaw(that);
if (CodegenUtil.hasTypeErased(decl))
CodegenUtil.markTypeErased(that);
if (CodegenUtil.hasUntrustedType(decl) || hasTypeParameterWithConstraintsOutsideScope(decl.getType(), that.getScope()))
CodegenUtil.markUntrustedType(that);
// we must be boxed, since safe member op "?." returns an optional type
// return;
} else if (that.getMemberOperator() instanceof Tree.MemberOp && Decl.isValueTypeDecl(that.getPrimary()) && CodegenUtil.isUnBoxed(that.getPrimary())) {
// it's unboxed if it's an unboxable type or it's declared void
if (Decl.isValueTypeDecl((TypedDeclaration) that.getDeclaration()) || (that.getDeclaration() instanceof Function && ((Function) that.getDeclaration()).isDeclaredVoid()))
CodegenUtil.markUnBoxed(that);
if (CodegenUtil.isRaw((TypedDeclaration) that.getDeclaration()))
CodegenUtil.markRaw(that);
if (CodegenUtil.hasTypeErased((TypedDeclaration) that.getDeclaration()))
CodegenUtil.markTypeErased(that);
} else {
propagateFromDeclaration(that, (TypedDeclaration) that.getDeclaration());
}
// be (ex: <String>), and in that case we will generate a proper Sequential<String> which is not raw at all
if (that.getMemberOperator() instanceof Tree.SpreadOp) {
// find the return element type
Type elementType = that.getTarget().getType();
CodegenUtil.markTypeErased(that, hasErasure(elementType));
}
if (ExpressionTransformer.isSuperOrSuperOf(that.getPrimary())) {
// if the target is an interface whose type arguments have been turned to raw, make this expression
// as erased
Reference target = that.getTarget();
if (target != null && target.getQualifyingType() != null && target.getQualifyingType().getDeclaration() instanceof Interface) {
if (isRaw(target.getQualifyingType())) {
CodegenUtil.markTypeErased(that);
} else // See note in ClassTransformer.makeDelegateToCompanion for a similar test
{
TypeDeclaration declaration = target.getQualifyingType().getDeclaration();
if (needsRawCastForMixinSuperCall(declaration, target.getType()))
CodegenUtil.markTypeErased(that);
}
}
}
Type primaryType;
if (that.getPrimary() instanceof Tree.Package || that.getTarget() == null) {
primaryType = that.getPrimary().getTypeModel();
} else {
primaryType = that.getTarget().getQualifyingType();
}
if (primaryType != null && (isRaw(primaryType) || willEraseToSequence(primaryType)) && that.getTarget() != null && that.getTarget().getDeclaration() instanceof TypedDeclaration && CodegenUtil.containsTypeParameter(((TypedDeclaration) that.getTarget().getDeclaration()).getType())) {
CodegenUtil.markTypeErased(that);
}
if (isRaw(primaryType) && that.getTypeModel().getDeclaration().isParameterized()) {
CodegenUtil.markRaw(that);
}
}
use of org.eclipse.ceylon.model.typechecker.model.Reference in project ceylon by eclipse.
the class ExpressionVisitor method accountForIntermediateRefinements.
private Reference accountForIntermediateRefinements(Tree.SpecifierStatement that, FunctionOrValue refinedMethodOrValue, FunctionOrValue methodOrValue, ClassOrInterface refiningType, List<Declaration> interveningRefinements) {
// accumulate an intersection of the types of
// everything it refines
List<Type> refinedTypes = new ArrayList<Type>();
// don't check this one here because it is
// separately checked in visit(SpecifierStatement)
Reference refinedProducedReference = getRefinedMemberReference(refinedMethodOrValue, refiningType);
Map<TypeParameter, Type> substs = substitutions(refinedMethodOrValue, methodOrValue);
Type refinedType = refinedProducedReference.getType().substitute(substs, null);
boolean allHaveNulls = hasNullReturnValues(refinedType, refinedMethodOrValue);
intersectReturnType(refinedTypes, refinedType);
for (Declaration intervening : interveningRefinements) {
if (intervening instanceof FunctionOrValue && // factors here as well?
!refinedMethodOrValue.equals(intervening)) {
FunctionOrValue refinement = (FunctionOrValue) intervening;
Reference refinedMember = getRefinedMemberReference(refinement, refiningType);
Map<TypeParameter, Type> subs = substitutions(refinement, methodOrValue);
Type type = refinedMember.getType().substitute(subs, null);
allHaveNulls = allHaveNulls && hasNullReturnValues(type, refinement);
intersectReturnType(refinedTypes, type);
checkIntermediateRefinement(that, refinement, refinedMember);
}
}
Type it = canonicalIntersection(refinedTypes, unit);
if (allHaveNulls && !unit.isOptionalType(it)) {
methodOrValue.setUncheckedNullType(true);
Tree.Term lhs = that.getBaseMemberExpression();
// TODO: this is pretty ugly, think of something better!
lhs.setTypeModel(unit.getOptionalType(lhs.getTypeModel()));
}
methodOrValue.setType(it);
return refinedProducedReference;
}
Aggregations