use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class SuperMethodInvocationRewriter method endVisit.
@Override
public void endVisit(SuperMethodInvocation node) {
Expression receiver = node.getReceiver();
ExecutableElement method = node.getExecutableElement();
TypeMirror exprType = node.getTypeMirror();
// Handle default method invocation: SomeInterface.super.method(...)
if (ElementUtil.isDefault(method)) {
FunctionElement element = new FunctionElement(nameTable.getFullFunctionName(method), exprType, ElementUtil.getDeclaringClass(method)).addParameters(TypeUtil.ID_TYPE).addParameters(ElementUtil.asTypes(method.getParameters()));
FunctionInvocation invocation = new FunctionInvocation(element, exprType);
List<Expression> args = invocation.getArguments();
if (receiver == null) {
args.add(new ThisExpression(TreeUtil.getEnclosingTypeElement(node).asType()));
} else {
// OuterReferenceResolver has provided an outer path.
args.add(TreeUtil.remove(receiver));
}
TreeUtil.copyList(node.getArguments(), args);
node.replaceWith(invocation);
return;
}
if (receiver == null) {
return;
}
VariableElement var = TreeUtil.getVariableElement(receiver);
assert var != null : "Expected receiver to be a variable";
TypeMirror receiverType = var.asType();
TypeElement receiverElem = TypeUtil.asTypeElement(receiverType);
SuperMethodElementPair superMethod = new SuperMethodElementPair(receiverElem, method);
superMethods.add(superMethod);
FunctionElement element = new FunctionElement(getSuperFunctionName(superMethod), exprType, receiverElem).addParameters(receiverType, TypeUtil.ID_TYPE).addParameters(ElementUtil.asTypes(method.getParameters()));
FunctionInvocation invocation = new FunctionInvocation(element, exprType);
List<Expression> args = invocation.getArguments();
args.add(TreeUtil.remove(receiver));
String selectorExpr = UnicodeUtils.format("@selector(%s)", nameTable.getMethodSelector(method));
args.add(new NativeExpression(selectorExpr, TypeUtil.ID_TYPE));
TreeUtil.copyList(node.getArguments(), args);
node.replaceWith(invocation);
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class Functionizer method setFunctionCaller.
/**
* Replace method block statements with single statement that invokes function.
*/
private void setFunctionCaller(MethodDeclaration method, ExecutableElement methodElement) {
TypeMirror returnType = methodElement.getReturnType();
TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
Block body = new Block();
method.setBody(body);
method.removeModifiers(Modifier.NATIVE);
List<Statement> stmts = body.getStatements();
FunctionInvocation invocation = new FunctionInvocation(newFunctionElement(methodElement), returnType);
List<Expression> args = invocation.getArguments();
if (!ElementUtil.isStatic(methodElement)) {
args.add(new ThisExpression(declaringClass.asType()));
}
for (SingleVariableDeclaration param : method.getParameters()) {
args.add(new SimpleName(param.getVariableElement()));
}
if (TypeUtil.isVoid(returnType)) {
stmts.add(new ExpressionStatement(invocation));
if (ElementUtil.isConstructor(methodElement)) {
stmts.add(new ReturnStatement(new ThisExpression(declaringClass.asType())));
}
} else {
stmts.add(new ReturnStatement(invocation));
}
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(ConstructorInvocation node) {
ExecutableElement element = node.getExecutableElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
FunctionElement funcElement = newFunctionElement(element);
FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
List<Expression> args = invocation.getArguments();
args.add(new ThisExpression(declaringClass.asType()));
for (VariableElement captureParam : captureInfo.getImplicitPrefixParams(declaringClass)) {
args.add(new SimpleName(captureParam));
}
TreeUtil.moveList(node.getArguments(), args);
for (VariableElement captureParam : captureInfo.getImplicitPostfixParams(declaringClass)) {
args.add(new SimpleName(captureParam));
}
node.replaceWith(new ExpressionStatement(invocation));
assert funcElement.getParameterTypes().size() == args.size();
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(MethodInvocation node) {
ExecutableElement element = node.getExecutableElement();
if (!ElementUtil.isStatic(element) && !functionizableMethods.contains(element)) {
return;
}
FunctionInvocation functionInvocation = new FunctionInvocation(newFunctionElement(element), node.getTypeMirror());
List<Expression> args = functionInvocation.getArguments();
TreeUtil.moveList(node.getArguments(), args);
if (!ElementUtil.isStatic(element)) {
Expression expr = node.getExpression();
if (expr == null) {
expr = new ThisExpression(TreeUtil.getEnclosingTypeElement(node).asType());
}
args.add(0, TreeUtil.remove(expr));
}
node.replaceWith(functionInvocation);
}
use of com.google.devtools.j2objc.ast.ThisExpression in project j2objc by google.
the class DestructorGenerator method createRelease.
private Statement createRelease(VariableElement var) {
TypeMirror varType = var.asType();
if (ElementUtil.isStatic(var) || varType.getKind().isPrimitive() || ElementUtil.isWeakReference(var)) {
return null;
}
boolean isVolatile = ElementUtil.isVolatile(var);
boolean isRetainedWith = ElementUtil.isRetainedWithField(var);
String funcName = null;
if (isRetainedWith) {
funcName = isVolatile ? "JreVolatileRetainedWithRelease" : "JreRetainedWithRelease";
} else if (isVolatile) {
funcName = "JreReleaseVolatile";
} else if (options.useReferenceCounting()) {
funcName = "RELEASE_";
}
if (funcName == null) {
return null;
}
TypeMirror voidType = typeUtil.getVoid();
TypeMirror idType = TypeUtil.ID_TYPE;
FunctionElement element = new FunctionElement(funcName, voidType, null);
FunctionInvocation releaseInvocation = new FunctionInvocation(element, voidType);
if (isRetainedWith) {
element.addParameters(idType);
releaseInvocation.addArgument(new ThisExpression(ElementUtil.getDeclaringClass(var).asType()));
}
element.addParameters(isVolatile ? TypeUtil.ID_PTR_TYPE : idType);
Expression arg = new SimpleName(var);
if (isVolatile) {
arg = new PrefixExpression(new PointerType(varType), PrefixExpression.Operator.ADDRESS_OF, arg);
}
releaseInvocation.addArgument(arg);
return new ExpressionStatement(releaseInvocation);
}
Aggregations