Search in sources :

Example 31 with JvmFormalParameter

use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.

the class BoundTypeArgumentMergerTest method mergeSuccessive.

public LightweightMergedBoundTypeArgument mergeSuccessive(final Triple<String, VarianceInfo, VarianceInfo>... mergeUs) {
    try {
        int _length = mergeUs.length;
        boolean _greaterThan = (_length > 2);
        Assert.assertTrue(_greaterThan);
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def void method(");
        final Function1<Triple<String, VarianceInfo, VarianceInfo>, CharSequence> _function = (Triple<String, VarianceInfo, VarianceInfo> it) -> {
            return it.getFirst();
        };
        String _join = IterableExtensions.<Triple<String, VarianceInfo, VarianceInfo>>join(((Iterable<Triple<String, VarianceInfo, VarianceInfo>>) Conversions.doWrapArray(mergeUs)), 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);
        final ArrayList<LightweightBoundTypeArgument> mergable = CollectionLiterals.<LightweightBoundTypeArgument>newArrayList();
        final Procedure2<JvmFormalParameter, Integer> _function_1 = (JvmFormalParameter p, Integer i) -> {
            final Triple<String, VarianceInfo, VarianceInfo> input = mergeUs[(i).intValue()];
            LightweightTypeReference _lightweightTypeReference = this.toLightweightTypeReference(p.getParameterType());
            Object _object = new Object();
            VarianceInfo _second = input.getSecond();
            VarianceInfo _third = input.getThird();
            LightweightBoundTypeArgument _lightweightBoundTypeArgument = new LightweightBoundTypeArgument(_lightweightTypeReference, null, _object, _second, _third);
            mergable.add(_lightweightBoundTypeArgument);
        };
        IterableExtensions.<JvmFormalParameter>forEach(operation.getParameters(), _function_1);
        final Iterator<LightweightBoundTypeArgument> iterator = mergable.iterator();
        LightweightBoundTypeArgument first = iterator.next();
        LightweightBoundTypeArgument second = iterator.next();
        LightweightMergedBoundTypeArgument merged = this.merger.merge(Collections.<LightweightBoundTypeArgument>unmodifiableList(CollectionLiterals.<LightweightBoundTypeArgument>newArrayList(first, second)), this.getOwner());
        while (iterator.hasNext()) {
            {
                LightweightTypeReference _typeReference = merged.getTypeReference();
                Object _object = new Object();
                VarianceInfo _variance = merged.getVariance();
                VarianceInfo _variance_1 = merged.getVariance();
                LightweightBoundTypeArgument _lightweightBoundTypeArgument = new LightweightBoundTypeArgument(_typeReference, null, _object, _variance, _variance_1);
                first = _lightweightBoundTypeArgument;
                second = iterator.next();
                merged = this.merger.merge(Collections.<LightweightBoundTypeArgument>unmodifiableList(CollectionLiterals.<LightweightBoundTypeArgument>newArrayList(first, second)), this.getOwner());
            }
        }
        return merged;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) VarianceInfo(org.eclipse.xtext.xbase.typesystem.util.VarianceInfo) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) Triple(org.eclipse.xtext.util.Triple) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) LightweightMergedBoundTypeArgument(org.eclipse.xtext.xbase.typesystem.references.LightweightMergedBoundTypeArgument) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) LightweightBoundTypeArgument(org.eclipse.xtext.xbase.typesystem.references.LightweightBoundTypeArgument)

Example 32 with JvmFormalParameter

use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.

the class BoundTypeArgumentMergerTest method mergeWithSource.

public LightweightMergedBoundTypeArgument mergeWithSource(final Object source, final Triple<String, VarianceInfo, VarianceInfo>... mergeUs) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def void method(");
        final Function1<Triple<String, VarianceInfo, VarianceInfo>, CharSequence> _function = (Triple<String, VarianceInfo, VarianceInfo> it) -> {
            return it.getFirst();
        };
        String _join = IterableExtensions.<Triple<String, VarianceInfo, VarianceInfo>>join(((Iterable<Triple<String, VarianceInfo, VarianceInfo>>) Conversions.doWrapArray(mergeUs)), 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);
        final ArrayList<LightweightBoundTypeArgument> mergable = CollectionLiterals.<LightweightBoundTypeArgument>newArrayList();
        final Procedure2<JvmFormalParameter, Integer> _function_1 = (JvmFormalParameter p, Integer i) -> {
            final Triple<String, VarianceInfo, VarianceInfo> input = mergeUs[(i).intValue()];
            LightweightTypeReference _lightweightTypeReference = this.toLightweightTypeReference(p.getParameterType());
            Object _elvis = null;
            if (source != null) {
                _elvis = source;
            } else {
                Object _object = new Object();
                _elvis = _object;
            }
            VarianceInfo _second = input.getSecond();
            VarianceInfo _third = input.getThird();
            LightweightBoundTypeArgument _lightweightBoundTypeArgument = new LightweightBoundTypeArgument(_lightweightTypeReference, null, _elvis, _second, _third);
            mergable.add(_lightweightBoundTypeArgument);
        };
        IterableExtensions.<JvmFormalParameter>forEach(operation.getParameters(), _function_1);
        return this.merger.merge(mergable, this.getOwner());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) VarianceInfo(org.eclipse.xtext.xbase.typesystem.util.VarianceInfo) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) Triple(org.eclipse.xtext.util.Triple) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) LightweightBoundTypeArgument(org.eclipse.xtext.xbase.typesystem.references.LightweightBoundTypeArgument)

