use of org.eclipse.xtext.xbase.compiler.output.ITreeAppendable in project xtext-xtend by eclipse.
the class XtendCompiler method _toJavaStatement.
@Override
protected void _toJavaStatement(final XConstructorCall expr, ITreeAppendable b, final boolean isReferenced) {
for (XExpression arg : expr.getArguments()) {
prepareExpression(arg, b);
}
if (!isReferenced) {
b.newLine();
constructorCallToJavaExpression(expr, b);
if (expr.eContainer() instanceof AnonymousClass) {
JvmConstructor constructor = expr.getConstructor();
JvmDeclaredType declaringType = constructor.getDeclaringType();
compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, b);
}
b.append(";");
} else if (isVariableDeclarationRequired(expr, b, true)) {
Later later = new Later() {
@Override
public void exec(ITreeAppendable appendable) {
constructorCallToJavaExpression(expr, appendable);
if (expr.eContainer() instanceof AnonymousClass) {
JvmConstructor constructor = expr.getConstructor();
JvmDeclaredType declaringType = constructor.getDeclaringType();
compileAnonymousClassBody((AnonymousClass) expr.eContainer(), declaringType, appendable);
}
}
};
declareFreshLocalVariable(expr, b, later);
}
}
use of org.eclipse.xtext.xbase.compiler.output.ITreeAppendable in project xtext-xtend by eclipse.
the class DispatchMethodCompileStrategy method apply.
@Override
public void apply(/* @Nullable */
ITreeAppendable a) {
if (a == null)
throw new IllegalArgumentException("a is never null");
boolean needsElse = true;
int parameterCount = dispatchOperation.getParameters().size();
List<JvmOperation> sortedDispatchOperations = sorter.getAllDispatchCases(dispatchOperation);
boolean[] allCasesSameType = new boolean[parameterCount];
for (int i = 0; i < parameterCount; i++) {
allCasesSameType[i] = true;
JvmTypeReference dispatchParameterType = dispatchOperation.getParameters().get(i).getParameterType();
for (JvmOperation operation : sortedDispatchOperations) {
JvmFormalParameter parameter = operation.getParameters().get(i);
JvmTypeReference caseParameterType = parameter.getParameterType();
if (!Strings.equal(dispatchParameterType.getIdentifier(), caseParameterType.getIdentifier())) {
allCasesSameType[i] = false;
break;
}
}
}
ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, dispatchOperation);
for (JvmOperation operation : sortedDispatchOperations) {
ITreeAppendable operationAppendable = treeAppendableUtil.traceSignificant(a, operation, true);
final List<Later> laters = newArrayList();
for (int i = 0; i < parameterCount; i++) {
final JvmFormalParameter dispatchParam = dispatchOperation.getParameters().get(i);
final LightweightTypeReference dispatchParamType = owner.toLightweightTypeReference(dispatchParam.getParameterType());
final JvmFormalParameter caseParam = operation.getParameters().get(i);
final LightweightTypeReference caseParamType = owner.toLightweightTypeReference(caseParam.getParameterType());
final String name = getVarName(dispatchParam, operationAppendable);
if (caseParamType.isType(Void.class)) {
laters.add(new Later() {
@Override
public void exec(ITreeAppendable appendable) {
appendable.append(name).append(" == null");
}
});
} else if (!allCasesSameType[i]) {
laters.add(new Later() {
@Override
public void exec(ITreeAppendable appendable) {
if (caseParamType.isAssignableFrom(dispatchParamType, new TypeConformanceComputationArgument(true, false, true, true, false, false)) && !dispatchParamType.isPrimitive()) {
appendable.append(name).append(" != null");
} else {
appendable.append(name).append(" instanceof ");
JvmType type = caseParamType.getWrapperTypeIfPrimitive().getType();
if (type == null) {
throw new IllegalStateException(String.valueOf(caseParamType));
}
appendable.append(type);
}
}
});
}
}
// if it's not the first if append an 'else'
if (sortedDispatchOperations.get(0) != operation) {
operationAppendable.append(" else ");
}
if (laters.isEmpty()) {
needsElse = false;
if (sortedDispatchOperations.size() != 1) {
operationAppendable.append("{").increaseIndentation();
operationAppendable.newLine();
}
} else {
operationAppendable.append("if (");
operationAppendable.increaseIndentation().increaseIndentation();
Iterator<Later> iterator = laters.iterator();
while (iterator.hasNext()) {
iterator.next().exec(operationAppendable);
if (iterator.hasNext()) {
operationAppendable.newLine().append(" && ");
}
}
operationAppendable.decreaseIndentation().decreaseIndentation();
operationAppendable.append(") {").increaseIndentation();
operationAppendable.newLine();
}
final boolean isCurrentVoid = typeReferences.is(operation.getReturnType(), Void.TYPE);
final boolean isDispatchVoid = typeReferences.is(dispatchOperation.getReturnType(), Void.TYPE);
if (isDispatchVoid) {
generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
// we generate a redundant return statement here to get a better debugging experience
operationAppendable.append(";").newLine().append("return;");
} else {
if (isCurrentVoid) {
generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
operationAppendable.append(";").newLine().append("return null");
} else {
operationAppendable.append("return ");
generateActualDispatchCall(dispatchOperation, operation, operationAppendable, owner);
}
operationAppendable.append(";");
}
if (sortedDispatchOperations.size() != 1) {
operationAppendable.decreaseIndentation();
a.newLine().append("}");
}
}
if (needsElse) {
a.append(" else {").increaseIndentation();
a.newLine();
a.increaseIndentation();
a.append("throw new IllegalArgumentException(\"Unhandled parameter types: \" +").newLine();
JvmType jvmType = typeReferences.findDeclaredType("java.util.Arrays", dispatchOperation);
if (jvmType != null) {
a.append(jvmType);
} else {
a.append(Arrays.class.getSimpleName());
}
a.append(".<Object>asList(");
Iterator<JvmFormalParameter> iterator = dispatchOperation.getParameters().iterator();
while (iterator.hasNext()) {
JvmFormalParameter parameter = iterator.next();
final String name = getVarName(parameter, a);
a.append(name);
if (iterator.hasNext()) {
a.append(", ");
}
}
a.append(").toString());");
a.decreaseIndentation();
a.decreaseIndentation().newLine().append("}");
}
}
use of org.eclipse.xtext.xbase.compiler.output.ITreeAppendable in project xtext-xtend by eclipse.
the class XtendReentrantTypeResolver method inferAnonymousClassConstructor.
protected JvmConstructor inferAnonymousClassConstructor(AnonymousClass anonymousClass, JvmGenericType inferredLocalClass, JvmConstructor superConstructor, boolean visible) {
JvmConstructor constructor = TypesFactory.eINSTANCE.createJvmConstructor();
inferredLocalClass.getMembers().add(constructor);
associator.associatePrimary(anonymousClass.getConstructorCall(), constructor);
if (visible) {
constructor.setVisibility(JvmVisibility.DEFAULT);
} else
constructor.setVisibility(JvmVisibility.PRIVATE);
constructor.setSimpleName(inferredLocalClass.getSimpleName());
constructor.setVarArgs(superConstructor.isVarArgs());
final List<JvmFormalParameter> parameters = superConstructor.getParameters();
for (JvmFormalParameter parameter : parameters) {
// trigger name computation
parameter.getName();
constructor.getParameters().add(typesBuilder.cloneWithProxies(parameter));
}
for (JvmTypeReference exception : superConstructor.getExceptions()) constructor.getExceptions().add(typesBuilder.cloneWithProxies(exception));
if (!parameters.isEmpty()) {
typesBuilder.setBody(constructor, new Procedures.Procedure1<ITreeAppendable>() {
@Override
public void apply(ITreeAppendable a) {
a.append("super(");
for (int i = 0; i < parameters.size(); i++) {
if (i != 0) {
a.append(", ");
}
a.append(parameters.get(i).getSimpleName());
}
a.append(");");
}
});
}
return constructor;
}
use of org.eclipse.xtext.xbase.compiler.output.ITreeAppendable in project xtext-xtend by eclipse.
the class XtendGenerator method compileLocalTypeStubs.
public void compileLocalTypeStubs(final JvmFeature feature, final ITreeAppendable appendable, final GeneratorConfig config) {
final Function1<JvmGenericType, Boolean> _function = (JvmGenericType it) -> {
boolean _isAnonymous = it.isAnonymous();
return Boolean.valueOf((!_isAnonymous));
};
final Consumer<JvmGenericType> _function_1 = (JvmGenericType it) -> {
appendable.newLine();
EObject _head = IterableExtensions.<EObject>head(this.getSourceElements(it));
final AnonymousClass anonymousClass = ((AnonymousClass) _head);
final ITreeAppendable childAppendable = appendable.trace(anonymousClass);
childAppendable.append("abstract class ");
this._treeAppendableUtil.traceSignificant(childAppendable, anonymousClass).append(it.getSimpleName());
boolean _isEmpty = it.getTypeParameters().isEmpty();
if (_isEmpty) {
childAppendable.append(" ");
}
this.generateExtendsClause(it, childAppendable, null);
childAppendable.append("{").increaseIndentation();
boolean _needSyntheticThisVariable = this.needSyntheticThisVariable(anonymousClass, it);
if (_needSyntheticThisVariable) {
Pair<String, JvmGenericType> _mappedTo = Pair.<String, JvmGenericType>of("this", it);
String _simpleName = it.getSimpleName();
String _plus = ("_this" + _simpleName);
final String thisName = childAppendable.declareSyntheticVariable(_mappedTo, _plus);
childAppendable.newLine().append("final ").append(it.getSimpleName()).append(" ").append(thisName).append(" = this;").newLine();
}
final Procedure1<LoopParams> _function_2 = (LoopParams it_1) -> {
final Function1<ITreeAppendable, ITreeAppendable> _function_3 = (ITreeAppendable it_2) -> {
return it_2.newLine();
};
it_1.setSeparator(_function_3);
};
final Procedure1<JvmMember> _function_3 = (JvmMember it_1) -> {
final ITreeAppendable memberAppendable = this._treeAppendableUtil.traceWithComments(childAppendable, it_1);
memberAppendable.openScope();
if ((it_1 instanceof JvmOperation)) {
final ITreeAppendable tracedAppendable = childAppendable.trace(it_1);
tracedAppendable.newLine();
this.generateJavaDoc(it_1, tracedAppendable, config);
this.generateVisibilityModifier(it_1, tracedAppendable);
tracedAppendable.append("abstract ");
this.generateTypeParameterDeclaration(((JvmTypeParameterDeclarator) it_1), tracedAppendable, null);
JvmTypeReference _returnType = ((JvmOperation) it_1).getReturnType();
boolean _tripleEquals = (_returnType == null);
if (_tripleEquals) {
tracedAppendable.append("void");
} else {
this._errorSafeExtensions.serializeSafely(((JvmOperation) it_1).getReturnType(), "Object", tracedAppendable);
}
tracedAppendable.append(" ");
this._treeAppendableUtil.traceSignificant(tracedAppendable, it_1).append(((JvmOperation) it_1).getSimpleName());
tracedAppendable.append("(");
this.generateParameters(((JvmExecutable) it_1), tracedAppendable, null);
tracedAppendable.append(")");
this.generateThrowsClause(((JvmExecutable) it_1), tracedAppendable, null);
tracedAppendable.append(";");
} else {
if ((it_1 instanceof JvmField)) {
final ITreeAppendable tracedAppendable_1 = childAppendable.trace(it_1);
tracedAppendable_1.newLine();
this.generateJavaDoc(it_1, tracedAppendable_1, config);
this.generateAnnotations(((JvmField) it_1).getAnnotations(), tracedAppendable_1, true, config);
if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
tracedAppendable_1.append("final ");
}
boolean _isStatic = ((JvmField) it_1).isStatic();
if (_isStatic) {
tracedAppendable_1.append("static ");
}
boolean _isTransient = ((JvmField) it_1).isTransient();
if (_isTransient) {
tracedAppendable_1.append("transient ");
}
boolean _isVolatile = ((JvmField) it_1).isVolatile();
if (_isVolatile) {
tracedAppendable_1.append("volatile ");
}
this._errorSafeExtensions.serializeSafely(((JvmField) it_1).getType(), "Object", tracedAppendable_1);
tracedAppendable_1.append(" ");
this._treeAppendableUtil.traceSignificant(tracedAppendable_1, it_1).append(((JvmField) it_1).getSimpleName());
if ((((JvmField) it_1).isFinal() && ((JvmField) it_1).isStatic())) {
Object _constantValue = ((JvmField) it_1).getConstantValue();
boolean _tripleNotEquals = (_constantValue != null);
if (_tripleNotEquals) {
tracedAppendable_1.append(" = ");
this.generateJavaConstant(((JvmField) it_1).getConstantValue(), tracedAppendable_1);
} else {
this.generateInitialization(((JvmField) it_1), tracedAppendable_1, config);
}
}
tracedAppendable_1.append(";");
} else {
this.generateMember(it_1, memberAppendable, config);
}
}
memberAppendable.closeScope();
};
this._loopExtensions.<JvmMember>forEach(childAppendable, this.getAddedDeclarations(it, anonymousClass), _function_2, _function_3);
childAppendable.decreaseIndentation().newLine().append("}");
appendable.newLine();
};
IterableExtensions.<JvmGenericType>filter(feature.getLocalClasses(), _function).forEach(_function_1);
}
Aggregations