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

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#openPseudoScope() . 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 void _toJavaStatement(final XSynchronizedExpression synchronizedExpression, final ITreeAppendable b, boolean isReferenced) {
	if (isReferenced) {
		declareSyntheticVariable(synchronizedExpression, b);
	}
	ITreeAppendable synchronizedAppendable = b.trace(synchronizedExpression, true);
	XExpression param = synchronizedExpression.getParam();
	if (!canCompileToJavaExpression(param, b))
		internalToJavaStatement(param, synchronizedAppendable, isReferenced);
	
	synchronizedAppendable.newLine().append("synchronized (");
	internalToJavaExpression(param, synchronizedAppendable);
	synchronizedAppendable.append(") {").increaseIndentation();
	synchronizedAppendable.openPseudoScope();
	
	XExpression expression = synchronizedExpression.getExpression();
	internalToJavaStatement(expression, b, isReferenced);
	if (isReferenced) {
		b.newLine().append(getVarName(synchronizedExpression, synchronizedAppendable)).append(" = ");
		internalToConvertedExpression(expression, b, getLightweightType(synchronizedExpression));
		b.append(";");
	}
	
	synchronizedAppendable.closeScope();
	synchronizedAppendable.decreaseIndentation().newLine().append("}");
}
 
Example 2
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaStatement(XBlockExpression expr, ITreeAppendable b, boolean isReferenced) {
	b = b.trace(expr, false);
	if (expr.getExpressions().isEmpty())
		return;
	if (expr.getExpressions().size()==1) {
		internalToJavaStatement(expr.getExpressions().get(0), b, isReferenced);
		return;
	}
	if (isReferenced)
		declareSyntheticVariable(expr, b);
	boolean needsBraces = isReferenced || !bracesAreAddedByOuterStructure(expr);
	if (needsBraces) {
		b.newLine().append("{").increaseIndentation();
		b.openPseudoScope();
	}
	final EList<XExpression> expressions = expr.getExpressions();
	for (int i = 0; i < expressions.size(); i++) {
		XExpression ex = expressions.get(i);
		if (i < expressions.size() - 1) {
			internalToJavaStatement(ex, b, false);
		} else {
			internalToJavaStatement(ex, b, isReferenced);
			if (isReferenced) {
				b.newLine().append(getVarName(expr, b)).append(" = ");
				internalToConvertedExpression(ex, b, getLightweightType(expr));
				b.append(";");
			}
		}
	}
	if (needsBraces) {
		b.closeScope();
		b.decreaseIndentation().newLine().append("}");
	}
}
 
Example 3
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.18
 */
protected void appendFinallyWithResources(XTryCatchFinallyExpression expr, ITreeAppendable b) {
	final String throwablesStore = b.getName(Tuples.pair(expr, "_caughtThrowables"));
	List<XVariableDeclaration> resources = expr.getResources();
	if (!resources.isEmpty()) {
		for (int i = resources.size() - 1; i >= 0; i--) {
			b.openPseudoScope();
			XVariableDeclaration res = resources.get(i);
			String resName = getVarName(res, b);
			b.newLine().append("if (" + resName + " != null) {");
			b.increaseIndentation();
			b.newLine().append("try {");
			b.increaseIndentation();
			b.newLine().append(resName + ".close();");
			// close inner try
			closeBlock(b);
			String throwable = b.declareSyntheticVariable(Tuples.pair(res, "_caughtThrowable"), "_t");
			b.append(" catch (").append(Throwable.class).append(" " + throwable + ") {");
			b.increaseIndentation();
			b.newLine().append(throwablesStore);
			b.append(".add(" + throwable + ");");
			// close inner catch
			closeBlock(b);
			// close if != null check
			closeBlock(b);
			b.closeScope();
		}
		b.newLine().append("if(!");
		b.append(throwablesStore);
		b.append(".isEmpty()) ");
		appendSneakyThrow(expr, b, throwablesStore + ".get(0)");
	}
}
 
Example 4
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(XWhileExpression expr, ITreeAppendable b, boolean isReferenced) {
	boolean needsStatement = !canCompileToJavaExpression(expr.getPredicate(), b);
	String varName = null;
	if (needsStatement) {
		internalToJavaStatement(expr.getPredicate(), b, true);
		varName = b.declareSyntheticVariable(expr, "_while");
		b.newLine().append("boolean ").append(varName).append(" = ");
		internalToJavaExpression(expr.getPredicate(), b);
		b.append(";");
	}
	b.newLine().append("while (");
	if (needsStatement) {
		b.append(varName);
	} else {
		internalToJavaExpression(expr.getPredicate(), b);
	}
	b.append(") {").increaseIndentation();
	b.openPseudoScope();
	internalToJavaStatement(expr.getBody(), b, false);
	if (needsStatement && !isEarlyExit(expr.getBody())) {
		internalToJavaStatement(expr.getPredicate(), b, true);
		b.newLine();
		b.append(varName).append(" = ");
		internalToJavaExpression(expr.getPredicate(), b);
		b.append(";");
	}
	b.closeScope();
	b.decreaseIndentation().newLine().append("}");
}
 
