Search in sources :

Example 1 with ITypeReferenceOwner

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

the class MockTypeParameterSubstitutor method doVisitParameterizedTypeReference.

@Override
public LightweightTypeReference doVisitParameterizedTypeReference(final ParameterizedTypeReference reference, final Set<JvmTypeParameter> visiting) {
    final JvmType type = reference.getType();
    if ((type instanceof JvmTypeParameter)) {
        boolean _add = visiting.add(((JvmTypeParameter) type));
        boolean _not = (!_add);
        if (_not) {
            return null;
        }
        try {
            final LightweightMergedBoundTypeArgument mappedReference = this.getTypeParameterMapping().get(type);
            if ((mappedReference != null)) {
                return mappedReference.getTypeReference().<Set<JvmTypeParameter>, LightweightTypeReference>accept(this, visiting);
            } else {
                ITypeReferenceOwner _owner = this.getOwner();
                Object _object = new Object();
                final SimpleUnboundTypeReference result = new SimpleUnboundTypeReference(_owner, ((JvmTypeParameter) type), _object);
                Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> _typeParameterMapping = this.getTypeParameterMapping();
                LightweightMergedBoundTypeArgument _lightweightMergedBoundTypeArgument = new LightweightMergedBoundTypeArgument(result, VarianceInfo.INVARIANT);
                _typeParameterMapping.put(((JvmTypeParameter) type), _lightweightMergedBoundTypeArgument);
                return result;
            }
        } finally {
            visiting.remove(type);
        }
    }
    return super.doVisitParameterizedTypeReference(reference, visiting);
}
Also used : LightweightMergedBoundTypeArgument(org.eclipse.xtext.xbase.typesystem.references.LightweightMergedBoundTypeArgument) Set(java.util.Set) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) JvmType(org.eclipse.xtext.common.types.JvmType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)

Example 2 with ITypeReferenceOwner

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

the class CommonSuperTypeTest method testCommonSuperType_23.

@Test
public void testCommonSuperType_23() {
    try {
        this.function("def void m() {}");
        ITypeReferenceOwner _owner = this.getOwner();
        AnyTypeReference _anyTypeReference = new AnyTypeReference(_owner);
        ITypeReferenceOwner _owner_1 = this.getOwner();
        AnyTypeReference _anyTypeReference_1 = new AnyTypeReference(_owner_1);
        final List<LightweightTypeReference> types = CollectionLiterals.<LightweightTypeReference>newImmutableList(_anyTypeReference, _anyTypeReference_1);
        final LightweightTypeReference superType = this.getServices().getTypeConformanceComputer().getCommonSuperType(types, this.getOwner());
        Assert.assertEquals("null", superType.getSimpleName());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) AnyTypeReference(org.eclipse.xtext.xbase.typesystem.references.AnyTypeReference) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) Test(org.junit.Test)

Example 3 with ITypeReferenceOwner

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

the class ActualTypeArgumentMergeTest method mappedBy.

public Map<JvmTypeParameter, List<LightweightBoundTypeArgument>> mappedBy(final String typeParameters, final String... alternatingTypeReferences) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def ");
        {
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(typeParameters);
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                _builder.append("<");
                _builder.append(typeParameters);
                _builder.append(">");
            }
        }
        _builder.append(" void method(");
        final Function1<String, CharSequence> _function = (String it) -> {
            return it;
        };
        String _join = IterableExtensions.<String>join(((Iterable<String>) Conversions.doWrapArray(alternatingTypeReferences)), null, " p, ", " p", _function);
        _builder.append(_join);
        _builder.append(") {}");
        final String signature = _builder.toString();
        final XtendFunction function = this.function(signature.toString());
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
        EList<JvmTypeParameter> _typeParameters = operation.getTypeParameters();
        ITypeReferenceOwner _owner = this.getOwner();
        final ActualTypeArgumentCollector collector = new ActualTypeArgumentCollector(_typeParameters, BoundTypeArgumentSource.INFERRED, _owner);
        int _size = ((List<String>) Conversions.doWrapArray(alternatingTypeReferences)).size();
        int _minus = (_size - 1);
        IntegerRange _withStep = new IntegerRange(0, _minus).withStep(2);
        for (final Integer i : _withStep) {
            collector.populateTypeParameterMapping(this.toLightweightTypeReference(operation.getParameters().get((i).intValue()).getParameterType()), this.toLightweightTypeReference(operation.getParameters().get(((i).intValue() + 1)).getParameterType()));
        }
        return collector.getTypeParameterMapping();
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) IntegerRange(org.eclipse.xtext.xbase.lib.IntegerRange) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) EList(org.eclipse.emf.common.util.EList) List(java.util.List) ActualTypeArgumentCollector(org.eclipse.xtext.xbase.typesystem.util.ActualTypeArgumentCollector)

