use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class JavaCloneWriter method createReleaseStatement.
private Statement createReleaseStatement(VariableElement var) {
if (options.useARC()) {
TypeMirror voidType = typeUtil.getVoid();
FunctionElement element = new FunctionElement("JreRelease", voidType, null).addParameters(TypeUtil.ID_TYPE);
FunctionInvocation invocation = new FunctionInvocation(element, voidType);
invocation.addArgument(new SimpleName(var));
return new ExpressionStatement(invocation);
} else {
return new ExpressionStatement(new MethodInvocation(new ExecutablePair(releaseMethod), new SimpleName(var)));
}
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class ArrayRewriter method newArrayAccess.
private Expression newArrayAccess(ArrayAccess arrayAccessNode, TypeMirror componentType, TypeElement iosArrayElement, boolean assignable) {
String funcName = ElementUtil.getName(iosArrayElement) + "_Get";
TypeMirror returnType = componentType;
TypeMirror declaredReturnType = componentType.getKind().isPrimitive() ? componentType : TypeUtil.ID_TYPE;
if (assignable) {
funcName += "Ref";
returnType = declaredReturnType = new PointerType(componentType);
}
FunctionElement element = new FunctionElement(funcName, declaredReturnType, iosArrayElement).addParameters(iosArrayElement.asType(), typeUtil.getInt());
FunctionInvocation invocation = new FunctionInvocation(element, returnType);
invocation.addArgument(arrayAccessNode.getArray().copy());
invocation.addArgument(arrayAccessNode.getIndex().copy());
if (assignable) {
return new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, invocation);
}
return invocation;
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class Autoboxer method rewriteBoxedAssignment.
private void rewriteBoxedAssignment(Assignment node) {
Expression lhs = node.getLeftHandSide();
Expression rhs = node.getRightHandSide();
TypeMirror type = lhs.getTypeMirror();
TypeMirror primitiveType = typeUtil.unboxedType(type);
if (primitiveType == null) {
return;
}
TypeMirror pointerType = new PointerType(type);
String funcName = "JreBoxed" + getAssignFunctionName(node.getOperator()) + translationUtil.getOperatorFunctionModifier(lhs) + NameTable.capitalize(primitiveType.toString());
FunctionElement element = new FunctionElement(funcName, type, TypeUtil.asTypeElement(type)).addParameters(pointerType, primitiveType);
FunctionInvocation invocation = new FunctionInvocation(element, type);
invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
invocation.addArgument(TreeUtil.remove(rhs));
unbox(rhs);
node.replaceWith(invocation);
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class CastResolver method endVisit.
@Override
public void endVisit(CastExpression node) {
TypeMirror type = node.getType().getTypeMirror();
Expression expr = node.getExpression();
TypeMirror exprType = expr.getTypeMirror();
if (TypeUtil.isFloatingPoint(exprType)) {
// Java wouldn't allow a cast from primitive to non-primitive.
assert type.getKind().isPrimitive();
switch(type.getKind()) {
case LONG:
node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToLong", type));
return;
case CHAR:
node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToChar", type));
return;
case BYTE:
case SHORT:
case INT:
node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToInt", typeUtil.getInt()));
return;
// Fall through.
default:
}
}
// Lean on Java's type-checking.
if (!type.getKind().isPrimitive() && typeUtil.isAssignable(exprType, typeUtil.erasure(type))) {
node.replaceWith(TreeUtil.remove(expr));
return;
}
FunctionInvocation castCheck = createCastCheck(type, expr);
if (castCheck != null) {
node.setExpression(castCheck);
}
}
use of com.google.devtools.j2objc.ast.FunctionInvocation in project j2objc by google.
the class CastResolver method endVisit.
/**
* Adds a cast check to compareTo methods. This helps Comparable types behave
* well in sorted collections which rely on Java's runtime type checking.
*/
@Override
@SuppressWarnings("TypeEquals")
public void endVisit(MethodDeclaration node) {
ExecutableElement element = node.getExecutableElement();
if (!ElementUtil.getName(element).equals("compareTo") || node.getBody() == null) {
return;
}
DeclaredType comparableType = typeUtil.findSupertype(ElementUtil.getDeclaringClass(element).asType(), "java.lang.Comparable");
if (comparableType == null) {
return;
}
List<? extends TypeMirror> typeArguments = comparableType.getTypeArguments();
List<? extends VariableElement> parameters = element.getParameters();
if (typeArguments.size() != 1 || parameters.size() != 1 || !typeArguments.get(0).equals(parameters.get(0).asType())) {
return;
}
VariableElement param = node.getParameter(0).getVariableElement();
FunctionInvocation castCheck = createCastCheck(typeArguments.get(0), new SimpleName(param));
if (castCheck != null) {
node.getBody().addStatement(0, new ExpressionStatement(castCheck));
}
}
Aggregations