Example 5
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature 
 */
protected void toJavaBasicForStatement(XBasicForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	ITreeAppendable loopAppendable = b.trace(expr);
	loopAppendable.openPseudoScope();
	loopAppendable.newLine().append("for (");
	
	EList<XExpression> initExpressions = expr.getInitExpressions();
	XExpression firstInitExpression = IterableExtensions.head(initExpressions);
	if (firstInitExpression instanceof XVariableDeclaration) {
		XVariableDeclaration variableDeclaration = (XVariableDeclaration) firstInitExpression;
		LightweightTypeReference type = appendVariableTypeAndName(variableDeclaration, loopAppendable);
		loopAppendable.append(" = ");
		if (variableDeclaration.getRight() != null) {
			compileAsJavaExpression(variableDeclaration.getRight(), loopAppendable, type);
		} else {
			appendDefaultLiteral(loopAppendable, type);
		}
	} else {
		for (int i = 0; i < initExpressions.size(); i++) {
			if (i != 0) {
				loopAppendable.append(", ");
			}
			XExpression initExpression = initExpressions.get(i);
			compileAsJavaExpression(initExpression, loopAppendable, getLightweightType(initExpression));
		}
	}
	
	loopAppendable.append(";");
	
	XExpression expression = expr.getExpression();
	if (expression != null) {
		loopAppendable.append(" ");
		internalToJavaExpression(expression, loopAppendable);
	}
	loopAppendable.append(";");
	
	EList<XExpression> updateExpressions = expr.getUpdateExpressions();
	for (int i = 0; i < updateExpressions.size(); i++) {
		if (i != 0) {
			loopAppendable.append(",");
		}
		loopAppendable.append(" ");
		XExpression updateExpression = updateExpressions.get(i);
		internalToJavaExpression(updateExpression, loopAppendable);
	}
	loopAppendable.append(") {").increaseIndentation();
	
	XExpression eachExpression = expr.getEachExpression();
	internalToJavaStatement(eachExpression, loopAppendable, false);
	
	loopAppendable.decreaseIndentation().newLine().append("}");
	loopAppendable.closeScope();
}
 
Example 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void toJavaWhileStatement(XBasicForLoopExpression expr, ITreeAppendable b, boolean isReferenced) {
	ITreeAppendable loopAppendable = b.trace(expr);
	
	boolean needBraces = !bracesAreAddedByOuterStructure(expr);
	if (needBraces) {
		loopAppendable.newLine().increaseIndentation().append("{");
		loopAppendable.openPseudoScope();
	}
	
	EList<XExpression> initExpressions = expr.getInitExpressions();
	for (int i = 0; i < initExpressions.size(); i++) {
		XExpression initExpression = initExpressions.get(i);
		if (i < initExpressions.size() - 1) {
			internalToJavaStatement(initExpression, loopAppendable, false);
		} else {
			internalToJavaStatement(initExpression, loopAppendable, isReferenced);
			if (isReferenced) {
				loopAppendable.newLine().append(getVarName(expr, loopAppendable)).append(" = (");
				internalToConvertedExpression(initExpression, loopAppendable, getLightweightType(expr));
				loopAppendable.append(");");
			}
		}
	}

	final String varName = loopAppendable.declareSyntheticVariable(expr, "_while");
	
	XExpression expression = expr.getExpression();
	if (expression != null) {
		internalToJavaStatement(expression, loopAppendable, true);
		loopAppendable.newLine().append("boolean ").append(varName).append(" = ");
		internalToJavaExpression(expression, loopAppendable);
		loopAppendable.append(";");
	} else {
		loopAppendable.newLine().append("boolean ").append(varName).append(" = true;");
	}
	loopAppendable.newLine();
	loopAppendable.append("while (");
	loopAppendable.append(varName);
	loopAppendable.append(") {").increaseIndentation();
	loopAppendable.openPseudoScope();
	
	XExpression eachExpression = expr.getEachExpression();
	internalToJavaStatement(eachExpression, loopAppendable, false);
	
	EList<XExpression> updateExpressions = expr.getUpdateExpressions();
	if (!updateExpressions.isEmpty()) {
		for (XExpression updateExpression : updateExpressions) {
			internalToJavaStatement(updateExpression, loopAppendable, false);
		}
	}
	
	if (!isEarlyExit(eachExpression)) {
		if (expression != null) {
			internalToJavaStatement(expression, loopAppendable, true);
			loopAppendable.newLine().append(varName).append(" = ");
			internalToJavaExpression(expression, loopAppendable);
			loopAppendable.append(";");
		} else {
			loopAppendable.newLine().append(varName).append(" = true;");
		}
	}
	
	loopAppendable.closeScope();
	loopAppendable.decreaseIndentation().newLine().append("}");
	
	if (needBraces) {
		loopAppendable.closeScope();
		loopAppendable.decreaseIndentation().newLine().append("}");
	}
}
 