Example 4 with ITypeReferenceOwner

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

the class DispatchMethodCompileStrategy method apply.

@Override
public void apply(/* @Nullable */
ITreeAppendable a) {
    if (a == null)
        throw new IllegalArgumentException("a is never null");
    int parameterCount = dispatchOperation.getParameters().size();
    List<JvmOperation> sortedDispatchOperations = sorter.getAllDispatchCases(dispatchOperation);
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, dispatchOperation);
    boolean[] allCasesSameType = new boolean[parameterCount];
    boolean[] voidIncluded = new boolean[parameterCount];
    boolean[] notNullIncluded = new boolean[parameterCount];
    for (int i = 0; i < parameterCount; i++) {
        allCasesSameType[i] = true;
        voidIncluded[i] = false;
        notNullIncluded[i] = false;
        JvmFormalParameter dispatchParam = dispatchOperation.getParameters().get(i);
        LightweightTypeReference dispatchParamType = owner.toLightweightTypeReference(dispatchParam.getParameterType());
        for (JvmOperation operation : sortedDispatchOperations) {
            JvmFormalParameter caseParam = operation.getParameters().get(i);
            LightweightTypeReference caseParamType = owner.toLightweightTypeReference(caseParam.getParameterType());
            if (!Strings.equal(dispatchParamType.getIdentifier(), caseParamType.getIdentifier())) {
                allCasesSameType[i] = false;
            }
            if (caseParamType.isType(Void.class)) {
                voidIncluded[i] = true;
            }
            if (isSameType(dispatchParamType, caseParamType) && !dispatchParamType.isPrimitive()) {
                notNullIncluded[i] = true;
            }
        }
    }
    boolean needsElse = anyFalse(notNullIncluded) || (anyFalse(voidIncluded) && sortedDispatchOperations.size() != 1);
    for (JvmOperation operation : sortedDispatchOperations) {
        ITreeAppendable operationAppendable = treeAppendableUtil.traceSignificant(a, operation, true);
        final List<Later> laters = newArrayList();
        for (int i = 0; i < parameterCount; i++) {
            final JvmFormalParameter dispatchParam = dispatchOperation.getParameters().get(i);
            final LightweightTypeReference dispatchParamType = owner.toLightweightTypeReference(dispatchParam.getParameterType());
            final JvmFormalParameter caseParam = operation.getParameters().get(i);
            final LightweightTypeReference caseParamType = owner.toLightweightTypeReference(caseParam.getParameterType());
            final String name = getVarName(dispatchParam, operationAppendable);
            if (caseParamType.isType(Void.class)) {
                laters.add(new Later() {

                    @Override
                    public void exec(ITreeAppendable appendable) {
                        appendable.append(name).append(" == null");
                    }
                });
            } else if (!allCasesSameType[i]) {
                laters.add(new Later() {

                    @Override
                    public void exec(ITreeAppendable appendable) {
                        if (isSameType(dispatchParamType, caseParamType) && !dispatchParamType.isPrimitive()) {
                            appendable.append(name).append(" != null");
                        } else {
                            appendable.append(name).append(" instanceof ");
                            JvmType type = caseParamType.getWrapperTypeIfPrimitive().getType();
                            if (type == null) {
                                throw new IllegalStateException(String.valueOf(caseParamType));
                            }
                            appendable.append(type);
                        }
                    }
                });
            }
        }
        boolean isLast = sortedDispatchOperations.get(sortedDispatchOperations.size() - 1) == operation;
        // if it's not the first if append an 'else'
        if (sortedDispatchOperations.get(0) != operation) {
            operationAppendable.append(" else ");
        }
        if (laters.isEmpty()) {
            if (sortedDispatchOperations.size() != 1) {
                operationAppendable.append("{").increaseIndentation();
                operationAppendable.newLine();
            }
        } else {
            if (!isLast || needsElse) {
                operationAppendable.append("if (");
                operationAppendable.increaseIndentation().increaseIndentation();
                Iterator<Later> iterator = laters.iterator();
                while (iterator.hasNext()) {
                    iterator.next().exec(operationAppendable);
                    if (iterator.hasNext()) {
                        operationAppendable.newLine().append(" && ");
                    }
                }
                operationAppendable.decreaseIndentation().decreaseIndentation();
                operationAppendable.append(") {").increaseIndentation();
                operationAppendable.newLine();
            } else {
                operationAppendable.append("{").increaseIndentation().newLine();
            }
        }
        final boolean isCurrentVoid = typeReferences.is(operation.getReturnType(), Void.TYPE);
        final boolean isDispatchVoid = typeReferences.is(dispatchOperation.getReturnType(), Void.TYPE);
        if (isDispatchVoid) {
            generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
            operationAppendable.append(";");
            // we generate a redundant return statement here to get a better debugging experience
            operationAppendable.newLine().append("return;");
        } else {
            if (isCurrentVoid) {
                generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
                operationAppendable.append(";").newLine().append("return null");
            } else {
                operationAppendable.append("return ");
                generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
            }
            operationAppendable.append(";");
        }
        if (sortedDispatchOperations.size() != 1 || anyTrue(voidIncluded)) {
            operationAppendable.decreaseIndentation();
            a.newLine().append("}");
        }
    }
    if (needsElse) {
        a.append(" else {").increaseIndentation();
        a.newLine();
        a.increaseIndentation();
        a.append("throw new IllegalArgumentException(\"Unhandled parameter types: \" +").newLine();
        JvmType jvmType = typeReferences.findDeclaredType("java.util.Arrays", dispatchOperation);
        if (jvmType != null) {
            a.append(jvmType);
        } else {
            a.append(Arrays.class.getSimpleName());
        }
        a.append(".<Object>asList(");
        Iterator<JvmFormalParameter> iterator = dispatchOperation.getParameters().iterator();
        while (iterator.hasNext()) {
            JvmFormalParameter parameter = iterator.next();
            final String name = getVarName(parameter, a);
            a.append(name);
            if (iterator.hasNext()) {
                a.append(", ");
            }
        }
        a.append(").toString());");
        a.decreaseIndentation();
        a.decreaseIndentation().newLine().append("}");
    }
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmType(org.eclipse.xtext.common.types.JvmType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) Later(org.eclipse.xtext.xbase.compiler.Later) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) Arrays(java.util.Arrays) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 5 with ITypeReferenceOwner

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

