Search in sources :

Example 1 with LightweightTypeReference

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

the class AbstractAssignabilityTest method isAssignableFrom.

public void isAssignableFrom(final Pair<String, String> lhsAndParams, final String rhs, final boolean expectation) {
    try {
        StringConcatenation _builder = new StringConcatenation();
        _builder.append("def ");
        {
            boolean _isNullOrEmpty = StringExtensions.isNullOrEmpty(lhsAndParams.getValue());
            boolean _not = (!_isNullOrEmpty);
            if (_not) {
                _builder.append("<");
                String _value = lhsAndParams.getValue();
                _builder.append(_value);
                _builder.append("> ");
            }
        }
        _builder.append("void method(");
        String _fixup = this.fixup(lhsAndParams.getKey());
        _builder.append(_fixup);
        _builder.append(" lhs, ");
        String _fixup_1 = this.fixup(rhs);
        _builder.append(_fixup_1);
        _builder.append(" rhs) {}");
        final String signature = _builder.toString();
        final XtendFunction function = this.function(signature.toString());
        final JvmOperation operation = this._iXtendJvmAssociations.getDirectlyInferredOperation(function);
        LightweightTypeReference _xifexpression = null;
        String _key = lhsAndParams.getKey();
        boolean _tripleNotEquals = (_key != null);
        if (_tripleNotEquals) {
            _xifexpression = this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>head(operation.getParameters()).getParameterType());
        } else {
            _xifexpression = this.getOwner().newAnyTypeReference();
        }
        final LightweightTypeReference lhsType = _xifexpression;
        LightweightTypeReference _xifexpression_1 = null;
        if ((rhs != null)) {
            _xifexpression_1 = this.toLightweightTypeReference(IterableExtensions.<JvmFormalParameter>last(operation.getParameters()).getParameterType());
        } else {
            _xifexpression_1 = this.getOwner().newAnyTypeReference();
        }
        final LightweightTypeReference rhsType = _xifexpression_1;
        String _simpleName = lhsType.getSimpleName();
        String _plus = (_simpleName + " := ");
        String _simpleName_1 = rhsType.getSimpleName();
        String _plus_1 = (_plus + _simpleName_1);
        Assert.assertEquals(_plus_1, Boolean.valueOf(expectation), Boolean.valueOf(this.testIsAssignable(lhsType, rhsType)));
        if (expectation) {
            List<LightweightTypeReference> _allSuperTypes = lhsType.getAllSuperTypes();
            for (final LightweightTypeReference superType : _allSuperTypes) {
                if (((superType.isArray() == lhsType.isArray()) || (lhsType.isArray() == rhsType.isArray()))) {
                    Assert.assertEquals(superType.toString(), Boolean.valueOf(expectation), Boolean.valueOf(this.testIsAssignable(superType, rhsType)));
                }
            }
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 2 with LightweightTypeReference

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

the class XtendGenerator method generateMembersInBody.

@Override
public ITreeAppendable generateMembersInBody(final JvmDeclaredType it, final ITreeAppendable appendable, final GeneratorConfig config) {
    ITreeAppendable _xifexpression = null;
    boolean _isLocal = it.isLocal();
    if (_isLocal) {
        ITreeAppendable _xblockexpression = null;
        {
            appendable.append("{").increaseIndentation();
            EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it));
            final AnonymousClass anonymousClass = ((AnonymousClass) _head);
            if (((!appendable.hasName(Pair.<String, JvmDeclaredType>of("this", it))) && this.needSyntheticThisVariable(anonymousClass, it))) {
                final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(anonymousClass);
                final LightweightTypeReference actualType = resolvedTypes.getActualType(anonymousClass);
                Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("this", it);
                final String thisName = appendable.declareSyntheticVariable(_mappedTo, "_this");
                appendable.newLine().append("final ").append(actualType).append(" ").append(thisName).append(" = this;");
            }
            final Function1<JvmField, Boolean> _function = (JvmField it_1) -> {
                boolean _xblockexpression_1 = false;
                {
                    Procedure1<? super ITreeAppendable> _compilationStrategy = this._jvmTypeExtensions.getCompilationStrategy(it_1);
                    boolean _tripleNotEquals = (_compilationStrategy != null);
                    if (_tripleNotEquals) {
                        return Boolean.valueOf(true);
                    } else {
                        StringConcatenationClient _compilationTemplate = this._jvmTypeExtensions.getCompilationTemplate(it_1);
                        boolean _tripleNotEquals_1 = (_compilationTemplate != null);
                        if (_tripleNotEquals_1) {
                            return Boolean.valueOf(true);
                        } else {
                            boolean _not = (!(it_1.isFinal() && it_1.isStatic()));
                            if (_not) {
                                final XExpression expression = this._iLogicalContainerProvider.getAssociatedExpression(it_1);
                                if (((expression != null) && config.isGenerateExpressions())) {
                                    return Boolean.valueOf(true);
                                }
                            }
                        }
                    }
                    _xblockexpression_1 = false;
                }
                return Boolean.valueOf(_xblockexpression_1);
            };
            final Iterable<JvmField> fieldsWithInitializer = IterableExtensions.<JvmField>filter(it.getDeclaredFields(), _function);
            boolean _isEmpty = IterableExtensions.isEmpty(fieldsWithInitializer);
            boolean _not = (!_isEmpty);
            if (_not) {
                appendable.newLine().append("{").increaseIndentation();
                final Procedure1<LoopParams> _function_1 = (LoopParams it_1) -> {
                    final Function1<ITreeAppendable, ITreeAppendable> _function_2 = (ITreeAppendable it_2) -> {
                        return it_2.newLine();
                    };
                    it_1.setSeparator(_function_2);
                };
                final Procedure1<JvmField> _function_2 = (JvmField it_1) -> {
                    final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(appendable, it_1);
                    memberAppendable.openScope();
                    appendable.newLine();
                    final ITreeAppendable tracedAppendable = appendable.trace(it_1);
                    this._treeAppendableUtil.traceSignificant(tracedAppendable, it_1).append(it_1.getSimpleName());
                    this.generateInitialization(it_1, tracedAppendable, config);
                    tracedAppendable.append(";");
                    memberAppendable.closeScope();
                };
                this._loopExtensions.<JvmField>forEach(appendable, fieldsWithInitializer, _function_1, _function_2);
                appendable.decreaseIndentation().newLine().append("}");
            }
            final Procedure1<LoopParams> _function_3 = (LoopParams it_1) -> {
                final Function1<ITreeAppendable, ITreeAppendable> _function_4 = (ITreeAppendable it_2) -> {
                    return it_2.newLine();
                };
                it_1.setSeparator(_function_4);
            };
            final Procedure1<JvmMember> _function_4 = (JvmMember it_1) -> {
                final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(appendable, it_1);
                memberAppendable.openScope();
                this.generateMember(it_1, memberAppendable, config);
                memberAppendable.closeScope();
            };
            this._loopExtensions.<JvmMember>forEach(appendable, this.getMembersToBeCompiled(it), _function_3, _function_4);
            _xblockexpression = appendable.decreaseIndentation().newLine().append("}");
        }
        _xifexpression = _xblockexpression;
    } else {
        _xifexpression = super.generateMembersInBody(it, appendable, config);
    }
    return _xifexpression;
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) IResolvedTypes(org.eclipse.xtext.xbase.typesystem.IResolvedTypes) StringConcatenationClient(org.eclipse.xtend2.lib.StringConcatenationClient) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) ITreeAppendable(org.eclipse.xtext.xbase.compiler.output.ITreeAppendable) LoopParams(org.eclipse.xtext.xbase.compiler.LoopParams) AnonymousClass(org.eclipse.xtend.core.xtend.AnonymousClass) EObject(org.eclipse.emf.ecore.EObject) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) XExpression(org.eclipse.xtext.xbase.XExpression) JvmMember(org.eclipse.xtext.common.types.JvmMember) JvmField(org.eclipse.xtext.common.types.JvmField) Pair(org.eclipse.xtext.xbase.lib.Pair)

