Search in sources :

Example 6 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class CompilationUnitImpl method toXtendMemberDeclaration.

public MemberDeclaration toXtendMemberDeclaration(final XtendMember delegate) {
    final Function1<XtendMember, XtendMemberDeclarationImpl<? extends XtendMember>> _function = (XtendMember it) -> {
        XtendMemberDeclarationImpl<? extends XtendMember> _switchResult = null;
        boolean _matched = false;
        if (delegate instanceof XtendTypeDeclaration) {
            _matched = true;
            _switchResult = this.toXtendTypeDeclaration(((XtendTypeDeclaration) delegate));
        }
        if (!_matched) {
            if (delegate instanceof XtendFunction) {
                _matched = true;
                XtendMethodDeclarationImpl _xtendMethodDeclarationImpl = new XtendMethodDeclarationImpl();
                final Procedure1<XtendMethodDeclarationImpl> _function_1 = (XtendMethodDeclarationImpl it_1) -> {
                    it_1.setDelegate(((XtendFunction) delegate));
                    it_1.setCompilationUnit(this);
                };
                _switchResult = ObjectExtensions.<XtendMethodDeclarationImpl>operator_doubleArrow(_xtendMethodDeclarationImpl, _function_1);
            }
        }
        if (!_matched) {
            if (delegate instanceof XtendConstructor) {
                _matched = true;
                XtendConstructorDeclarationImpl _xtendConstructorDeclarationImpl = new XtendConstructorDeclarationImpl();
                final Procedure1<XtendConstructorDeclarationImpl> _function_1 = (XtendConstructorDeclarationImpl it_1) -> {
                    it_1.setDelegate(((XtendConstructor) delegate));
                    it_1.setCompilationUnit(this);
                };
                _switchResult = ObjectExtensions.<XtendConstructorDeclarationImpl>operator_doubleArrow(_xtendConstructorDeclarationImpl, _function_1);
            }
        }
        if (!_matched) {
            if (delegate instanceof XtendField) {
                _matched = true;
                XtendMemberDeclarationImpl<XtendField> _xifexpression = null;
                EObject _eContainer = ((XtendField) delegate).eContainer();
                if ((_eContainer instanceof XtendAnnotationType)) {
                    XtendAnnotationTypeElementDeclarationImpl _xtendAnnotationTypeElementDeclarationImpl = new XtendAnnotationTypeElementDeclarationImpl();
                    final Procedure1<XtendAnnotationTypeElementDeclarationImpl> _function_1 = (XtendAnnotationTypeElementDeclarationImpl it_1) -> {
                        it_1.setDelegate(((XtendField) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<XtendAnnotationTypeElementDeclarationImpl>operator_doubleArrow(_xtendAnnotationTypeElementDeclarationImpl, _function_1);
                } else {
                    XtendFieldDeclarationImpl _xtendFieldDeclarationImpl = new XtendFieldDeclarationImpl();
                    final Procedure1<XtendFieldDeclarationImpl> _function_2 = (XtendFieldDeclarationImpl it_1) -> {
                        it_1.setDelegate(((XtendField) delegate));
                        it_1.setCompilationUnit(this);
                    };
                    _xifexpression = ObjectExtensions.<XtendFieldDeclarationImpl>operator_doubleArrow(_xtendFieldDeclarationImpl, _function_2);
                }
                _switchResult = _xifexpression;
            }
        }
        if (!_matched) {
            if (delegate instanceof XtendEnumLiteral) {
                _matched = true;
                XtendEnumerationValueDeclarationImpl _xtendEnumerationValueDeclarationImpl = new XtendEnumerationValueDeclarationImpl();
                final Procedure1<XtendEnumerationValueDeclarationImpl> _function_1 = (XtendEnumerationValueDeclarationImpl it_1) -> {
                    it_1.setDelegate(((XtendEnumLiteral) delegate));
                    it_1.setCompilationUnit(this);
                };
                _switchResult = ObjectExtensions.<XtendEnumerationValueDeclarationImpl>operator_doubleArrow(_xtendEnumerationValueDeclarationImpl, _function_1);
            }
        }
        return _switchResult;
    };
    return this.<XtendMember, XtendMemberDeclarationImpl<? extends XtendMember>>getOrCreate(delegate, _function);
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) XtendMethodDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendMethodDeclarationImpl) XtendMember(org.eclipse.xtend.core.xtend.XtendMember) XtendConstructor(org.eclipse.xtend.core.xtend.XtendConstructor) XtendEnumerationValueDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendEnumerationValueDeclarationImpl) XtendConstructorDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendConstructorDeclarationImpl) XtendField(org.eclipse.xtend.core.xtend.XtendField) XtendFieldDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendFieldDeclarationImpl) XtendAnnotationType(org.eclipse.xtend.core.xtend.XtendAnnotationType) Procedure1(org.eclipse.xtext.xbase.lib.Procedures.Procedure1) EObject(org.eclipse.emf.ecore.EObject) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XtendAnnotationTypeElementDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendAnnotationTypeElementDeclarationImpl) XtendEnumLiteral(org.eclipse.xtend.core.xtend.XtendEnumLiteral) XtendMemberDeclarationImpl(org.eclipse.xtend.core.macro.declaration.XtendMemberDeclarationImpl)

