Java Code Examples for org.eclipse.xtext.xbase.XExpression#eContainer()

The following examples show how to use org.eclipse.xtext.xbase.XExpression#eContainer() . 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: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private boolean mustInsertTypeCast(XExpression expression, LightweightTypeReference actualType) {
	IResolvedTypes resolvedTypes = getResolvedTypes(expression);
	if (mustCheckForMandatoryTypeCast(resolvedTypes, expression)) {
		if (expression instanceof XAbstractFeatureCall) {
			LightweightTypeReference featureType = resolvedTypes.getActualType(((XAbstractFeatureCall) expression).getFeature());
			if (featureType != null && !featureType.isMultiType() && actualType.isAssignableFrom(featureType)) {
				return false;
			}
			if (featureType != null && featureType.isMultiType()) {
				JvmTypeReference compliantTypeReference = featureType.toJavaCompliantTypeReference();
				if (actualType.isAssignableFrom(featureType.getOwner().toLightweightTypeReference(compliantTypeReference))) {
					return false;
				}
			}
		}
		if (expression.eContainer() instanceof XCastedExpression) {
			XCastedExpression castedExpression = (XCastedExpression) expression.eContainer();
			LightweightTypeReference castedExpressionType = getResolvedTypes(castedExpression).getActualType(castedExpression);
			if (castedExpressionType != null) {
				return actualType.getType() != castedExpressionType.getType();	
			}
		}
		return true;
	}
	return false;
}
 
Example 2
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
private void convertArrayToList(final LightweightTypeReference left, final ITreeAppendable appendable, XExpression context,
		final Later expression) {
	if (!(context.eContainer() instanceof XCastedExpression)) {
		if (context.eContainer() instanceof XAbstractFeatureCall) {
			appendable.append("((");
		} else {
			appendable.append("(");
		}
		appendable.append(left);
		appendable.append(")");
	}
	appendable.append(Conversions.class);
	appendable.append(".doWrapArray(");
	expression.exec(appendable);
	if (!(context.eContainer() instanceof XCastedExpression)) {
		if (context.eContainer() instanceof XAbstractFeatureCall) {
			appendable.append("))");
		} else {
			appendable.append(")");
		}
	} else {
		appendable.append(")");
	}
}
 
Example 3
Source File: SarlCompiler.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expression, ITreeAppendable appendable, boolean recursive) {
	// Add the following test for avoiding to create an variable declaration when the expression has already a name.
	final String refName = getReferenceName(expression, appendable);
	if (!Strings.isEmpty(refName)) {
		return false;
	}
	// Overridden for enabling the expressions that are specific to SARL
	if (expression instanceof SarlBreakExpression) {
		return false;
	}
	if (expression instanceof SarlContinueExpression) {
		return false;
	}
	final EObject container = expression.eContainer();
	if (container instanceof SarlAssertExpression) {
		return false;
	}
	return super.isVariableDeclarationRequired(expression, appendable, recursive);
}
 
Example 4
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected boolean bracesAreAddedByOuterStructure(XExpression expression) {
	EObject container = expression.eContainer();
	if (container instanceof XTryCatchFinallyExpression 
			|| container instanceof XIfExpression
			|| container instanceof XClosure
			|| container instanceof XSynchronizedExpression) {
		return true;
	}
	if (container instanceof XBlockExpression) {
		XBlockExpression blockExpression = (XBlockExpression) container;
		EList<XExpression> expressions = blockExpression.getExpressions();
		if (expressions.size() == 1 && expressions.get(0) == expression) {
			return bracesAreAddedByOuterStructure(blockExpression);
		}
	}
	if (!(container instanceof XExpression)) {
		return true;
	}
	return false;
}
 
Example 5
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
protected boolean isVariableDeclarationRequired(XExpression expr, ITreeAppendable b, boolean recursive) {
	boolean result = super.isVariableDeclarationRequired(expr, b, recursive);
	if (result && expr instanceof XConstructorCall) {
		EObject container = expr.eContainer();
		if (container instanceof AnonymousClass) {
			AnonymousClass anonymousClass = (AnonymousClass) container;
			result = isVariableDeclarationRequired(anonymousClass, b, recursive);
			if (result) {
				JvmConstructor constructor = anonymousClass.getConstructorCall().getConstructor();
				JvmDeclaredType type = constructor.getDeclaringType();
				if (((JvmGenericType) type).isAnonymous()) {
					return false;
				}
			}
		}
	}
	return result;
}
 