Example 3 with LightweightTypeReference

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

the class CompilationContextImpl method toJavaCode.

@Override
public String toJavaCode(final TypeReference typeref) {
    StringBuilderBasedAppendable _xifexpression = null;
    if ((this.importManager != null)) {
        _xifexpression = new StringBuilderBasedAppendable(this.importManager);
    } else {
        _xifexpression = new StringBuilderBasedAppendable();
    }
    final StringBuilderBasedAppendable appendable = _xifexpression;
    final LightweightTypeReference typeRef = this.compilationUnit.toLightweightTypeReference(typeref);
    appendable.append(typeRef);
    return appendable.toString();
}
Also used : LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) StringBuilderBasedAppendable(org.eclipse.xtext.xbase.compiler.StringBuilderBasedAppendable)

Example 4 with LightweightTypeReference

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

the class XtendReentrantTypeResolver method getReturnTypeOfOverriddenOperation.

@Override
protected LightweightTypeReference getReturnTypeOfOverriddenOperation(JvmOperation operation, ResolvedTypes resolvedTypes, IFeatureScopeSession session) {
    if (operation.getVisibility() == JvmVisibility.PRIVATE)
        return null;
    if (InferredTypeIndicator.isInferred(operation.getReturnType())) {
        LightweightTypeReference declaringType = resolvedTypes.getActualType(operation.getDeclaringType());
        if (declaringType == null) {
            throw new IllegalStateException("Cannot determine declaring type of operation: " + operation);
        }
        BottomResolvedOperation resolvedOperation = new BottomResolvedOperation(operation, declaringType, overrideTester);
        List<IResolvedOperation> overriddenMethods = resolvedOperation.getOverriddenAndImplementedMethods();
        if (overriddenMethods.isEmpty())
            return null;
        IResolvedOperation overriddenMethod = overriddenMethods.get(0);
        JvmOperation declaration = overriddenMethod.getDeclaration();
        XExpression inferredFrom = getInferredFrom(declaration.getReturnType());
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=439535
        if (inferredFrom != null && (inferredFrom == getInferredFrom(operation.getReturnType()) || isHandled(inferredFrom))) {
            return null;
        }
        LightweightTypeReference result = overriddenMethod.getResolvedReturnType();
        return result;
    }
    return null;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) XExpression(org.eclipse.xtext.xbase.XExpression) BottomResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.BottomResolvedOperation) IResolvedOperation(org.eclipse.xtext.xbase.typesystem.override.IResolvedOperation)

Example 5 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(RichStringForLoop object, ITypeComputationState state) {
    LightweightTypeReference charSequence = getRawTypeForName(CharSequence.class, state);
    ITypeComputationState eachState = state.withExpectation(charSequence);
    JvmFormalParameter parameter = object.getDeclaredParam();
    if (parameter != null) {
        LightweightTypeReference parameterType = computeForLoopParameterType(object, state);
        eachState = eachState.assignType(parameter, parameterType);
    }
    eachState.computeTypes(object.getEachExpression());
    state.withNonVoidExpectation().computeTypes(object.getBefore());
    state.withNonVoidExpectation().computeTypes(object.getSeparator());
    state.withNonVoidExpectation().computeTypes(object.getAfter());
    LightweightTypeReference primitiveVoid = getPrimitiveVoid(state);
    state.acceptActualType(primitiveVoid);
    state.acceptActualType(charSequence);
}
Also used : ITypeComputationState(org.eclipse.xtext.xbase.typesystem.computation.ITypeComputationState) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter)

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