org.eclipse.xtext.xbase.XReturnExpression Java Examples

The following examples show how to use org.eclipse.xtext.xbase.XReturnExpression. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EarlyExitValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkInvalidReturnExpression(XExpression expression) {
	final EReference contFeature = (EReference) expression.eContainingFeature();
	final Map<EReference, EarlyExitKind> map = getDisallowedEarlyExitReferences();
	if (map.containsKey(contFeature)) {
		EarlyExitKind exitKind = map.get(contFeature);
		List<XExpression> returns = newArrayList();
		collectExits(expression, returns);
		for (XExpression expr : returns) {
			if (expr instanceof XReturnExpression && (exitKind == EarlyExitKind.RETURN || exitKind == EarlyExitKind.BOTH)) {
				error("A return expression is not allowed in this context.", expr, null, IssueCodes.INVALID_EARLY_EXIT);
			}
			if (expr instanceof XThrowExpression && (exitKind == EarlyExitKind.THROW || exitKind == EarlyExitKind.BOTH)) {
				error("A throw expression is not allowed in this context.", expr, null, IssueCodes.INVALID_EARLY_EXIT);
			}
		}
	}
}
 
Example #2
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void checkValidReturn(XReturnExpression object, ITypeComputationState state) {
	// if the expectation comes from a method's return type
	// then it is legal, thus we must check if the return is
	// contained in a throw expression
	if (hasThrowableExpectation(state) &&
			EcoreUtil2.getContainerOfType(object, XThrowExpression.class) != null) {
		state.addDiagnostic(new EObjectDiagnosticImpl(
				Severity.ERROR,
				IssueCodes.INVALID_RETURN,
				"Invalid return inside throw.",
				object,
				null,
				-1,
				new String[] { 
				}));
	}
}
 
Example #3
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void checkValidReturnExpression(XExpression returnValue, ITypeComputationState expressionState) {
	ITypeComputationResult result = expressionState.computeTypes(returnValue);
	LightweightTypeReference actualType = result.getActualExpressionType();
	int conformanceFlags = result.getConformanceFlags();
	if (actualType.isPrimitiveVoid() && (conformanceFlags & ConformanceFlags.NO_IMPLICIT_RETURN) != 0) {
		String message = "Invalid return's expression.";
		if (returnValue instanceof XReturnExpression) {
			// when the return's expression is directory a return
			// we provide a more detailed error
			message = "Return cannot be nested.";
		}
		expressionState.addDiagnostic(new EObjectDiagnosticImpl(
				Severity.ERROR,
				IssueCodes.INVALID_RETURN,
				message,
				returnValue,
				null,
				-1,
				new String[] { 
				}));
	}
}
 
Example #4
Source File: TypeProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testReturnTypeInConstructor_02() throws Exception {
	XtendConstructor constructor = constructor(
			"new() {\n" + 
			"	return ''.toString\n" + 
			"}\n", false);
	XBlockExpression body = (XBlockExpression) constructor.getExpression();
	assertEquals("void", getExpectedType(body).getIdentifier());
	assertEquals("void", getExpectedReturnType(body).getIdentifier());
	XReturnExpression returnExpression = (XReturnExpression) body.getExpressions().get(0);
	XMemberFeatureCall toString = (XMemberFeatureCall) returnExpression.getExpression();
	assertEquals("void", getExpectedType(toString).getIdentifier());
	assertEquals("void", getExpectedReturnType(toString).getIdentifier());
}
 
Example #5
Source File: XbaseFormatter2.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final XReturnExpression expr, final FormattableDocument format) {
  final Procedure1<FormattingDataInit> _function = (FormattingDataInit it) -> {
    it.oneSpace();
  };
  Function1<? super FormattableDocument, ? extends Iterable<FormattingData>> _prepend = this._formattingDataFactory.prepend(this._nodeModelAccess.nodeForEObject(expr.getExpression()), _function);
  format.operator_add(_prepend);
  this.format(expr.getExpression(), format);
}
 
