protected AbstractClosureTypeHelper(XClosure closure, ITypeExpectation expectation, ITypeComputationState state) { this.closure = closure; this.expectation = expectation; this.state = state; this.services = state.getReferenceOwner().getServices(); }
protected CommonTypeComputationServices getServices() { return getOwner().getServices(); }
public ClosureTypeComputer(XClosure closure, ITypeExpectation expectation, ITypeComputationState state) { this.closure = closure; this.expectation = expectation; this.state = state; this.services = state.getReferenceOwner().getServices(); this.functionTypes = services.getFunctionTypes(); }
protected TypesFactory getTypesFactory() { return getOwner().getServices().getTypesFactory(); }
private LightweightTypeReference getCommonSuperTypes(List<LightweightTypeReference> types, ITypeReferenceOwner owner) { // use the given type list as-is TypeConformanceComputer conformanceComputer = owner.getServices().getTypeConformanceComputer(); LightweightTypeReference type = conformanceComputer.getCommonSuperType(types, owner); if (type == null) { // failed - remove void if any List<LightweightTypeReference> filteredOutTypes = Lists.newArrayListWithCapacity(types.size()); for(LightweightTypeReference outType: types) { if (!outType.isPrimitiveVoid()) { filteredOutTypes.add(outType); } } // and try again to compute the common super types if (!filteredOutTypes.isEmpty() && filteredOutTypes.size() != types.size()) { type = conformanceComputer.getCommonSuperType(filteredOutTypes, owner); } // still no success - use the first non-void type in the list or void if all are void if (type == null) { type = filteredOutTypes.isEmpty() ? types.get(0) : filteredOutTypes.get(0); } } return type; }
public int internalIsAssignableFrom(LightweightTypeReference reference, TypeConformanceComputationArgument argument) { TypeConformanceComputer conformanceCompouter = getOwner().getServices().getTypeConformanceComputer(); int result = conformanceCompouter.isConformant(this, reference, argument); return result; }
private LightweightTypeReference createUnknownTypeReference(JvmParameterizedTypeReference reference) { List<INode> nodes = NodeModelUtils.findNodesForFeature(reference, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE); if (nodes.isEmpty()) { Set<EObject> sourceElements = owner.getServices().getJvmModelAssociations().getSourceElements(reference); EObject firstSource = Iterables.getFirst(sourceElements, null); if (firstSource instanceof JvmParameterizedTypeReference) { nodes = NodeModelUtils.findNodesForFeature(firstSource, TypesPackage.Literals.JVM_PARAMETERIZED_TYPE_REFERENCE__TYPE); } } if (nodes.size() == 1) { String name = nodes.get(0).getText().trim(); if (name != null && name.length() != 0) { int lastDot = name.lastIndexOf('.'); int lastDollar = name.lastIndexOf('$'); int lastDotOrDollar = Math.max(lastDot, lastDollar); if (lastDotOrDollar != -1 && lastDotOrDollar != name.length() - 1) { String shortName = name.substring(lastDotOrDollar + 1); if (shortName.length() != 0) { name = shortName; } } return owner.newUnknownTypeReference(name); } } return owner.newUnknownTypeReference(); }
protected JvmType getObjectType() { return owner.getServices().getTypeReferences().findDeclaredType(Object.class, owner.getContextResourceSet()); }
/** * Returns <code>true</code> for expressions that seem to be early exit expressions, e.g. * <pre> * while(condition) { * if (anotherCondition) * return value * changeResultOfFirstCondition * } * </pre> */ protected boolean isIntentionalEarlyExit(/* @Nullable */ XExpression expression) { ExtendedEarlyExitComputer earlyExitComputer = getReferenceOwner().getServices().getEarlyExitComputer(); return earlyExitComputer.isIntentionalEarlyExit(expression); }
protected boolean isDefiniteEarlyExit(XExpression expression) { CommonTypeComputationServices services = getState().getReferenceOwner().getServices(); return services.getEarlyExitComputer().isDefiniteEarlyExit(expression); }
protected LightweightTypeReference normalizedMultiType(ITypeReferenceOwner referenceOwner, JvmTypeReference ref) { LightweightTypeReference result = referenceOwner.toLightweightTypeReference(ref); if (result.isSynonym()) { List<LightweightTypeReference> components = result.getMultiTypeComponents(); result = referenceOwner.getServices().getTypeConformanceComputer().getCommonSuperType(components, referenceOwner); } return result; }
@Override public JvmTypeReference toJavaCompliantTypeReference(IVisibilityHelper visibilityHelper) { return getOwner().getServices().getTypeReferences().getTypeForName(Object.class, getOwner().getContextResourceSet()); }
protected boolean isCompoundOperator() { if (!(getFeatureCall() instanceof XBinaryOperation)) { return false; } String methodName = getFeature().getSimpleName(); return getState().getReferenceOwner().getServices().getOperatorMapping().isCompoundMethod(methodName); }
protected JvmTypeReference resolveSynonymType(JvmSynonymTypeReference reference, EObject context) { LightweightTypeReference lightweight = toLightweight(reference, context); LightweightTypeReference superType = lightweight.getOwner().getServices().getTypeConformanceComputer().getCommonSuperType(lightweight.getMultiTypeComponents(), lightweight.getOwner()); return superType.toJavaCompliantTypeReference(); }
@Override public JvmTypeReference toTypeReference() { XFunctionTypeRef result = getOwner().getServices().getXtypeFactory().createXFunctionTypeRef(); result.setType(getType()); result.setEquivalent(getEquivalentTypeReference()); if (parameterTypes != null) { for(LightweightTypeReference parameterType: parameterTypes) { result.getParamTypes().add(parameterType.toTypeReference()); } } if (returnType != null) { result.setReturnType(returnType.toTypeReference()); } return result; }
LightweightTypeReference declaredType = arguments.getDeclaredTypeForLambda(idx); if (declaredType != null && !declaredType.isType(Object.class)) { CommonTypeComputationServices services = getState().getReferenceOwner().getServices(); JvmOperation operation = services.getFunctionTypes().findImplementingOperation(declaredType); if (operation == null) {
@Override public LightweightTypeReference doVisitParameterizedTypeReference(ParameterizedTypeReference reference) { DeclaratorTypeArgumentCollector typeArgumentCollector = new ConstraintAwareTypeArgumentCollector(owner); Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> typeParameterMapping = typeArgumentCollector.getTypeParameterMapping(reference); TypeParameterSubstitutor<?> substitutor = new UnboundTypeParameterPreservingSubstitutor(typeParameterMapping, owner); JvmGenericType iterable = (JvmGenericType) owner.getServices().getTypeReferences().findDeclaredType(Iterable.class, owner.getContextResourceSet()); if (iterable == null) { return owner.newReferenceToObject(); } LightweightTypeReference substituteMe = owner.newParameterizedTypeReference(iterable.getTypeParameters().get(0)); LightweightTypeReference substitutedArgument = substitutor.substitute(substituteMe).getUpperBoundSubstitute(); if (substitutedArgument.getType() instanceof JvmTypeParameter && !(owner.getDeclaredTypeParameters().contains(substitutedArgument.getType()))) { return substitutedArgument.getRawTypeReference(); } return substitutedArgument; }
protected JvmTypeReference toJavaCompliantTypeReference(List<LightweightTypeReference> types, IVisibilityHelper visibilityHelper) { LightweightTypeReference type = getServices().getTypeConformanceComputer().getCommonSuperType(types, getOwner()); if (type == null) { return getOwner().getServices().getTypeReferences().getTypeForName(Object.class, getOwner().getContextResourceSet()); } return type.toJavaCompliantTypeReference(visibilityHelper); }
/** * Same as {@link LightweightTypeReference#isSubtypeOf(Class)} but does not accept synonym types as subtypes. */ protected boolean isSubtypeButNotSynonym(LightweightTypeReference expectation, Class<?> clazz) { if (expectation.isType(clazz)) { return true; } ITypeReferenceOwner owner = expectation.getOwner(); JvmType declaredType = owner.getServices().getTypeReferences().findDeclaredType(clazz, owner.getContextResourceSet()); if (declaredType == null) { return false; } LightweightTypeReference superType = owner.newParameterizedTypeReference(declaredType); // don't allow synonyms, e.g. Iterable is not considered to be a supertype of Functions.Function0 boolean result = superType.isAssignableFrom(expectation.getRawTypeReference(), new TypeConformanceComputationArgument(false, false, true, true, false, false)); return result; }
private void convertMultiType(LightweightTypeReference expectation, CompoundTypeReference multiType, XExpression context, ITreeAppendable b, Later expression) { LightweightTypeReference castTo = null; List<LightweightTypeReference> components = multiType.getMultiTypeComponents(); ITypeReferenceOwner owner = multiType.getOwner(); LightweightTypeReference commonType = owner.getServices().getTypeConformanceComputer().getCommonSuperType(components, owner); if (!isJavaConformant(expectation, commonType)) { for(LightweightTypeReference candidate: multiType.getMultiTypeComponents()) { if (isJavaConformant(expectation, candidate)) { castTo = candidate; break; } } } if (castTo != null && mustInsertTypeCast(context, castTo)) { b.append("(("); b.append(castTo); b.append(")"); expression.exec(b); b.append(")"); } else { expression.exec(b); } }