Example 7 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class XtendJvmModelInferrer method deriveGenericDispatchOperationSignature.

/**
 * @return a {@link JvmOperation} with common denominator argument types of all given operations
 */
/* @Nullable */
protected JvmOperation deriveGenericDispatchOperationSignature(Iterable<JvmOperation> localOperations, JvmGenericType target) {
    final Iterator<JvmOperation> iterator = localOperations.iterator();
    if (!iterator.hasNext())
        return null;
    JvmOperation first = iterator.next();
    JvmOperation result = typesFactory.createJvmOperation();
    target.getMembers().add(result);
    for (int i = 0; i < first.getParameters().size(); i++) {
        JvmFormalParameter parameter = typesFactory.createJvmFormalParameter();
        result.getParameters().add(parameter);
        parameter.setParameterType(jvmTypesBuilder.inferredType());
        JvmFormalParameter parameter2 = first.getParameters().get(i);
        parameter.setName(parameter2.getName());
    }
    jvmTypesBuilder.setBody(result, compileStrategies.forDispatcher(result));
    JvmVisibility commonVisibility = null;
    boolean isFirst = true;
    boolean allStatic = true;
    for (JvmOperation jvmOperation : localOperations) {
        Iterable<XtendFunction> xtendFunctions = Iterables.filter(associations.getSourceElements(jvmOperation), XtendFunction.class);
        for (XtendFunction func : xtendFunctions) {
            JvmVisibility xtendVisibility = func.getDeclaredVisibility();
            if (isFirst) {
                commonVisibility = xtendVisibility;
                isFirst = false;
            } else if (commonVisibility != xtendVisibility) {
                commonVisibility = null;
            }
            associator.associate(func, result);
            if (!func.isStatic())
                allStatic = false;
        }
        for (JvmTypeReference declaredException : jvmOperation.getExceptions()) result.getExceptions().add(jvmTypesBuilder.cloneWithProxies(declaredException));
    }
    if (commonVisibility == null)
        result.setVisibility(JvmVisibility.PUBLIC);
    else
        result.setVisibility(commonVisibility);
    result.setStatic(allStatic);
    return result;
}
Also used : JvmOperation(org.eclipse.xtext.common.types.JvmOperation) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility) JvmTypeConstraint(org.eclipse.xtext.common.types.JvmTypeConstraint)

Example 8 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class XtendQualifiedNameProvider method getFullyQualifiedName.