Example #6
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> exitPoints(final XExpression expression) {
  if (expression instanceof XDoWhileExpression) {
    return _exitPoints((XDoWhileExpression)expression);
  } else if (expression instanceof XWhileExpression) {
    return _exitPoints((XWhileExpression)expression);
  } else if (expression instanceof XAbstractFeatureCall) {
    return _exitPoints((XAbstractFeatureCall)expression);
  } else if (expression instanceof XBasicForLoopExpression) {
    return _exitPoints((XBasicForLoopExpression)expression);
  } else if (expression instanceof XBlockExpression) {
    return _exitPoints((XBlockExpression)expression);
  } else if (expression instanceof XConstructorCall) {
    return _exitPoints((XConstructorCall)expression);
  } else if (expression instanceof XForLoopExpression) {
    return _exitPoints((XForLoopExpression)expression);
  } else if (expression instanceof XIfExpression) {
    return _exitPoints((XIfExpression)expression);
  } else if (expression instanceof XReturnExpression) {
    return _exitPoints((XReturnExpression)expression);
  } else if (expression instanceof XSwitchExpression) {
    return _exitPoints((XSwitchExpression)expression);
  } else if (expression instanceof XSynchronizedExpression) {
    return _exitPoints((XSynchronizedExpression)expression);
  } else if (expression instanceof XThrowExpression) {
    return _exitPoints((XThrowExpression)expression);
  } else if (expression instanceof XTryCatchFinallyExpression) {
    return _exitPoints((XTryCatchFinallyExpression)expression);
  } else if (expression instanceof XVariableDeclaration) {
    return _exitPoints((XVariableDeclaration)expression);
  } else if (expression != null) {
    return _exitPoints(expression);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(expression).toString());
  }
}
 
Example #7
Source File: AbstractBatchReturnTypeTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
public boolean hasReturnExpression(final XExpression expression) {
  boolean _switchResult = false;
  boolean _matched = false;
  if (expression instanceof XReturnExpression) {
    _matched=true;
    _switchResult = true;
  }
  if (!_matched) {
    if (expression instanceof XThrowExpression) {
      _matched=true;
      _switchResult = true;
    }
  }
  if (!_matched) {
    if (expression instanceof XClosure) {
      _matched=true;
      _switchResult = false;
    }
  }
  if (!_matched) {
    final Function1<EObject, Boolean> _function = (EObject content) -> {
      boolean _switchResult_1 = false;
      boolean _matched_1 = false;
      if (content instanceof XExpression) {
        _matched_1=true;
        _switchResult_1 = this.hasReturnExpression(((XExpression)content));
      }
      if (!_matched_1) {
        _switchResult_1 = false;
      }
      return Boolean.valueOf(_switchResult_1);
    };
    _switchResult = IterableExtensions.<EObject>exists(expression.eContents(), _function);
  }
  return _switchResult;
}
 
Example #8
Source File: XbaseFormatter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _format(final XReturnExpression expr, @Extension final IFormattableDocument format) {
  final Procedure1<IHiddenRegionFormatter> _function = (IHiddenRegionFormatter it) -> {
    it.oneSpace();
  };
  format.<XExpression>prepend(expr.getExpression(), _function);
  format.<XExpression>format(expr.getExpression());
}
 
Example #9
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testReturnExpressionInBlock_1() throws Exception {
	XBlockExpression block = (XBlockExpression) expression(
		"{ return 1 2 }");
	assertEquals(2, block.getExpressions().size());
	assertTrue(block.getExpressions().get(0) instanceof XReturnExpression);
	XReturnExpression returnExpression = (XReturnExpression) block.getExpressions().get(0);
	assertTrue(returnExpression.getExpression() instanceof XNumberLiteral);
}
 
Example #10
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testReturnExpressionInBlock_2() throws Exception {
	XBlockExpression block = (XBlockExpression) expression(
		"{ return }");
	assertEquals(1, block.getExpressions().size());
	assertTrue(block.getExpressions().get(0) instanceof XReturnExpression);
	XReturnExpression returnExpression = (XReturnExpression) block.getExpressions().get(0);
	assertNull(returnExpression.getExpression());
}
 