Example 33 with JvmFormalParameter

use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.

the class CommonSuperTypeTest method isSuperTypeOf.

public Object isSuperTypeOf(final Pair<String, String> superTypeAndParam, final String... types) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def ");
        {
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(superTypeAndParam.getValue());
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                _builder.append("<");
                String _value = superTypeAndParam.getValue();
                _builder.append(_value);
                _builder.append("> ");
            }
        }
        _builder.append("void method(");
        {
            boolean _hasElements = false;
            for (final String type : types) {
                if (!_hasElements) {
                    _hasElements = true;
                } else {
                    _builder.appendImmediate(", ", "");
                }
                String _fixup = this.fixup(type);
                _builder.append(_fixup);
                _builder.append(" t");
            }
        }
        _builder.append(") {}");
        final String signature = _builder.toString();
        final XtendFunction function = this.function(signature.toString());
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
        final Function1<JvmFormalParameter, LightweightTypeReference> _function = (JvmFormalParameter it) -> {
            return this.toLightweightTypeReference(it.getParameterType());
        };
        List<LightweightTypeReference> _map = ListExtensions.<JvmFormalParameter, LightweightTypeReference>map(operation.getParameters(), _function);
        final ArrayList<LightweightTypeReference> typeReferences = new ArrayList<LightweightTypeReference>(_map);
        final TypeConformanceComputer conformanceComputer = this.getServices().getTypeConformanceComputer();
        LightweightTypeReference computedSuperType = conformanceComputer.getCommonSuperType(typeReferences, this.getOwner());
        String _simpleName = null;
        if (computedSuperType != null) {
            _simpleName = computedSuperType.getSimpleName();
        }
        Assert.assertEquals(superTypeAndParam.getKey(), _simpleName);
        computedSuperType = this.getServices().getTypeConformanceComputer().getCommonSuperType(IterableExtensions.<LightweightTypeReference>toList(Iterables.<LightweightTypeReference>concat(typeReferences, typeReferences)), this.getOwner());
        String _simpleName_1 = null;
        if (computedSuperType != null) {
            _simpleName_1 = computedSuperType.getSimpleName();
        }
        Assert.assertEquals(superTypeAndParam.getKey(), _simpleName_1);
        computedSuperType = this.getServices().getTypeConformanceComputer().getCommonSuperType(ListExtensions.<LightweightTypeReference>reverseView(typeReferences), this.getOwner());
        String _simpleName_2 = null;
        if (computedSuperType != null) {
            _simpleName_2 = computedSuperType.getSimpleName();
        }
        Assert.assertEquals(superTypeAndParam.getKey(), _simpleName_2);
        boolean _isPrimitiveOrVoid = this.isPrimitiveOrVoid(computedSuperType);
        boolean _not_1 = (!_isPrimitiveOrVoid);
        if (_not_1) {
            List<AnyTypeReference> _newImmutableList = CollectionLiterals.<AnyTypeReference>newImmutableList(this.getOwner().newAnyTypeReference(), this.getOwner().newAnyTypeReference());
            computedSuperType = this.getServices().getTypeConformanceComputer().getCommonSuperType(IterableExtensions.<LightweightTypeReference>toList(Iterables.<LightweightTypeReference>concat(typeReferences, _newImmutableList)), this.getOwner());
            String _simpleName_3 = null;
            if (computedSuperType != null) {
                _simpleName_3 = computedSuperType.getSimpleName();
            }
            Assert.assertEquals(superTypeAndParam.getKey(), _simpleName_3);
        }
        if ((computedSuperType != null)) {
            final Procedure1<LightweightTypeReference> _function_1 = (LightweightTypeReference superType) -> {
                final Consumer<LightweightTypeReference> _function_2 = (LightweightTypeReference it) -> {
                    LightweightTypeReference _commonSuperType = conformanceComputer.getCommonSuperType(Collections.<LightweightTypeReference>unmodifiableList(CollectionLiterals.<LightweightTypeReference>newArrayList(it, superType)), superType.getOwner());
                    String _simpleName_4 = null;
                    if (_commonSuperType != null) {
                        _simpleName_4 = _commonSuperType.getSimpleName();
                    }
                    Assert.assertEquals(superTypeAndParam.getKey(), _simpleName_4);
                };
                typeReferences.forEach(_function_2);
            };
            ObjectExtensions.<LightweightTypeReference>operator_doubleArrow(computedSuperType, _function_1);
        }
        if ((computedSuperType != null)) {
            for (final LightweightTypeReference subType : typeReferences) {
                Assert.assertTrue(computedSuperType.isAssignableFrom(subType));
            }
        }
        return computedSuperType;
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) ArrayList(java.util.ArrayList) AnyTypeReference(org.eclipse.xtext.xbase.typesystem.references.AnyTypeReference) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) TypeConformanceComputer(org.eclipse.xtext.xbase.typesystem.conformance.TypeConformanceComputer) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) Consumer(java.util.function.Consumer) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 34 with JvmFormalParameter