the class DispatchHelper method getAllDispatchCases.

/**
 * Return all the cases that are associated with the given dispatch operation.
 */
public List<JvmOperation> getAllDispatchCases(JvmOperation dispatcherOperation) {
    DispatchSignature dispatchSignature = new DispatchSignature(dispatcherOperation.getSimpleName(), dispatcherOperation.getParameters().size());
    JvmDeclaredType type = dispatcherOperation.getDeclaringType();
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, type);
    ContextualVisibilityHelper contextualVisibilityHelper = new ContextualVisibilityHelper(visibilityHelper, owner.newParameterizedTypeReference(type));
    return getAllDispatchMethods(dispatchSignature, type, contextualVisibilityHelper);
}
Also used : ContextualVisibilityHelper(org.eclipse.xtext.xbase.typesystem.util.ContextualVisibilityHelper) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Aggregations

ITypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner)16 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)11 StandardTypeReferenceOwner (org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)11 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)6 EObject (org.eclipse.emf.ecore.EObject)4 JvmTypeParameter (org.eclipse.xtext.common.types.JvmTypeParameter)4 List (java.util.List)3 EList (org.eclipse.emf.common.util.EList)3 WrappedException (org.eclipse.emf.common.util.WrappedException)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)3 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)3 JvmType (org.eclipse.xtext.common.types.JvmType)3 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)3 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)3 ISemanticModification (org.eclipse.xtext.ui.editor.model.edit.ISemanticModification)3 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)3 ParameterizedTypeReference (org.eclipse.xtext.xbase.typesystem.references.ParameterizedTypeReference)3 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)2 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)2