Example #11
Source File: XbaseParserTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testReturnExpressionInBlock_3() throws Exception {
	XBlockExpression block = (XBlockExpression) expression(
		"{ return; 1 2 }");
	assertEquals(3, block.getExpressions().size());
	assertTrue(block.getExpressions().get(0) instanceof XReturnExpression);
	XReturnExpression returnExpression = (XReturnExpression) block.getExpressions().get(0);
	assertNull(returnExpression.getExpression());
}
 
Example #12
Source File: AbstractExpectationTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFeatureCallVarArgument_04() {
  ExpectationTestingTypeComputer _typeComputer = this.getTypeComputer();
  final Function1<XExpression, Boolean> _function = (XExpression it) -> {
    return Boolean.valueOf((it instanceof XReturnExpression));
  };
  _typeComputer.setPredicate(_function);
  this.expects("new foo.ClassWithGenericMethod().genericMethod(return null)").types("Unbound[T]").finalizedAs("Object");
}
 
Example #13
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void _toJavaStatement(XReturnExpression expr, ITreeAppendable b, boolean isReferenced) {
	if (expr.getExpression()!=null) {
		internalToJavaStatement(expr.getExpression(), b, true);
		b.newLine().append("return ");
		LightweightTypeReference returnTypeToCompile = findRealReturnType(expr);
		internalToConvertedExpression(expr.getExpression(), b, returnTypeToCompile);
		b.append(";");
	} else {
		b.newLine().append("return;");
	}
}
 
Example #14
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected void internalToConvertedExpression(XExpression obj, ITreeAppendable appendable) {
	if (obj instanceof XBlockExpression) {
		_toJavaExpression((XBlockExpression) obj, appendable);
	} else if (obj instanceof XCastedExpression) {
		_toJavaExpression((XCastedExpression) obj, appendable);
	} else if (obj instanceof XClosure) {
		_toJavaExpression((XClosure) obj, appendable);
	} else if (obj instanceof XAnnotation) {
		_toJavaExpression((XAnnotation) obj, appendable);
	} else if (obj instanceof XConstructorCall) {
		_toJavaExpression((XConstructorCall) obj, appendable);
	} else if (obj instanceof XIfExpression) {
		_toJavaExpression((XIfExpression) obj, appendable);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaExpression((XInstanceOfExpression) obj, appendable);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaExpression((XSwitchExpression) obj, appendable);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaExpression((XTryCatchFinallyExpression) obj, appendable);
	} else if (obj instanceof XListLiteral) {
		_toJavaExpression((XListLiteral) obj, appendable);
	} else if (obj instanceof XSetLiteral) {
		_toJavaExpression((XSetLiteral) obj, appendable);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaExpression((XSynchronizedExpression) obj, appendable);
	} else if (obj instanceof XReturnExpression) {
		_toJavaExpression((XReturnExpression) obj, appendable);
	} else if (obj instanceof XThrowExpression) {
		_toJavaExpression((XThrowExpression) obj, appendable);
	} else {
		super.internalToConvertedExpression(obj, appendable);
	}
}
 
Example #15
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkNoReturnsInCreateExtensions(XtendFunction func) {
	if (func.getCreateExtensionInfo() == null)
		return;
	List<XReturnExpression> found = newArrayList();
	collectReturnExpressions(func.getCreateExtensionInfo().getCreateExpression(), found);
	for (XReturnExpression xReturnExpression : found) {
		error("Return is not allowed in creation expression", xReturnExpression, null, INVALID_EARLY_EXIT);
	}
}
 
Example #16
Source File: XbaseTypeComputer.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _computeTypes(XReturnExpression object, ITypeComputationState state) {
	XExpression returnValue = object.getExpression();
	ITypeComputationState expressionState = state.withReturnExpectation();

	checkValidReturn(object, state);
	
	LightweightTypeReference primitiveVoid = getPrimitiveVoid(state);
	if (returnValue != null) {
		checkValidReturnExpression(returnValue, expressionState);
		state.acceptActualType(primitiveVoid, ConformanceFlags.NO_IMPLICIT_RETURN);
	} else {
		state.acceptActualType(primitiveVoid, ConformanceFlags.EXPLICIT_VOID_RETURN);
		state.acceptActualType(primitiveVoid, ConformanceFlags.NO_IMPLICIT_RETURN);
	}
}
 