Example 6
Source File: ReorderedVarArgFeatureCallArgumentsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void withIndizes(final IFeatureCallArguments arguments, final int... indexes) {
  final Consumer<Integer> _function = (Integer it) -> {
    Assert.assertTrue(arguments.hasUnprocessedArguments());
    final IFeatureCallArgumentSlot slot = arguments.getNextUnprocessedArgumentSlot();
    final XExpression expression = IterableExtensions.<XExpression>head(slot.getArgumentExpressions());
    EObject _eContainer = expression.eContainer();
    final XFeatureCall featureCall = ((XFeatureCall) _eContainer);
    Assert.assertEquals((it).intValue(), featureCall.getFeatureCallArguments().indexOf(expression));
    slot.markProcessed();
  };
  ((List<Integer>)Conversions.doWrapArray(indexes)).forEach(_function);
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 7
Source File: SARLValidator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValueExpectedRecursive(XExpression expr) {
	final EObject container = expr.eContainer();
	if (container instanceof SarlBreakExpression) {
		return false;
	}
	if (container instanceof SarlContinueExpression) {
		return false;
	}
	if (container instanceof SarlAssertExpression) {
		return true;
	}
	return super.isValueExpectedRecursive(expr);
}
 
Example 8
Source File: CheckJavaValidator.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected boolean isValueExpectedRecursive(final XExpression expr) {
  EObject container = expr.eContainer();
  if (container instanceof XIssueExpression || container instanceof XGuardExpression) {
    return true;
  }
  return super.isValueExpectedRecursive(expr);
}
 
Example 9
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void acceptForLoop(JvmFormalParameter parameter, /* @Nullable */ XExpression expression) {
	currentAppendable = null;
	super.acceptForLoop(parameter, expression);
	if (expression == null)
		throw new IllegalArgumentException("expression may not be null");
	RichStringForLoop forLoop = (RichStringForLoop) expression.eContainer();
	forStack.add(forLoop);
	appendable.newLine();
	pushAppendable(forLoop);
	appendable.append("{").increaseIndentation();
	
	ITreeAppendable debugAppendable = appendable.trace(forLoop, true);
	internalToJavaStatement(expression, debugAppendable, true);
	String variableName = null;
	if (forLoop.getBefore() != null || forLoop.getSeparator() != null || forLoop.getAfter() != null) {
		variableName = debugAppendable.declareSyntheticVariable(forLoop, "_hasElements");
		debugAppendable.newLine();
		debugAppendable.append("boolean ");
		debugAppendable.append(variableName);
		debugAppendable.append(" = false;");
	}
	debugAppendable.newLine();
	debugAppendable.append("for(final ");
	// TODO tracing if parameter was explicitly declared
	LightweightTypeReference paramType = getLightweightType(parameter);
	if (paramType != null) {
		debugAppendable.append(paramType);
	} else {
		debugAppendable.append("Object");
	}
	debugAppendable.append(" ");
	String loopParam = debugAppendable.declareVariable(parameter, parameter.getName());
	debugAppendable.append(loopParam);
	debugAppendable.append(" : ");
	internalToJavaExpression(expression, debugAppendable);
	debugAppendable.append(") {").increaseIndentation();
}
 
Example 10
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected boolean isValueExpectedRecursive(XExpression expr) {
	EObject container = expr.eContainer();
	if (container instanceof RichString 
		|| container instanceof RichStringForLoop
		|| container instanceof XtendField) {
		return true;
	}
	return super.isValueExpectedRecursive(expr);
}
 
Example 11
Source File: ReorderedFeatureCallArgumentsTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void withIndizes(final IFeatureCallArguments arguments, final int... indexes) {
  final Consumer<Integer> _function = (Integer it) -> {
    Assert.assertTrue(arguments.hasUnprocessedArguments());
    final IFeatureCallArgumentSlot slot = arguments.getNextUnprocessedArgumentSlot();
    final XExpression expression = slot.getArgumentExpression();
    EObject _eContainer = expression.eContainer();
    final XFeatureCall featureCall = ((XFeatureCall) _eContainer);
    Assert.assertEquals((it).intValue(), featureCall.getFeatureCallArguments().indexOf(expression));
    slot.markProcessed();
  };
  ((List<Integer>)Conversions.doWrapArray(indexes)).forEach(_function);
  Assert.assertFalse(arguments.hasUnprocessedArguments());
}
 
Example 12
Source File: NewFeatureNameUtil.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void setFeatureScopeContext(XExpression siblingExpression) {
	XBlockExpression containerBlock = 
			(siblingExpression.eContainer() instanceof XBlockExpression) 
			? (XBlockExpression) siblingExpression.eContainer() 
			: null;
	EObject context = siblingExpression;
	if (containerBlock != null && !containerBlock.getExpressions().isEmpty()) {
		context = containerBlock.getExpressions().get(containerBlock.getExpressions().size() - 1);
	}
	IExpressionScope expressionScope = batchTypeResolver.resolveTypes(context).getExpressionScope(context, IExpressionScope.Anchor.AFTER);
	featureCallScope = expressionScope.getFeatureScope();
}
 
Example 13
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected XAbstractFeatureCall getFeatureCall(final XExpression argument) {
	EObject expr = argument.eContainer();
	if (expr instanceof XAbstractFeatureCall) {
		return (XAbstractFeatureCall) expr;
	}
	if (expr instanceof XBlockExpression) {
		XBlockExpression blockExpression = (XBlockExpression) expr;
		if (blockExpression.getExpressions().size() == 1)
			return getFeatureCall(blockExpression);
	}
	return null;
}
 
Example 14
Source File: TypeConvertingCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/** Convert a wrapper expression (number, char, boolean) to its primitive equivalent.
 *
 * @param wrapper unused in this context but useful for inheritors.
 * @param primitive the primitive type to convert to.
 * @param context the context of the convertion, i.e. the containing expression.
 * @param appendable the receiver of the convertion.
 * @param expression the expression to convert.
 */
protected void convertWrapperToPrimitive(
		final LightweightTypeReference wrapper, 
		final LightweightTypeReference primitive, 
		XExpression context, 
		final ITreeAppendable appendable,
		final Later expression) {
	XExpression normalized = normalizeBlockExpression(context);
	if (normalized instanceof XAbstractFeatureCall && !(context.eContainer() instanceof XAbstractFeatureCall)) {
		// Avoid javac bug
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=410797
		// TODO make that dependent on the compiler version (javac 1.7 fixed that bug)
		XAbstractFeatureCall featureCall = (XAbstractFeatureCall) normalized;
		if (featureCall.isStatic()) {
			JvmIdentifiableElement feature = featureCall.getFeature();
			if (feature instanceof JvmOperation) {
				if (!((JvmOperation) feature).getTypeParameters().isEmpty()) {
					appendable.append("(");
					appendable.append(primitive);
					appendable.append(") ");
					expression.exec(appendable);
					return;
				}
			}
		}
	}
	appendable.append("(");
	if (mustInsertTypeCast(context, wrapper)) {
		appendable.append("(");
		appendable.append(wrapper);
		appendable.append(") ");
	} 
	expression.exec(appendable);
	appendable.append(")");
	appendable.append(".");
	appendable.append(primitive);
	appendable.append("Value()");
}
 
Example 15
Source File: XbaseValidator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected boolean isValueExpectedRecursive(XExpression expr) {
	EStructuralFeature feature = expr.eContainingFeature();
	EObject container = expr.eContainer();
	
	// is part of block
	if (container instanceof XBlockExpression) {
		XBlockExpression blockExpression = (XBlockExpression) container;
		final List<XExpression> expressions = blockExpression.getExpressions();
		if (expressions.get(expressions.size()-1) != expr) {
			return false;
		}
	}
	// no expectation cases
	if (feature == XbasePackage.Literals.XTRY_CATCH_FINALLY_EXPRESSION__FINALLY_EXPRESSION
		|| feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__BODY
		|| feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__EACH_EXPRESSION) {
		return false;
	}
	// is value expected
	if (container instanceof XAbstractFeatureCall 
		|| container instanceof XConstructorCall
		|| container instanceof XAssignment
		|| container instanceof XVariableDeclaration
		|| container instanceof XReturnExpression
		|| container instanceof XThrowExpression
		|| feature == XbasePackage.Literals.XFOR_LOOP_EXPRESSION__FOR_EXPRESSION
		|| feature == XbasePackage.Literals.XSWITCH_EXPRESSION__SWITCH
		|| feature == XbasePackage.Literals.XCASE_PART__CASE
		|| feature == XbasePackage.Literals.XIF_EXPRESSION__IF
		|| feature == XbasePackage.Literals.XABSTRACT_WHILE_EXPRESSION__PREDICATE
		|| feature == XbasePackage.Literals.XBASIC_FOR_LOOP_EXPRESSION__EXPRESSION
		|| feature == XbasePackage.Literals.XSYNCHRONIZED_EXPRESSION__PARAM) {
		return true;
	}
	if (isLocalClassSemantics(container) || logicalContainerProvider.getLogicalContainer(expr) != null) {
		LightweightTypeReference expectedReturnType = typeResolver.resolveTypes(expr).getExpectedReturnType(expr);
		return expectedReturnType == null || !expectedReturnType.isPrimitiveVoid();
	}
	if (container instanceof XCasePart || container instanceof XCatchClause) {
		container = container.eContainer();
	}
	if (container instanceof XExpression) {
		return isValueExpectedRecursive((XExpression) container);
	}
	return true;
}
 
Example 16
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 17
Source File: SarlCompiler.java    From sarl with Apache License 2.0 4 votes vote down vote up
private void convertNullSafeWrapperToPrimitive(
		LightweightTypeReference wrapper,
		LightweightTypeReference primitive,
		XExpression context,
		ITreeAppendable appendable,
		Later expression) {
	// BEGIN Specific
	final String defaultValue = primitive.isType(boolean.class) ? "false" : "0"; //$NON-NLS-1$ //$NON-NLS-2$
	// END Specific
	final XExpression normalized = normalizeBlockExpression(context);
	if (normalized instanceof XAbstractFeatureCall && !(context.eContainer() instanceof XAbstractFeatureCall)) {
		// Avoid javac bug
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=410797
		// TODO make that dependent on the compiler version (javac 1.7 fixed that bug)
		final XAbstractFeatureCall featureCall = (XAbstractFeatureCall) normalized;
		if (featureCall.isStatic()) {
			final JvmIdentifiableElement feature = featureCall.getFeature();
			if (feature instanceof JvmOperation) {
				if (!((JvmOperation) feature).getTypeParameters().isEmpty()) {
					// BEGIN Specific
					appendable.append("(("); //$NON-NLS-1$
					expression.exec(appendable);
					appendable.append(") == null ? "); //$NON-NLS-1$
					appendable.append(defaultValue);
					appendable.append(" : "); //$NON-NLS-1$
					// END Specific
					appendable.append("("); //$NON-NLS-1$
					appendable.append(primitive);
					appendable.append(") "); //$NON-NLS-1$
					expression.exec(appendable);
					// BEGIN Specific
					appendable.append(") "); //$NON-NLS-1$
					// END Specific
					return;
				}
			}
		}
	}

	// BEGIN Specific
	appendable.append("(("); //$NON-NLS-1$
	expression.exec(appendable);
	appendable.append(") == null ? "); //$NON-NLS-1$
	appendable.append(defaultValue);
	appendable.append(" : "); //$NON-NLS-1$
	// END Specific
	final boolean mustInsertTypeCast;
	try {
		mustInsertTypeCast = (Boolean) this.reflect.invoke(this, "mustInsertTypeCast", context, wrapper); //$NON-NLS-1$
	} catch (Exception exception) {
		throw new Error(exception);
	}
	if (mustInsertTypeCast) {
		appendable.append("("); //$NON-NLS-1$
		appendable.append(wrapper);
		appendable.append(") "); //$NON-NLS-1$
	}
	// BEGIN Specific
	appendable.append("("); //$NON-NLS-1$
	expression.exec(appendable);
	appendable.append(")"); //$NON-NLS-1$
	// END Specific
	appendable.append("."); //$NON-NLS-1$
	appendable.append(primitive);
	appendable.append("Value())"); //$NON-NLS-1$
}