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);
}
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;
}
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;
}
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);
}
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;
}
}
}
}
}
}
}
}
}
Aggregations