Example #17
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void collectReturnExpressions(EObject expr, List<XReturnExpression> found) {
	if (expr instanceof XReturnExpression) {
		found.add((XReturnExpression) expr);
	} else if (expr instanceof XClosure) {
		return;
	}
	for (EObject child : expr.eContents()) {
		collectReturnExpressions(child, found);
	}
}
 
Example #18
Source File: EarlyExitValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void collectExits(EObject expr, List<XExpression> found) {
	if (expr instanceof XReturnExpression) {
		found.add((XExpression) expr);
	} else if (expr instanceof XThrowExpression) {
		found.add((XExpression) expr);
	} else if (expr instanceof XClosure) {
		return;
	}
	for (EObject child : expr.eContents()) {
		collectExits(child, found);
	}
}
 
Example #19
Source File: CheckJavaValidator.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks that return expressions do not occur in check implementations.
 *
 * @param expression
 *          the expression
 */
@Check
public void checkReturnExpressions(final XReturnExpression expression) {
  if (expression.getExpression() != null) {
    error(Messages.CheckJavaValidator_NO_RETURN_IN_CHECK_IMPL, XbasePackage.Literals.XRETURN_EXPRESSION__EXPRESSION, IssueCodes.RETURN_IN_IMPL);
  }
}
 
Example #20
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Check
public void checkReturn(XReturnExpression expr) {
	XExpression returnedExpression = expr.getExpression();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(expr);
	LightweightTypeReference expectedReturnType = resolvedTypes.getExpectedReturnType(expr);
	if (expectedReturnType == null) {
		return;
	}
	if (expectedReturnType.isPrimitiveVoid()) {
		if (returnedExpression != null)
			error("Void functions cannot return a value.", expr, null,
					ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_RETURN);
	} else {
		if (returnedExpression == null)
			error("The function must return a result of type " + expectedReturnType.getHumanReadableName() + ".", expr, null,
					ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INVALID_RETURN);
		else {
			LightweightTypeReference expressionType = getActualType(returnedExpression);
			if (expressionType.isPrimitiveVoid()) {
				error("Incompatible types. Expected " + getNameOfTypes(expectedReturnType) + " but was "
						+ canonicalName(expressionType), returnedExpression, null,
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX, INCOMPATIBLE_TYPES);
			}
		}

	}
}
 
Example #21
Source File: JavaInlineExpressionCompiler.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static XExpression filterSingleOperation(XExpression expression) {
	XExpression content = expression;
	while (content instanceof XBlockExpression) {
		final XBlockExpression blockExpr = (XBlockExpression) content;
		if (blockExpr.getExpressions().size() == 1) {
			content = blockExpr.getExpressions().get(0);
		} else {
			content = null;
		}
	}
	if (content instanceof XReturnExpression) {
		content = ((XReturnExpression) content).getExpression();
	}
	return content;
}
 
