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

The following examples show how to use org.eclipse.xtext.xbase.compiler.output.ITreeAppendable#declareSyntheticVariable() . 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
protected void doReassignThisInClosure(final ITreeAppendable b, JvmType prevType) {
	final String proposedName = prevType.getSimpleName()+".this";
	if (!b.hasObject(proposedName)) {
		b.declareSyntheticVariable(prevType, proposedName);
		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");
				}
			}
		}
	}
}
 
Example 2
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param isReferenced unused in this context but necessary for dispatch signature  
 */
protected void _toJavaStatement(XDoWhileExpression expr, ITreeAppendable b, boolean isReferenced) {
	boolean needsStatement = !canCompileToJavaExpression(expr.getPredicate(), b);
	String variable = null;
	if (needsStatement) {
		variable = b.declareSyntheticVariable(expr, "_dowhile");
		b.newLine().append("boolean ").append(variable).append(" = false;");
	}
	b.newLine().append("do {").increaseIndentation();
	internalToJavaStatement(expr.getBody(), b, false);
	if (needsStatement && !isEarlyExit(expr.getBody())) {
		internalToJavaStatement(expr.getPredicate(), b, true);
		b.newLine();
		b.append(variable).append(" = ");
		internalToJavaExpression(expr.getPredicate(), b);
		b.append(";");
	}
	b.decreaseIndentation().newLine().append("} while(");
	if (needsStatement) {
		b.append(variable);
	} else {
		internalToJavaExpression(expr.getPredicate(), b);
	}
	b.append(");");
}
 
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: 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 5
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 6
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected String declareSwitchResultVariable(XSwitchExpression expr, ITreeAppendable b, boolean isReferenced) {
	LightweightTypeReference type = getTypeForVariableDeclaration(expr);
	String switchResultName = b.declareSyntheticVariable(getSwitchExpressionKey(expr), "_switchResult");
	if (isReferenced) {
		b.newLine();
		b.append(type);
		b.append(" ").append(switchResultName).append(" = ");
		b.append(getDefaultValueLiteral(expr));
		b.append(";");
	}
	return switchResultName;
}
 
Example 7
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void generateCheckedExceptionHandling(ITreeAppendable appendable) {
	String name = appendable.declareSyntheticVariable(new Object(), "_e");
	appendable.decreaseIndentation().newLine().append("} catch (").append(Throwable.class).append(" ").append(name).append(") {").increaseIndentation();
	appendable.newLine().append("throw ");
	appendable.append(Exceptions.class);
	appendable.append(".sneakyThrow(");
	appendable.append(name);
	appendable.append(");");
	appendable.decreaseIndentation().newLine().append("}");
}
 
Example 8
Source File: AbstractXbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void declareFreshLocalVariable(XExpression expr, ITreeAppendable b, Later expression) {
	LightweightTypeReference type = getTypeForVariableDeclaration(expr);
	final String proposedName = makeJavaIdentifier(getFavoriteVariableName(expr));
	final String varName = b.declareSyntheticVariable(expr, proposedName);
	b.newLine();
	b.append(type);
	b.append(" ").append(varName).append(" = ");
	expression.exec(b);
	b.append(";");
}
 
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: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
protected void appendCatchAndFinally(XTryCatchFinallyExpression expr, ITreeAppendable b, boolean isReferenced) {
	final EList<XCatchClause> catchClauses = expr.getCatchClauses();
	final XExpression finallyExp = expr.getFinallyExpression();
	boolean isTryWithResources = !expr.getResources().isEmpty();
	// If Java 7 or better: use Java's try-with-resources
	boolean nativeTryWithResources = isAtLeast(b, JAVA7);
	final String throwablesStore = b.getName(Tuples.pair(expr, "_caughtThrowables"));

	// Catch
	if (!catchClauses.isEmpty()) {
		String variable = b.declareSyntheticVariable(Tuples.pair(expr, "_caughtThrowable"), "_t");
		b.append(" catch (final Throwable ").append(variable).append(") ");
		b.append("{").increaseIndentation().newLine();
		Iterator<XCatchClause> iterator = catchClauses.iterator();
		while (iterator.hasNext()) {
			XCatchClause catchClause = iterator.next();
			ITreeAppendable catchClauseAppendable = b.trace(catchClause);
			appendCatchClause(catchClause, isReferenced, variable, catchClauseAppendable);
			if (iterator.hasNext()) {
				b.append(" else ");
			}
		}
		b.append(" else {");
		b.increaseIndentation().newLine();
		if (isTryWithResources && !nativeTryWithResources) {
			b.append(throwablesStore + ".add(" + variable + ");").newLine();
		}
		appendSneakyThrow(expr, b, variable);
		closeBlock(b);
		closeBlock(b);
	}

	// Finally
	if (finallyExp != null || (!nativeTryWithResources && isTryWithResources)) {
		b.append(" finally {").increaseIndentation();
		if (finallyExp != null)
			internalToJavaStatement(finallyExp, b, false);
		if (!nativeTryWithResources && isTryWithResources)
			appendFinallyWithResources(expr, b);
		b.decreaseIndentation().newLine().append("}");
	}
}
 
