Search in sources :

Example 6 with JvmFormalParameter

use of org.eclipse.xtext.common.types.JvmFormalParameter 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)

Example 7 with JvmFormalParameter

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

the class MutableJvmParameterDeclarationImpl method setSimpleName.

@Override
public void setSimpleName(final String name) {
    this.checkMutable();
    ConditionUtils.checkJavaIdentifier(name, "name");
    JvmFormalParameter _delegate = this.getDelegate();
    _delegate.setName(name);
}
Also used : JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter)

Example 8 with JvmFormalParameter

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

the class ProblemSupportImpl method getSignificantFeature.

public EStructuralFeature getSignificantFeature(final EObject obj) {
    EStructuralFeature _switchResult = null;
    boolean _matched = false;
    if (obj instanceof XtendTypeDeclaration) {
        _matched = true;
        _switchResult = XtendPackage.eINSTANCE.getXtendTypeDeclaration_Name();
    }
    if (!_matched) {
        if (obj instanceof XtendField) {
            _matched = true;
            _switchResult = XtendPackage.eINSTANCE.getXtendField_Name();
        }
    }
    if (!_matched) {
        if (obj instanceof XtendFunction) {
            _matched = true;
            _switchResult = XtendPackage.eINSTANCE.getXtendFunction_Name();
        }
    }
    if (!_matched) {
        if (obj instanceof JvmFormalParameter) {
            _matched = true;
            _switchResult = TypesPackage.eINSTANCE.getJvmFormalParameter_Name();
        }
    }
    if (!_matched) {
        if (obj instanceof XAnnotation) {
            _matched = true;
            _switchResult = XAnnotationsPackage.eINSTANCE.getXAnnotation_AnnotationType();
        }
    }
    return _switchResult;
}
Also used : XtendFunction(org.eclipse.xtend.core.xtend.XtendFunction) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) XAnnotation(org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation) XtendField(org.eclipse.xtend.core.xtend.XtendField)

Example 9 with JvmFormalParameter

use of org.eclipse.xtext.common.types.JvmFormalParameter 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");
    boolean needsElse = true;
    int parameterCount = dispatchOperation.getParameters().size();
    List<JvmOperation> sortedDispatchOperations = sorter.getAllDispatchCases(dispatchOperation);
    boolean[] allCasesSameType = new boolean[parameterCount];
    for (int i = 0; i < parameterCount; i++) {
        allCasesSameType[i] = true;
        JvmTypeReference dispatchParameterType = dispatchOperation.getParameters().get(i).getParameterType();
        for (JvmOperation operation : sortedDispatchOperations) {
            JvmFormalParameter parameter = operation.getParameters().get(i);
            JvmTypeReference caseParameterType = parameter.getParameterType();
            if (!Strings.equal(dispatchParameterType.getIdentifier(), caseParameterType.getIdentifier())) {
                allCasesSameType[i] = false;
                break;
            }
        }
    }
    ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, dispatchOperation);
    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 (caseParamType.isAssignableFrom(dispatchParamType, new TypeConformanceComputationArgument(true, false, true, true, false, false)) && !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);
                        }
                    }
                });
            }
        }
        // if it's not the first if append an 'else'
        if (sortedDispatchOperations.get(0) != operation) {
            operationAppendable.append(" else ");
        }
        if (laters.isEmpty()) {
            needsElse = false;
            if (sortedDispatchOperations.size() != 1) {
                operationAppendable.append("{").increaseIndentation();
                operationAppendable.newLine();
            }
        } else {
            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();
        }
        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);
            // we generate a redundant return statement here to get a better debugging experience
            operationAppendable.append(";").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) {
            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) TypeConformanceComputationArgument(org.eclipse.xtext.xbase.typesystem.conformance.TypeConformanceComputationArgument) 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) JvmTypeReference(org.eclipse.xtext.common.types.JvmTypeReference) Arrays(java.util.Arrays) StandardTypeReferenceOwner(org.eclipse.xtext.xbase.typesystem.references.StandardTypeReferenceOwner)

Example 10 with JvmFormalParameter

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

the class XtendJvmModelInferrer method translateParameter.

protected void translateParameter(JvmExecutable executable, XtendParameter parameter) {
    JvmFormalParameter jvmParam = typesFactory.createJvmFormalParameter();
    jvmParam.setName(parameter.getName());
    if (parameter.isVarArg()) {
        executable.setVarArgs(true);
        JvmGenericArrayTypeReference arrayType = typeReferences.createArrayType(jvmTypesBuilder.cloneWithProxies(parameter.getParameterType()));
        jvmParam.setParameterType(arrayType);
    } else {
        jvmParam.setParameterType(jvmTypesBuilder.cloneWithProxies(parameter.getParameterType()));
    }
    associator.associate(parameter, jvmParam);
    translateAnnotationsTo(parameter.getAnnotations(), jvmParam);
    if (parameter.isExtension() && typeReferences.findDeclaredType(Extension.class, parameter) != null) {
        jvmParam.getAnnotations().add(_annotationTypesBuilder.annotationRef(Extension.class));
    }
    executable.getParameters().add(jvmParam);
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) JvmGenericArrayTypeReference(org.eclipse.xtext.common.types.JvmGenericArrayTypeReference) JvmFormalParameter(org.eclipse.xtext.common.types.JvmFormalParameter)

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