Example #22
Source File: ExtractMethodRefactoring.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(final IProgressMonitor pm) throws CoreException,
		OperationCanceledException {
	StatusWrapper status = statusProvider.get();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(firstExpression, new CancelIndicator() {
		@Override
		public boolean isCanceled() {
			return pm.isCanceled();
		}
	});
	try {
		Set<String> calledExternalFeatureNames = newHashSet();
		returnType = calculateReturnType(resolvedTypes);
		if (returnType != null && !equal("void", returnType.getIdentifier()))
			returnExpression = lastExpression;
		boolean isReturnAllowed = isEndOfOriginalMethod(); 
		for (EObject element : EcoreUtil2.eAllContents(originalMethod.getExpression())) {
			if (pm.isCanceled()) {
				throw new OperationCanceledException();
			}
			boolean isLocalExpression = EcoreUtil.isAncestor(expressions, element);
			if (element instanceof XFeatureCall) {
				XFeatureCall featureCall = (XFeatureCall) element;
				JvmIdentifiableElement feature = featureCall.getFeature();
				LightweightTypeReference featureType = resolvedTypes.getActualType(featureCall);
				boolean isLocalFeature = EcoreUtil.isAncestor(expressions, feature);
				if (!isLocalFeature && isLocalExpression) {
					// call-out
					if (feature instanceof JvmFormalParameter || feature instanceof XVariableDeclaration) {
						if (!calledExternalFeatureNames.contains(feature.getSimpleName())) {
							calledExternalFeatureNames.add(feature.getSimpleName());
							ParameterInfo parameterInfo = new ParameterInfo(featureType.getIdentifier(), 
									feature.getSimpleName(), 
									parameterInfos.size());
							parameterInfos.add(parameterInfo);
							parameter2type.put(parameterInfo, featureType);
						}
						externalFeatureCalls.put(feature.getSimpleName(), featureCall);
					}
				} else if (isLocalFeature && !isLocalExpression) {
					// call-in
					if (returnExpression != null) {
						status.add(RefactoringStatus.FATAL,
								"Ambiguous return value: Multiple local variables are accessed in subsequent code.");
						break;
					}
					returnExpression = featureCall;
					returnType = featureType;
				}
			} else if(isLocalExpression) {
				if(element instanceof XReturnExpression && !isReturnAllowed) {
					status.add(RefactoringStatus.FATAL,
						"Extracting method would break control flow due to return statements.");
					break;
				} else if (element instanceof JvmTypeReference) {
					JvmType type = ((JvmTypeReference) element).getType();
					if (type instanceof JvmTypeParameter) {
						JvmOperation operation = associations.getDirectlyInferredOperation(originalMethod);
						if (operation != null) {
							List<JvmTypeParameter> typeParameters = operation.getTypeParameters();
							if (typeParameters.contains(type)) 
								neededTypeParameters.add((JvmTypeParameter) type);
						}
					}
				} else if (element instanceof JvmFormalParameter)
					localFeatureNames.add(((JvmFormalParameter) element).getName());
				else if (element instanceof XVariableDeclaration)
					localFeatureNames.add(((XVariableDeclaration) element).getIdentifier());
			}
		}
	} catch (OperationCanceledException e) {
		throw e;
	} catch (Exception exc) {
		handleException(exc, status);
	}
	return status.getRefactoringStatus();
}
 
Example #23
Source File: AbstractXbaseSemanticSequencer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Deprecated
protected void sequence_XReturnExpression(EObject context, XReturnExpression semanticObject) {
	sequence_XReturnExpression(createContext(context, semanticObject), semanticObject);
}
 
Example #24
Source File: DefaultEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected Collection<IEarlyExitComputer.ExitPoint> _exitPoints(final XReturnExpression expression) {
  IEarlyExitComputer.ExitPoint _exitPoint = new IEarlyExitComputer.ExitPoint(expression, false);
  return Collections.<IEarlyExitComputer.ExitPoint>singletonList(_exitPoint);
}
 
Example #25
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected Object _doEvaluate(XReturnExpression returnExpr, IEvaluationContext context, CancelIndicator indicator) {
	Object returnValue = internalEvaluate(returnExpr.getExpression(), context, indicator);
	throw new ReturnValue(returnValue);
}
 
Example #26
Source File: XbaseInterpreter.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * don't call this directly. Always call evaluate() internalEvaluate()
 */