use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.

the class DeferredTypeParameterHintCollectorTest method in.

public Map<JvmTypeParameter, LightweightMergedBoundTypeArgument> in(final String typeParameters, final String expectedType, final String actualType) {
    final JvmOperation operation = this.operation(typeParameters, expectedType, actualType);
    ITypeReferenceOwner _owner = this.getOwner();
    final DeferredTypeParameterHintCollector collector = new DeferredTypeParameterHintCollector(_owner);
    final DefaultReentrantTypeResolver resolver = this.resolverProvider.get();
    resolver.initializeFrom(EcoreUtil.getRootContainer(operation));
    ITypeReferenceOwner _owner_1 = this.getOwner();
    PublicResolvedTypes _publicResolvedTypes = new PublicResolvedTypes(resolver);
    final MockTypeParameterSubstitutor substitutor = new MockTypeParameterSubstitutor(_owner_1, _publicResolvedTypes);
    final LightweightTypeReference hasUnbounds = substitutor.substitute(this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>head(operation.getParameters()).getParameterType()));
    final LightweightTypeReference isActual = this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>last(operation.getParameters()).getParameterType());
    collector.processPairedReferences(hasUnbounds, isActual);
    return substitutor.getTypeParameterMapping();
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) MockTypeParameterSubstitutor(org.eclipse.xtend.core.tests.typesystem.MockTypeParameterSubstitutor) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) DeferredTypeParameterHintCollector(org.eclipse.xtext.xbase.typesystem.util.DeferredTypeParameterHintCollector) PublicResolvedTypes(org.eclipse.xtend.core.tests.typesystem.PublicResolvedTypes) ITypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.ITypeReferenceOwner) DefaultReentrantTypeResolver(org.eclipse.xtext.xbase.typesystem.internal.DefaultReentrantTypeResolver)

Example 35 with JvmFormalParameter

use of org.eclipse.xtext.common.types.JvmFormalParameter in project xtext-xtend by eclipse.

the class ErrorTest method testErrorModel_017.

@Test
public void testErrorModel_017() throws Exception {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("import static extension Throwables.*");
    _builder.newLine();
    _builder.append("import java.net.URI");
    _builder.newLine();
    _builder.newLine();
    _builder.append("class Throwables {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("def static <T> T propagate(()=>T proc, (Exception)=>Exception handler) {");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("try {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("proc.apply");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("} catch(Exception e) {");
    _builder.newLine();
    _builder.append("\t\t\t");
    _builder.append("throw handler.apply(e)");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    _builder.append("class Client {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("valuri = [| new URI(\'\')].propagate [ new IllegalArgumentException(it) ]");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    final XtendFile file = this.processWithoutException(_builder);
    final XtendTypeDeclaration client = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
    XtendMember _head = IterableExtensions.<XtendMember>head(client.getMembers());
    final XtendField field = ((XtendField) _head);
    XExpression _initialValue = field.getInitialValue();
    final XMemberFeatureCall initializer = ((XMemberFeatureCall) _initialValue);
    XExpression _head_1 = IterableExtensions.<XExpression>head(initializer.getMemberCallArguments());
    final XClosure closure = ((XClosure) _head_1);
    final JvmFormalParameter implicit = IterableExtensions.<JvmFormalParameter>head(closure.getImplicitFormalParameters());
    Assert.assertNotNull(implicit);
    final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(implicit);
    Assert.assertNotNull(resolvedTypes.getActualType(implicit));
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XClosure(org.eclipse.xtext.xbase.XClosure) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) XMemberFeatureCall(org.eclipse.xtext.xbase.XMemberFeatureCall) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XExpression(org.eclipse.xtext.xbase.XExpression) XtendField(org.eclipse.xtend.core.xtend.XtendField) Test(org.junit.Test)

Aggregations

JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)45 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)22 Test (org.junit.Test)17 XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)16 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)16 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)14 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)11 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)9 XExpression (org.eclipse.xtext.xbase.XExpression)8 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)7 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)7 EObject (org.eclipse.emf.ecore.EObject)5 JvmType (org.eclipse.xtext.common.types.JvmType)5 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)5 XClosure (org.eclipse.xtext.xbase.XClosure)5 IResolvedTypes (org.eclipse.xtext.xbase.typesystem.IResolvedTypes)5 DispatchHelper (org.eclipse.xtend.core.jvmmodel.DispatchHelper)4 XtendField (org.eclipse.xtend.core.xtend.XtendField)4 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)4 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)4