@Override
public QualifiedName getFullyQualifiedName(EObject obj) {
    if (obj instanceof XtendTypeDeclaration) {
        XtendTypeDeclaration typeDecl = (XtendTypeDeclaration) obj;
        String typeName = typeDecl.getName();
        if (typeDecl.eContainer() instanceof XtendFile) {
            if (typeName == null)
                return null;
            XtendFile file = (XtendFile) typeDecl.eContainer();
            String packageName = file.getPackage();
            if (packageName != null) {
                return qualifiedNameConverter.toQualifiedName(packageName).append(typeName);
            }
            return QualifiedName.create(typeName);
        } else {
            return concatNames(obj, typeName);
        }
    }
    if (obj instanceof XtendConstructor) {
        return getFullyQualifiedName(obj.eContainer());
    }
    if (obj instanceof JvmIdentifiableElement && !(obj instanceof JvmTypeParameter)) {
        return getFullyQualifiedName((JvmIdentifiableElement) obj);
    }
    if (obj instanceof XtendField) {
        return concatNames(obj, ((XtendField) obj).getName());
    }
    if (obj instanceof XtendFunction) {
        return concatNames(obj, ((XtendFunction) obj).getName());
    }
    if (obj instanceof XtendEnumLiteral) {
        return concatNames(obj, ((XtendEnumLiteral) obj).getName());
    }
    return null;
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmIdentifiableElement(org.eclipse.xtext.common.types.JvmIdentifiableElement) XtendConstructor(org.eclipse.xtend.core.xtend.XtendConstructor) JvmTypeParameter(org.eclipse.xtext.common.types.JvmTypeParameter) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XtendEnumLiteral(org.eclipse.xtend.core.xtend.XtendEnumLiteral) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 9 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class XtendReentrantTypeResolver method _doPrepare.

@Override
protected void _doPrepare(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, JvmOperation operation, Map<JvmIdentifiableElement, ResolvedTypes> resolvedTypesByContext) {
    super._doPrepare(resolvedTypes, featureScopeSession, operation, resolvedTypesByContext);
    resolvedTypes = resolvedTypesByContext.get(operation);
    if (dispatchHelper.isDispatcherFunction(operation)) {
        List<JvmFormalParameter> parameters = operation.getParameters();
        for (int i = 0; i < parameters.size(); i++) {
            JvmFormalParameter parameter = parameters.get(i);
            JvmTypeReference parameterType = parameter.getParameterType();
            if (InferredTypeIndicator.isInferred(parameterType)) {
                XComputedTypeReference casted = (XComputedTypeReference) parameterType;
                XComputedTypeReference computedParameterType = getServices().getXtypeFactory().createXComputedTypeReference();
                computedParameterType.setTypeProvider(new DispatchParameterTypeReferenceProvider(operation, i, resolvedTypes, featureScopeSession, this));
                casted.setEquivalent(computedParameterType);
            } else if (parameterType == null) {
                XComputedTypeReference computedParameterType = getServices().getXtypeFactory().createXComputedTypeReference();
                computedParameterType.setTypeProvider(new DispatchParameterTypeReferenceProvider(operation, i, resolvedTypes, featureScopeSession, this));
                parameter.setParameterType(computedParameterType);
            }
        }
    } else if (operation.getParameters().size() >= 1) {
        EObject sourceElement = associations.getPrimarySourceElement(operation);
        if (sourceElement instanceof XtendFunction) {
            XtendFunction function = (XtendFunction) sourceElement;
            if (function.getCreateExtensionInfo() != null) {
                JvmFormalParameter firstParameter = operation.getParameters().get(0);
                JvmTypeReference parameterType = firstParameter.getParameterType();
                if (InferredTypeIndicator.isInferred(parameterType)) {
                    XComputedTypeReference casted = (XComputedTypeReference) parameterType;
                    XComputedTypeReference computedParameterType = getServices().getXtypeFactory().createXComputedTypeReference();
                    computedParameterType.setTypeProvider(new InitializerParameterTypeReferenceProvider(firstParameter, function, resolvedTypesByContext, resolvedTypes, featureScopeSession, this));
                    casted.setEquivalent(computedParameterType);
                }
            }
        }
    }
    doPrepareLocalTypes(resolvedTypes, featureScopeSession, operation, resolvedTypesByContext);
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) EObject(org.eclipse.emf.ecore.EObject) XComputedTypeReference(org.eclipse.xtext.xtype.XComputedTypeReference)

Example 10 with XtendFunction

use of org.eclipse.xtend.core.xtend.XtendFunction in project xtext-xtend by eclipse.

the class XtendValidator method checkDispatchFunctions.

@Check
public void checkDispatchFunctions(XtendClass clazz) {
    JvmGenericType type = associations.getInferredType(clazz);
    if (type != null) {
        Multimap<DispatchHelper.DispatchSignature, JvmOperation> dispatchMethods = dispatchHelper.getDeclaredOrEnhancedDispatchMethods(type);
        checkDispatchNonDispatchConflict(clazz, dispatchMethods);
        for (DispatchHelper.DispatchSignature signature : dispatchMethods.keySet()) {
            Collection<JvmOperation> dispatchOperations = dispatchMethods.get(signature);
            JvmOperation syntheticDispatchMethod = dispatchHelper.getDispatcherOperation(type, signature);
            if (syntheticDispatchMethod != null) {
                JvmOperation overriddenOperation = overrideHelper.findOverriddenOperation(syntheticDispatchMethod);
                Boolean expectStatic = null;
                if (overriddenOperation != null) {
                    if (isMorePrivateThan(syntheticDispatchMethod.getVisibility(), overriddenOperation.getVisibility())) {
                        String msg = "Synthetic dispatch method reduces visibility of overridden method " + overriddenOperation.getIdentifier();
                        addDispatchError(type, dispatchOperations, msg, null, OVERRIDE_REDUCES_VISIBILITY);
                    }
                    expectStatic = overriddenOperation.isStatic();
                }
                LightweightTypeReference dispatchMethodReturnType = getActualType(clazz, syntheticDispatchMethod);
                if (dispatchOperations.size() == 1) {
                    JvmOperation singleOp = dispatchOperations.iterator().next();
                    XtendFunction function = associations.getXtendFunction(singleOp);
                    addIssue("Single dispatch method.", function, XTEND_MEMBER__MODIFIERS, function.getModifiers().indexOf("dispatch"), SINGLE_DISPATCH_FUNCTION);
                } else {
                    Multimap<List<JvmType>, JvmOperation> signatures = HashMultimap.create();
                    boolean[] allPrimitive = new boolean[signature.getArity()];
                    Arrays.fill(allPrimitive, true);
                    boolean isFirstLocalOperation = true;
                    JvmVisibility commonVisibility = null;
                    Boolean commonStatic = null;
                    for (JvmOperation jvmOperation : dispatchOperations) {
                        signatures.put(getParamTypes(jvmOperation, true), jvmOperation);
                        for (int i = 0; i < jvmOperation.getParameters().size(); i++) {
                            JvmFormalParameter parameter = jvmOperation.getParameters().get(i);
                            if (!(parameter.getParameterType().getType() instanceof JvmPrimitiveType)) {
                                allPrimitive[i] = false;
                            }
                        }
                        if (jvmOperation.getDeclaringType() == type) {
                            if (expectStatic != null) {
                                if (expectStatic && !jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must be static because the dispatch methods in the superclass are static.";
                                    addDispatchError(jvmOperation, msg, "static", DISPATCH_FUNCTIONS_STATIC_EXPECTED);
                                }
                                if (!expectStatic && jvmOperation.isStatic()) {
                                    String msg = "The dispatch method must not be static because the dispatch methods in the superclass are not static.";
                                    addDispatchError(jvmOperation, msg, "static", DISPATCH_FUNCTIONS_NON_STATIC_EXPECTED);
                                }
                            }
                            if (isFirstLocalOperation) {
                                commonVisibility = jvmOperation.getVisibility();
                                commonStatic = jvmOperation.isStatic();
                                isFirstLocalOperation = false;
                            } else {
                                if (jvmOperation.getVisibility() != commonVisibility) {
                                    commonVisibility = null;
                                }
                                if (commonStatic != null && commonStatic != jvmOperation.isStatic()) {
                                    commonStatic = null;
                                }
                            }
                            // TODO move validation to type computation
                            if (dispatchMethodReturnType != null) {
                                XtendFunction function = associations.getXtendFunction(jvmOperation);
                                if (function != null) {
                                    LightweightTypeReference operationType = getActualType(function.getExpression(), jvmOperation);
                                    if (!dispatchMethodReturnType.isAssignableFrom(operationType)) {
                                        error("Incompatible return type of dispatch method. Expected " + dispatchMethodReturnType.getHumanReadableName() + " but was " + operationType.getHumanReadableName(), function, XtendPackage.Literals.XTEND_FUNCTION__RETURN_TYPE, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INCOMPATIBLE_RETURN_TYPE);
                                    }
                                }
                            }
                        }
                    }
                    if (commonVisibility == null) {
                        addDispatchError(type, dispatchOperations, "All local dispatch methods must have the same visibility.", null, DISPATCH_FUNCTIONS_WITH_DIFFERENT_VISIBILITY);
                    }
                    if (expectStatic == null && commonStatic == null) {
                        addDispatchError(type, dispatchOperations, "Static and non-static dispatch methods can not be mixed.", "static", DISPATCH_FUNCTIONS_MIXED_STATIC_AND_NON_STATIC);
                    }
                    for (final List<JvmType> paramTypes : signatures.keySet()) {
                        Collection<JvmOperation> ops = signatures.get(paramTypes);
                        if (ops.size() > 1) {
                            if (Iterables.any(ops, new Predicate<JvmOperation>() {

                                @Override
                                public boolean apply(JvmOperation input) {
                                    return !getParamTypes(input, false).equals(paramTypes);
                                }
                            })) {
                                for (JvmOperation jvmOperation : ops) {
                                    XtendFunction function = associations.getXtendFunction(jvmOperation);
                                    error("Duplicate dispatch methods. Primitives cannot overload their wrapper types in dispatch methods.", function, null, DUPLICATE_METHOD);
                                }
                            }
                        }
                    }
                    for (int i = 0; i < allPrimitive.length; i++) {
                        if (allPrimitive[i]) {
                            Iterator<JvmOperation> operationIter = dispatchOperations.iterator();
                            JvmType paramType1 = operationIter.next().getParameters().get(i).getParameterType().getType();
                            while (operationIter.hasNext()) {
                                JvmType paramType2 = operationIter.next().getParameters().get(i).getParameterType().getType();
                                if (!paramType2.equals(paramType1)) {
                                    for (JvmOperation jvmOperation : dispatchOperations) {
                                        XtendFunction function = associations.getXtendFunction(jvmOperation);
                                        addIssue("Dispatch methods have arguments with different primitive types.", function, XTEND_EXECUTABLE__PARAMETERS, i, DISPATCH_FUNCTIONS_DIFFERENT_PRIMITIVE_ARGS);
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) LightweightTypeReference(org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) RichString(org.eclipse.xtend.core.xtend.RichString) JvmType(org.eclipse.xtext.common.types.JvmType) JvmOperation(org.eclipse.xtext.common.types.JvmOperation) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) JvmPrimitiveType(org.eclipse.xtext.common.types.JvmPrimitiveType) JvmVisibility(org.eclipse.xtext.common.types.JvmVisibility) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) DispatchHelper(org.eclipse.xtend.core.jvmmodel.DispatchHelper) Check(org.eclipse.xtext.validation.Check)

Aggregations

XtendFunction (org.eclipse.xtend.core.xtend.XtendFunction)441 Test (org.junit.Test)374 XtendClass (org.eclipse.xtend.core.xtend.XtendClass)248 XBlockExpression (org.eclipse.xtext.xbase.XBlockExpression)198 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)182 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)116 JvmOperation (org.eclipse.xtext.common.types.JvmOperation)101 XExpression (org.eclipse.xtext.xbase.XExpression)95 XtendMember (org.eclipse.xtend.core.xtend.XtendMember)91 XMemberFeatureCall (org.eclipse.xtext.xbase.XMemberFeatureCall)78 XFeatureCall (org.eclipse.xtext.xbase.XFeatureCall)72 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)60 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)46 XtendTypeDeclaration (org.eclipse.xtend.core.xtend.XtendTypeDeclaration)39 JvmGenericType (org.eclipse.xtext.common.types.JvmGenericType)30 EObject (org.eclipse.emf.ecore.EObject)28 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)23 LightweightTypeReference (org.eclipse.xtext.xbase.typesystem.references.LightweightTypeReference)23 RichString (org.eclipse.xtend.core.xtend.RichString)17 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)16