protected Object doEvaluate(XExpression expression, IEvaluationContext context, CancelIndicator indicator) {
	if (expression instanceof XAssignment) {
      return _doEvaluate((XAssignment)expression, context, indicator);
    } else if (expression instanceof XDoWhileExpression) {
      return _doEvaluate((XDoWhileExpression)expression, context, indicator);
    } else if (expression instanceof XMemberFeatureCall) {
      return _doEvaluate((XMemberFeatureCall)expression, context, indicator);
    } else if (expression instanceof XWhileExpression) {
      return _doEvaluate((XWhileExpression)expression, context, indicator);
    } else if (expression instanceof XFeatureCall) {
    	return _doEvaluate((XFeatureCall)expression, context, indicator);
    } else if (expression instanceof XAbstractFeatureCall) {
    	return _doEvaluate((XAbstractFeatureCall)expression, context, indicator);
    } else if (expression instanceof XBlockExpression) {
      return _doEvaluate((XBlockExpression)expression, context, indicator);
    } else if (expression instanceof XSynchronizedExpression) {
	  return _doEvaluate((XSynchronizedExpression)expression, context, indicator);
	} else if (expression instanceof XBooleanLiteral) {
      return _doEvaluate((XBooleanLiteral)expression, context, indicator);
    } else if (expression instanceof XCastedExpression) {
      return _doEvaluate((XCastedExpression)expression, context, indicator);
    } else if (expression instanceof XClosure) {
      return _doEvaluate((XClosure)expression, context, indicator);
    } else if (expression instanceof XConstructorCall) {
      return _doEvaluate((XConstructorCall)expression, context, indicator);
    } else if (expression instanceof XForLoopExpression) {
      return _doEvaluate((XForLoopExpression)expression, context, indicator);
    } else if (expression instanceof XBasicForLoopExpression) {
	  return _doEvaluate((XBasicForLoopExpression)expression, context, indicator);
	} else if (expression instanceof XIfExpression) {
      return _doEvaluate((XIfExpression)expression, context, indicator);
    } else if (expression instanceof XInstanceOfExpression) {
      return _doEvaluate((XInstanceOfExpression)expression, context, indicator);
    } else if (expression instanceof XNullLiteral) {
      return _doEvaluate((XNullLiteral)expression, context, indicator);
    } else if (expression instanceof XNumberLiteral) {
      return _doEvaluate((XNumberLiteral)expression, context, indicator);
    } else if (expression instanceof XReturnExpression) {
      return _doEvaluate((XReturnExpression)expression, context, indicator);
    } else if (expression instanceof XStringLiteral) {
      return _doEvaluate((XStringLiteral)expression, context, indicator);
    } else if (expression instanceof XSwitchExpression) {
      return _doEvaluate((XSwitchExpression)expression, context, indicator);
    } else if (expression instanceof XThrowExpression) {
      return _doEvaluate((XThrowExpression)expression, context, indicator);
    } else if (expression instanceof XTryCatchFinallyExpression) {
      return _doEvaluate((XTryCatchFinallyExpression)expression, context, indicator);
    } else if (expression instanceof XTypeLiteral) {
      return _doEvaluate((XTypeLiteral)expression, context, indicator);
    } else if (expression instanceof XVariableDeclaration) {
	      return _doEvaluate((XVariableDeclaration)expression, context, indicator);
    } else if (expression instanceof XListLiteral) {
	      return _doEvaluate((XListLiteral)expression, context, indicator);
    } else if (expression instanceof XSetLiteral) {
	      return _doEvaluate((XSetLiteral)expression, context, indicator);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(expression, context, indicator).toString());
    }
}
 
Example #27
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
	if (expr instanceof XAnnotation) {
		return false;
	}
	if (expr instanceof XListLiteral) {
		return false;
	}
	if (expr instanceof XSetLiteral) {
		return false;
	}
	if (expr instanceof XCastedExpression) {
		return false;
	}
	if (expr instanceof XInstanceOfExpression) {
		return false;
	}
	if (expr instanceof XMemberFeatureCall && isVariableDeclarationRequired((XMemberFeatureCall) expr, b))
		return true;
	EObject container = expr.eContainer();
	if ((container instanceof XVariableDeclaration)
		|| (container instanceof XReturnExpression) 
		|| (container instanceof XThrowExpression)) {
		return false;
	}
	if (container instanceof XIfExpression) {
		XIfExpression ifExpression = (XIfExpression) container;
		if (ifExpression.getThen() == expr || ifExpression.getElse() == expr) {
			return false;
		}
	}
	if (container instanceof XCasePart) {
		XCasePart casePart = (XCasePart) container;
		if (casePart.getThen() == expr) {
			return false;
		}
	}
	if (container instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) container;
		if (switchExpression.getDefault() == expr) {
			return false;
		}
	}
	if (container instanceof XBlockExpression) {
		List<XExpression> siblings = ((XBlockExpression) container).getExpressions();
		if (siblings.get(siblings.size() - 1) == expr) {
			return isVariableDeclarationRequired(getFeatureCall(expr), expr, b);
		}
	}
	if (container instanceof XClosure) {
		if (((XClosure) container).getExpression() == expr) {
			return false;
		}
	}
	if (expr instanceof XAssignment) {
		XAssignment a = (XAssignment) expr;
		for (XExpression arg : getActualArguments(a)) {
			if (isVariableDeclarationRequired(arg, b, recursive)) {
				return true;
			}
		}
	}
	return super.isVariableDeclarationRequired(expr, b, recursive);
}
 
