Search in sources :

Example 31 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class XtendTypeComputer method _computeTypes.

protected void _computeTypes(RichStringIf object, ITypeComputationState state) {
    LightweightTypeReference charSequence = getRawTypeForName(CharSequence.class, state);
    LightweightTypeReference booleanType = getRawTypeForName(Boolean.TYPE, state);
    ITypeComputationState conditionExpectation = state.withExpectation(booleanType);
    XExpression condition = object.getIf();
    conditionExpectation.computeTypes(condition);
    XExpression thenExpression = object.getThen();
    ITypeComputationState thenState = reassignCheckedType(condition, thenExpression, state);
    thenState.withExpectation(charSequence).computeTypes(thenExpression);
    for (RichStringElseIf elseIf : object.getElseIfs()) {
        state.withExpectation(booleanType).computeTypes(elseIf.getIf());
        ITypeComputationState elseState = reassignCheckedType(elseIf.getIf(), elseIf.getThen(), state);
        elseState.withExpectation(charSequence).computeTypes(elseIf.getThen());
    }
    state.withExpectation(charSequence).computeTypes(object.getElse());
    state.acceptActualType(charSequence);
}
Also used : ITypeComputationState(org.eclipse.xtext.xbase.typesystem.computation.ITypeComputationState) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) RichStringElseIf(org.eclipse.xtend.core.xtend.RichStringElseIf)

Example 32 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class XtendTypeComputer method _computeTypes.

protected void _computeTypes(RichString object, ITypeComputationState state) {
    List<XExpression> expressions = object.getExpressions();
    if (!expressions.isEmpty()) {
        for (XExpression expression : expressions) {
            ITypeComputationState expressionState = state.withoutExpectation();
            expressionState.computeTypes(expression);
            if (expression instanceof XVariableDeclaration) {
                addLocalToCurrentScope((XVariableDeclaration) expression, state);
            }
        }
    }
    for (ITypeExpectation expectation : state.getExpectations()) {
        LightweightTypeReference expectedType = expectation.getExpectedType();
        if (expectedType != null && expectedType.isType(StringConcatenation.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        } else if (expectedType != null && expectedType.isType(StringConcatenationClient.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        } else if (expectedType != null && expectedType.isType(String.class)) {
            expectation.acceptActualType(expectedType, ConformanceFlags.CHECKED_SUCCESS | ConformanceFlags.DEMAND_CONVERSION);
        // TODO this special treatment here should become obsolete as soon as the expectations are properly propagated
        } else if (!(object.eContainer() instanceof XCastedExpression) && object.eContainingFeature() != XbasePackage.Literals.XMEMBER_FEATURE_CALL__MEMBER_CALL_TARGET && (expectedType != null && !expectedType.isResolved() || expectedType == null && !expectation.isVoidTypeAllowed())) {
            LightweightTypeReference type = getRawTypeForName(String.class, state);
            expectation.acceptActualType(type, ConformanceFlags.UNCHECKED | ConformanceFlags.DEMAND_CONVERSION);
        } else {
            LightweightTypeReference type = getRawTypeForName(CharSequence.class, state);
            expectation.acceptActualType(type, ConformanceFlags.UNCHECKED);
        }
    }
}
Also used : ITypeComputationState(org.eclipse.xtext.xbase.typesystem.computation.ITypeComputationState) XVariableDeclaration(org.eclipse.xtext.xbase.XVariableDeclaration) XCastedExpression(org.eclipse.xtext.xbase.XCastedExpression) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XExpression(org.eclipse.xtext.xbase.XExpression) RichString(org.eclipse.xtend.core.xtend.RichString) ITypeExpectation(org.eclipse.xtext.xbase.typesystem.computation.ITypeExpectation)

Example 33 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class XtendTypeComputer method _computeTypes.

protected void _computeTypes(RichStringLiteral object, ITypeComputationState state) {
    LightweightTypeReference type = getRawTypeForName(CharSequence.class, state);
    state.acceptActualType(type);
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)

Example 34 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method getParamTypes.

protected List<JvmType> getParamTypes(JvmOperation jvmOperation, boolean wrapPrimitives) {
    List<JvmType> types = newArrayList();
    for (JvmFormalParameter p : jvmOperation.getParameters()) {
        LightweightTypeReference typeReference = toLightweightTypeReference(p.getParameterType());
        if (wrapPrimitives) {
            typeReference = typeReference.getWrapperTypeIfPrimitive();
        }
        types.add(typeReference.getType());
    }
    return types;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmType(org.eclipse.xtext.common.types.JvmType)

Example 35 with LightweightTypeReference

use of org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference in project xtext-xtend by eclipse.

the class XtendValidator method doCheckDuplicateExecutables.

protected <Executable extends IResolvedExecutable> void doCheckDuplicateExecutables(JvmGenericType inferredType, List<Executable> declaredOperations, Function<String, List<Executable>> bySignature, Set<EObject> flaggedOperations) {
    Set<Executable> processed = Sets.newHashSet();
    for (Executable declaredExecutable : declaredOperations) {
        if (!processed.contains(declaredExecutable)) {
            List<Executable> sameErasure = bySignature.apply(declaredExecutable.getResolvedErasureSignature());
            if (sameErasure.size() > 1) {
                Multimap<String, Executable> perSignature = HashMultimap.create(sameErasure.size(), 2);
                outer: for (Executable executable : sameErasure) {
                    for (LightweightTypeReference parameterType : executable.getResolvedParameterTypes()) {
                        if (parameterType.isUnknown())
                            continue outer;
                    }
                    perSignature.put(executable.getResolvedSignature(), executable);
                }
                if (perSignature.size() > 1) {
                    for (Collection<Executable> sameSignature : perSignature.asMap().values()) {
                        for (Executable operationWithSameSignature : sameSignature) {
                            JvmExecutable executable = operationWithSameSignature.getDeclaration();
                            EObject otherSource = associations.getPrimarySourceElement(executable);
                            if (flaggedOperations.add(otherSource)) {
                                if (sameSignature.size() > 1) {
                                    error("Duplicate " + typeLabel(executable) + " " + operationWithSameSignature.getSimpleSignature() + " in type " + inferredType.getSimpleName(), otherSource, nameFeature(otherSource), DUPLICATE_METHOD);
                                } else {
                                    error("The " + typeLabel(executable) + " " + operationWithSameSignature.getSimpleSignature() + " has the same erasure " + operationWithSameSignature.getResolvedErasureSignature() + " as another " + typeLabel(executable) + " in type " + inferredType.getSimpleName(), otherSource, nameFeature(otherSource), DUPLICATE_METHOD);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) EObject(org.eclipse.emf.ecore.EObject) RichString(org.eclipse.xtend.core.xtend.RichString) IResolvedExecutable(org.eclipse.xtext.xbase.typesystem.override.IResolvedExecutable) JvmExecutable(org.eclipse.xtext.common.types.JvmExecutable) XtendExecutable(org.eclipse.xtend.core.xtend.XtendExecutable)

Aggregations

LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)83 Test (org.junit.Test)28 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)23 XExpression (org.eclipse.xtext.xbase.XExpression)22 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)20 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)19 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)16 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)13 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)10 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)10 IFeatureCallArguments (org.eclipse.xtext.xbase.typesystem.arguments.IFeatureCallArguments)10 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)9 XNumberLiteral (org.eclipse.xtext.xbase.XNumberLiteral)9 JvmType (org.eclipse.xtext.common.types.JvmType)8 ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)8 EObject (org.eclipse.emf.ecore.EObject)7 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)7 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)7 RichString (org.eclipse.xtend.core.xtend.RichString)6 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)6