Example 7
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected ITreeAppendable appendOpenIfStatement(XCasePart casePart, ITreeAppendable b, String matchedVariable, String variableName, XSwitchExpressionCompilationState state) {
	ITreeAppendable caseAppendable = b.trace(casePart, true);
	if (state.caseNeedsIfNotMatchedCheck()) {
		caseAppendable.newLine().append("if (!").append(matchedVariable).append(") {");
		caseAppendable.increaseIndentation();
	}
	JvmTypeReference typeGuard = casePart.getTypeGuard();
	if (typeGuard != null) {
		ITreeAppendable typeGuardAppendable = caseAppendable.trace(typeGuard, true);
		typeGuardAppendable.newLine().append("if (");
		if (typeGuard instanceof JvmSynonymTypeReference) {
			Iterator<JvmTypeReference> iter = ((JvmSynonymTypeReference) typeGuard).getReferences().iterator();
			while(iter.hasNext()) {
				typeGuardAppendable.append(variableName);
				typeGuardAppendable.append(" instanceof ");
				typeGuardAppendable.trace(typeGuard).append(iter.next().getType());
				if (iter.hasNext()) {
					typeGuardAppendable.append(" || ");
				}
			}
		} else {
			typeGuardAppendable.append(variableName);
			typeGuardAppendable.append(" instanceof ");
			typeGuardAppendable.trace(typeGuard).append(typeGuard.getType());
		}
		typeGuardAppendable.append(") {");
		typeGuardAppendable.increaseIndentation();
		typeGuardAppendable.openPseudoScope();
	}
	if (casePart.getCase() != null) {
		ITreeAppendable conditionAppendable = caseAppendable.trace(casePart.getCase(), true);
		internalToJavaStatement(casePart.getCase(), conditionAppendable, true);
		conditionAppendable.newLine().append("if (");
		LightweightTypeReference convertedType = getLightweightType(casePart.getCase());
		if (convertedType.isType(Boolean.TYPE) || convertedType.isType(Boolean.class)) {
			internalToJavaExpression(casePart.getCase(), conditionAppendable);
		} else {
			JvmType objectsType = findKnownType(Objects.class, casePart);
			if (objectsType != null) {
				conditionAppendable.append(objectsType);
				conditionAppendable.append(".equal(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			} else {
				// use ObjectExtensions rather than a == b || a != null && a.equals(b) since
				// that won't work with primitive types and other incompatible conditional operands
				conditionAppendable.append(ObjectExtensions.class);
				conditionAppendable.append(".operator_equals(").append(variableName).append(", ");
				internalToJavaExpression(casePart.getCase(), conditionAppendable);
				conditionAppendable.append(")");
			}
		}
		conditionAppendable.append(")");
		caseAppendable.append(" {");
		caseAppendable.increaseIndentation();
	}
	// set matched to true
	return caseAppendable.newLine().append(matchedVariable).append("=true;");
}
 
Example 8
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected ITreeAppendable toLambda(XClosure closure, ITreeAppendable b, LightweightTypeReference type,
		JvmOperation operation, boolean writeExplicitTargetType) {
	if (writeExplicitTargetType) {
		b.append("((");
		b.append(type);
		b.append(") ");
	}
	try {
		b.openPseudoScope();
		b.append("(");
		List<JvmFormalParameter> closureParams = closure.getFormalParameters();
		boolean isVarArgs = operation.isVarArgs();
		for (int i = 0; i < closureParams.size(); i++) {
			JvmFormalParameter closureParam = closureParams.get(i);
			LightweightTypeReference parameterType = getClosureOperationParameterType(type, operation, i);
			if (isVarArgs && i == closureParams.size()-1 && parameterType.isArray()) {
				b.append(parameterType.getComponentType());
				b.append("...");
			} else {
				b.append(parameterType);
			}
			b.append(" ");
			String proposedParamName = makeJavaIdentifier(closureParam.getName());	
			// Usually a normal variable would suffice here. The 'unique name' variable is a workaround for
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=445949
			String name = b.declareUniqueNameVariable(closureParam, proposedParamName);
			b.append(name);
			if (i != closureParams.size() - 1)
				b.append(", ");
		}
		b.append(") -> {");
		b.increaseIndentation();
		LightweightTypeReference returnType = getClosureOperationReturnType(type, operation);
		compile(closure.getExpression(), b, returnType, newHashSet(operation.getExceptions()));
		closeBlock(b);
	} finally {
		b.closeScope();
	}
	if (writeExplicitTargetType) {
		b.append(")");
	}
	return b;
}
 
Example 9
Source File: SarlCompiler.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("checkstyle:npathcomplexity")
private ITreeAppendable toSerializableAnonymousClass(XClosure closure, ITreeAppendable appendable,
		LightweightTypeReference type, JvmOperation operation) {
	// This function is implemented in order to generate static inner class when the closure
	// cannot be represented neither by a Java 8 lambda nor a not-static inner class.
	// It solves the issues related to the serialization and deserialization of the closures.
	final List<JvmFormalParameter> closureParams = closure.getFormalParameters();
	appendable.openPseudoScope();
	try {
		final List<XAbstractFeatureCall> localReferences = getReferencedExternalCalls(
				closure.getExpression(), closureParams, false, appendable);
		final LightweightTypeReference returnType = getClosureOperationReturnType(type, operation);
		appendable.append("new ").append(type).append("() {"); //$NON-NLS-1$ //$NON-NLS-2$
		appendable.increaseIndentation();
		String selfVariable = null;
		try {
			appendable.openScope();
			if (needSyntheticSelfVariable(closure, type)) {
				appendable.newLine().append("final "); //$NON-NLS-1$
				appendable.append(type).append(" "); //$NON-NLS-1$
				selfVariable = appendable.declareVariable(type.getType(), "_self"); //$NON-NLS-1$
				appendable.append(selfVariable);
				appendable.append(" = this;"); //$NON-NLS-1$
			}
			appendOperationVisibility(appendable, operation);
			if (!operation.getTypeParameters().isEmpty()) {
				appendTypeParameters(appendable, operation, type);
			}
			appendable.append(returnType);
			appendable.append(" ").append(operation.getSimpleName()); //$NON-NLS-1$
			appendable.append("("); //$NON-NLS-1$
			final boolean isVarArgs = operation.isVarArgs();
			for (int i = 0; i < closureParams.size(); i++) {
				final JvmFormalParameter closureParam = closureParams.get(i);
				final LightweightTypeReference parameterType = getClosureOperationParameterType(type, operation, i);
				if (isVarArgs && i == closureParams.size() - 1 && parameterType.isArray()) {
					appendClosureParameterVarArgs(closureParam, parameterType.getComponentType(), appendable);
				} else {
					appendClosureParameter(closureParam, parameterType, appendable);
				}
				if (i != closureParams.size() - 1) {
					appendable.append(", "); //$NON-NLS-1$
				}
			}
			appendable.append(")"); //$NON-NLS-1$
			if (!operation.getExceptions().isEmpty()) {
				appendable.append(" throws "); //$NON-NLS-1$
				for (int i = 0; i < operation.getExceptions().size(); ++i) {
					serialize(operation.getExceptions().get(i), closure, appendable, false, false, false, false);
					if (i != operation.getExceptions().size() - 1) {
						appendable.append(", "); //$NON-NLS-1$
					}
				}
			}
			appendable.append(" {"); //$NON-NLS-1$
			appendable.increaseIndentation();
			if (selfVariable == null) {
				reassignThisInClosure(appendable, type.getType());
			} else {
				// We have already assigned the closure type to _self, so don't assign it again
				reassignThisInClosure(appendable, null);
			}
			compile(closure.getExpression(), appendable, returnType, newHashSet(operation.getExceptions()));
			closeBlock(appendable);
		} catch (Exception exception) {
			throw new RuntimeException(exception);
		} finally {
			appendable.closeScope();
		}
		appendable.newLine().append("private ").append(Object.class).append(" writeReplace() throws ");  //$NON-NLS-1$//$NON-NLS-2$
		appendable.append(ObjectStreamException.class).append(" {").increaseIndentation().newLine(); //$NON-NLS-1$
		if (selfVariable == null) {
			reassignThisInClosure(appendable, type.getType());
		} else {
			// We have already assigned the closure type to _self, so don't assign it again
			reassignThisInClosure(appendable, null);
		}
		appendable.append("return new ").append(SerializableProxy.class); //$NON-NLS-1$
		appendable.append("(").append(appendable.getName(type)).append(".class"); //$NON-NLS-1$ //$NON-NLS-2$
		for (final XAbstractFeatureCall call : localReferences) {
			appendable.append(", "); //$NON-NLS-1$
			compileAsJavaExpression(call, appendable, returnType);
		}

		appendable.append(");").decreaseIndentation().newLine().append("}"); //$NON-NLS-1$ //$NON-NLS-2$
		appendable.decreaseIndentation().newLine().append("}"); //$NON-NLS-1$
	} finally {
		appendable.closeScope();
	}
	return appendable;
}