Example #28
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void _toJavaExpression(XReturnExpression returnExpression, ITreeAppendable b) {
	b.append("/* error - couldn't compile nested return */");
}
 
Example #29
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
@Override
protected void doInternalToJavaStatement(XExpression obj, ITreeAppendable appendable, boolean isReferenced) {
	if (obj instanceof XBlockExpression) {
		_toJavaStatement((XBlockExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XCastedExpression) {
		_toJavaStatement((XCastedExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XClosure) {
		_toJavaStatement((XClosure) obj, appendable, isReferenced);
	} else if (obj instanceof XConstructorCall) {
		_toJavaStatement((XConstructorCall) obj, appendable, isReferenced);
	} else if (obj instanceof XDoWhileExpression) {
		_toJavaStatement((XDoWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XForLoopExpression) {
		_toJavaStatement((XForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XBasicForLoopExpression) {
		_toJavaStatement((XBasicForLoopExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XIfExpression) {
		_toJavaStatement((XIfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XInstanceOfExpression) {
		_toJavaStatement((XInstanceOfExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XReturnExpression) {
		_toJavaStatement((XReturnExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XSwitchExpression) {
		_toJavaStatement((XSwitchExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XThrowExpression) {
		_toJavaStatement((XThrowExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XTryCatchFinallyExpression) {
		_toJavaStatement((XTryCatchFinallyExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XVariableDeclaration) {
		_toJavaStatement((XVariableDeclaration) obj, appendable, isReferenced);
	} else if (obj instanceof XWhileExpression) {
		_toJavaStatement((XWhileExpression) obj, appendable, isReferenced);
	} else if (obj instanceof XListLiteral) {
		_toJavaStatement((XListLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSetLiteral) {
		_toJavaStatement((XSetLiteral) obj, appendable, isReferenced);
	} else if (obj instanceof XSynchronizedExpression) {
		_toJavaStatement((XSynchronizedExpression) obj, appendable, isReferenced);
	} else {
		super.doInternalToJavaStatement(obj, appendable, isReferenced);
	}
}
 
Example #30
Source File: ExtendedEarlyExitComputer.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public boolean isDefiniteEarlyExit(XExpression expression) {
	// TODO further improvements
	if (expression instanceof XIfExpression) {
		XIfExpression ifExpression = (XIfExpression) expression;
		return isDefiniteEarlyExit(ifExpression.getThen()) && isDefiniteEarlyExit(ifExpression.getElse());
	} else if (expression instanceof XSwitchExpression) {
		XSwitchExpression switchExpression = (XSwitchExpression) expression;
		if (isDefiniteEarlyExit(switchExpression.getDefault())) {
			for(XCasePart caseExpression: switchExpression.getCases()) {
				if (!isDefiniteEarlyExit(caseExpression.getThen())) {
					return false;
				}
			}
			return true;
		}
		return false;
	} else if (expression instanceof XTryCatchFinallyExpression) {
		XTryCatchFinallyExpression tryExpression = (XTryCatchFinallyExpression) expression;
		if (isDefiniteEarlyExit(tryExpression.getFinallyExpression())) {
			return true;
		}
		if (isDefiniteEarlyExit(tryExpression.getExpression())) {
			for(XCatchClause catchClause: tryExpression.getCatchClauses()) {
				if (!isDefiniteEarlyExit(catchClause.getExpression())) {
					return false;
				}
			}
			return true;
		}
		return false;
	} else if (expression instanceof XBlockExpression) {
		List<XExpression> expressions = ((XBlockExpression) expression).getExpressions();
		for(int i = expressions.size() - 1; i >= 0; i--) {
			if (isDefiniteEarlyExit(expressions.get(i))) {
				return true;
			}
		}
	} else if (expression instanceof XSynchronizedExpression) {
		return isDefiniteEarlyExit(((XSynchronizedExpression) expression).getExpression());
	}
	return expression instanceof XReturnExpression || expression instanceof XThrowExpression;
}