Example 11
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 12
Source File: CacheMethodCompileStrategy.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void apply(ITreeAppendable appendable) {
	JvmOperation cacheMethod = (JvmOperation) logicalContainerProvider.getLogicalContainer(createExtensionInfo.getCreateExpression());
	JvmDeclaredType containerType = cacheMethod.getDeclaringType();
	IResolvedTypes resolvedTypes = typeResolver.resolveTypes(containerType);
	final ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, containerType);
	LightweightTypeReference listType = owner.newReferenceTo(ArrayList.class, new TypeReferenceInitializer<ParameterizedTypeReference>() {
		@Override
		public LightweightTypeReference enhance(ParameterizedTypeReference reference) {
			reference.addTypeArgument(owner.newWildcardTypeReference());
			return reference;
		}
	});
	String cacheVarName = cacheField.getSimpleName();
	String cacheKeyVarName = appendable.declareSyntheticVariable("CacheKey", "_cacheKey");
	appendable.append("final ").append(listType).append(" ").append(cacheKeyVarName)
		.append(" = ").append(CollectionLiterals.class).append(".newArrayList(");
	List<JvmFormalParameter> list = cacheMethod.getParameters();
	for (Iterator<JvmFormalParameter> iterator = list.iterator(); iterator.hasNext();) {
		JvmFormalParameter jvmFormalParameter = iterator.next();
		appendable.append(getVarName(jvmFormalParameter));
		if (iterator.hasNext()) {
			appendable.append(", ");
		}
	}
	appendable.append(");");
	// declare result variable
	LightweightTypeReference returnType = resolvedTypes.getActualType(initializerMethod.getParameters().get(0));
	if (returnType != null) {
		appendable.newLine().append("final ").append(returnType);
	} else {
		appendable.newLine().append("final Object");
	}
	String resultVarName = "_result";
	appendable.append(" ").append(resultVarName).append(";");
	// open synchronize block
	appendable.newLine().append("synchronized (").append(cacheVarName).append(") {");
	appendable.increaseIndentation();
	// if the cache contains the key return the previously created object.
	appendable.newLine().append("if (").append(cacheVarName).append(".containsKey(").append(cacheKeyVarName)
			.append(")) {");
	appendable.increaseIndentation();
	appendable.newLine().append("return ").append(cacheVarName).append(".get(").append(cacheKeyVarName).append(");");
	appendable.decreaseIndentation().newLine().append("}");
	
	// execute the creation
	compiler.toJavaStatement(createExtensionInfo.getCreateExpression(), appendable, true);
	appendable.newLine();
	appendable.append(resultVarName).append(" = ");
	compiler.toJavaExpression(createExtensionInfo.getCreateExpression(), appendable);
	appendable.append(";");

	// store the newly created object in the cache
	appendable.newLine().append(cacheVarName).append(".put(").append(cacheKeyVarName).append(", ");
	LightweightTypeReference fieldType = resolvedTypes.getActualType(cacheField);
	LightweightTypeReference declaredResultType = fieldType.getTypeArguments().get(1);
	boolean castRequired = false;
	if (!declaredResultType.isAssignableFrom(returnType)) {
		castRequired = true;
		appendable.append("(").append(declaredResultType).append(")");
	}
	appendable.append(resultVarName).append(");");

	// close synchronize block
	appendable.decreaseIndentation();
	appendable.newLine().append("}");
	appendable.newLine().append(initializerMethod.getSimpleName()).append("(").append(resultVarName);
	for (JvmFormalParameter parameter : cacheMethod.getParameters()) {
		appendable.append(", ").append(parameter.getName());
	}
	appendable.append(");");
	// return the result
	appendable.newLine().append("return ");
	if (castRequired) {
		appendable.append("(").append(declaredResultType).append(")");
	}
	appendable.append(resultVarName).append(";");
}