Java Code Examples for org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#hasName()

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#hasName() . 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: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected String getSwitchLocalVariableName(XSwitchExpression expr, ITreeAppendable b) {
	JvmFormalParameter declaredParam = expr.getDeclaredParam();
	if (declaredParam != null) {
		if (b.hasName(declaredParam)) {
			return b.getName(declaredParam);
		}
		return null;
	}
	XExpression switchExpression = expr.getSwitch();
	if (b.hasName(switchExpression)) {
		return b.getName(switchExpression);
	} 
	if (switchExpression instanceof XFeatureCall) {
		XFeatureCall featureCall = (XFeatureCall) switchExpression;
		JvmIdentifiableElement feature = featureCall.getFeature();
		if (b.hasName(feature)) {
			return b.getName(feature);
		}
	}
	return null;
}
 
Example 2
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _toJavaExpression(final XClosure closure, final ITreeAppendable b) {
	if (b.hasName(closure)) {
		b.trace(closure, false).append(getVarName(closure, b));
	} else {
		LightweightTypeReference type = getLightweightType(closure);
		JvmOperation operation = findImplementingOperation(type);
		if (operation != null) {
			GeneratorConfig config = b.getGeneratorConfig();
			if (config != null && config.getJavaSourceVersion().isAtLeast(JAVA8) && canCompileToJavaLambda(closure, type, operation)) {
				toLambda(closure, b.trace(closure, false), type, operation, true);
			} else {
				toAnonymousClass(closure, b.trace(closure, false), type, operation);
			}
		}
	}
}
 
Example 3
Source File: XtendCompiler.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Symmetric to {@link XtendGenerator#reassignThisType(ITreeAppendable, JvmDeclaredType)}
 */
@Override
protected void doReassignThisInClosure(ITreeAppendable b, JvmType prevType) {
	if (prevType instanceof JvmDeclaredType && ((JvmDeclaredType) prevType).isLocal()) {
		if (b.hasName(Pair.of("this", prevType))) {
			b.declareVariable(prevType, b.getName(Pair.of("this", prevType)));
		} else {
			b.declareSyntheticVariable(prevType, "");
		}
		if (b.hasObject("super")) {
			Object superElement = b.getObject("super");
			if (superElement instanceof JvmType) {
				// Don't reassign the super of the enclosing type if it has already been reassigned
				String superVariable = b.getName(superElement);
				if ("super".equals(superVariable)) {
					b.declareSyntheticVariable(superElement, prevType.getSimpleName()+".super");
				}
			}
		}
	} else {
		super.doReassignThisInClosure(b, prevType);
	}
}
 
Example 4
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected boolean nullSafeMemberFeatureCallExpressionNeedsPreparation(XExpression argument, ITreeAppendable b) {
	if (b.hasName(argument))
		return false;
	if (argument instanceof JvmField || argument instanceof JvmFormalParameter)
		return false;
	return true;
}
 
Example 5
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected ITreeAppendable appendLeftOperand(final XAbstractFeatureCall expr, ITreeAppendable appendable, boolean isExpressionContext) {
	XBinaryOperation binaryOperation = (XBinaryOperation) expr;
	XAbstractFeatureCall leftOperand = (XAbstractFeatureCall) binaryOperation.getLeftOperand();
	JvmIdentifiableElement feature = leftOperand.getFeature();
	if (appendable.hasName(feature)) {
		return appendable.append(appendable.getName(feature));
	}
	boolean hasReceiver = appendReceiver(leftOperand, appendable, isExpressionContext);
	if (hasReceiver) {
		appendable.append(".");
	}
	return appendable.append(feature.getSimpleName());
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
/* @Nullable */
protected String getReferenceName(XExpression expr, ITreeAppendable b) {
	if (expr instanceof XSwitchExpression) {
		Object key = getSwitchExpressionKey((XSwitchExpression) expr);
		if (b.hasName(key))
			return b.getName(key);
	}
	return super.getReferenceName(expr, b);
}
 
Example 7
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @return the variable name under which the result of the expression is stored. Returns <code>null</code> if the
 *          expression hasn't been assigned to a local variable before.
 */
/* @Nullable */
protected String getReferenceName(XExpression expr, ITreeAppendable b) {
	if (b.hasName(expr))
		return b.getName(expr);
	if (expr instanceof XFeatureCall) {
		XFeatureCall featureCall = (XFeatureCall) expr;
		if (b.hasName(featureCall.getFeature()))
			return b.getName(featureCall.getFeature());
	}
	return null;
}
 
Example 8
Source File: XtendGenerator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String reassignThisType(final ITreeAppendable b, final JvmDeclaredType declaredType) {
  String _xblockexpression = null;
  {
    boolean _hasObject = b.hasObject("this");
    if (_hasObject) {
      final Object element = b.getObject("this");
      if ((element instanceof JvmDeclaredType)) {
        boolean _isLocal = ((JvmDeclaredType)element).isLocal();
        if (_isLocal) {
          Pair<String, JvmDeclaredType> _mappedTo = Pair.<String, JvmDeclaredType>of("this", ((JvmDeclaredType)element));
          boolean _hasName = b.hasName(_mappedTo);
          if (_hasName) {
            Pair<String, JvmDeclaredType> _mappedTo_1 = Pair.<String, JvmDeclaredType>of("this", ((JvmDeclaredType)element));
            b.declareVariable(element, b.getName(_mappedTo_1));
          } else {
            b.declareVariable(element, "");
          }
        } else {
          String _simpleName = ((JvmDeclaredType)element).getSimpleName();
          final String proposedName = (_simpleName + ".this");
          b.declareVariable(element, proposedName);
        }
      }
    }
    String _xifexpression = null;
    if ((declaredType != null)) {
      _xifexpression = b.declareVariable(declaredType, "this");
    }
    _xblockexpression = _xifexpression;
  }
  return _xblockexpression;
}
 
Example 9
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendFeatureCall(XAbstractFeatureCall call, ITreeAppendable b) {
	if (expressionHelper.isInlined(call)) {
		appendInlineFeatureCall(call, b);
		return;
	}
	JvmIdentifiableElement feature = call.getFeature();
	String name = null;
	if (feature instanceof JvmConstructor) {
		JvmDeclaredType constructorContainer = ((JvmConstructor) feature).getDeclaringType();
		JvmIdentifiableElement logicalContainer = contextProvider.getNearestLogicalContainer(call);
		JvmDeclaredType contextType = ((JvmMember) logicalContainer).getDeclaringType();
		if (contextType == constructorContainer)
			name = "this";
		else
			name = "super";
	} else if(feature != null) {
		if (b.hasName(feature)) {
			name = b.getName(feature);
		} else {
			name = featureNameProvider.getSimpleName(feature);
		}
	}
	if(name == null)
		name = "/* name is null */";
	b.trace(call, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE, 0).append(name);
	if (feature instanceof JvmExecutable) {
		b.append("(");
		List<XExpression> arguments = getActualArguments(call);
		if (!arguments.isEmpty()) {
			XExpression receiver = null;
			if (call instanceof XMemberFeatureCall) {
				receiver = ((XMemberFeatureCall) call).getMemberCallTarget();
			} else if (call instanceof XAssignment) {
				receiver = ((XAssignment) call).getAssignable();
			}
			boolean shouldBreakFirstArgument = receiver == null || arguments.get(0) != receiver;
			appendArguments(arguments, b, shouldBreakFirstArgument);
		}
		b.append(")");
	}
}