use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class LabelRewriter method visit.
@Override
public boolean visit(MethodDeclaration node) {
// Rename any labels that have the same names; legal in Java but not C.
final Map<String, Integer> labelCounts = new HashMap<>();
node.accept(new TreeVisitor() {
@Override
public void endVisit(LabeledStatement labeledStatement) {
final String name = labeledStatement.getLabel().getIdentifier();
int value = labelCounts.containsKey(name) ? labelCounts.get(name) + 1 : 1;
labelCounts.put(name, value);
if (value > 1) {
final String newName = name + '_' + value;
labeledStatement.setLabel(new SimpleName(newName));
// Update references to this label.
labeledStatement.accept(new TreeVisitor() {
@Override
public void endVisit(ContinueStatement node) {
if (node.getLabel() != null && node.getLabel().getIdentifier().equals(name)) {
node.setLabel(new SimpleName(newName));
}
}
@Override
public void endVisit(BreakStatement node) {
if (node.getLabel() != null && node.getLabel().getIdentifier().equals(name)) {
node.setLabel(new SimpleName(newName));
}
}
});
}
}
});
return true;
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class OuterReferenceResolver method addCaptureArgs.
private void addCaptureArgs(TypeElement type, List<Expression> args) {
for (VariableElement var : captureInfo.getCapturedVars(type)) {
Expression path = getPathForLocalVar(var);
if (path == null) {
path = new SimpleName(var);
}
args.add(path);
}
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class CastResolver method createCastCheck.
private FunctionInvocation createCastCheck(TypeMirror type, Expression expr) {
type = typeUtil.erasure(type);
TypeMirror idType = TypeUtil.ID_TYPE;
if (TypeUtil.isInterface(type) || isObjectArray(type)) {
// Interfaces and object arrays requre a isInstance call.
FunctionElement element = new FunctionElement("cast_check", idType, null).addParameters(idType, TypeUtil.IOS_CLASS.asType());
FunctionInvocation invocation = new FunctionInvocation(element, idType);
invocation.addArgument(TreeUtil.remove(expr));
invocation.addArgument(new TypeLiteral(type, typeUtil));
return invocation;
} else if (TypeUtil.isArray(type) || TypeUtil.isDeclaredType(type)) {
// Primitive array and non-interface type casts are checked using Objective-C's
// isKindOfClass:.
TypeElement objcClass = typeUtil.getObjcClass(type);
FunctionElement checkFunction = new FunctionElement("cast_chk", idType, null).addParameters(idType, idType);
FunctionInvocation invocation = new FunctionInvocation(checkFunction, idType);
invocation.addArgument(TreeUtil.remove(expr));
ExecutableElement classElement = GeneratedExecutableElement.newMethodWithSelector("class", idType, objcClass).addModifiers(Modifier.STATIC);
MethodInvocation classInvocation = new MethodInvocation(new ExecutablePair(classElement), new SimpleName(objcClass));
invocation.addArgument(classInvocation);
return invocation;
}
return null;
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class EnumRewriter method addArcInitialization.
// ARC does not allow using "objc_constructInstance" so ARC code doesn't get
// the shared allocation optimization.
private void addArcInitialization(EnumDeclaration node) {
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
int i = 0;
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
VariableElement varElement = constant.getVariableElement();
ClassInstanceCreation creation = new ClassInstanceCreation(constant.getExecutablePair());
TreeUtil.copyList(constant.getArguments(), creation.getArguments());
creation.addArgument(new StringLiteral(ElementUtil.getName(varElement), typeUtil));
creation.addArgument(new NumberLiteral(i++, typeUtil));
creation.setHasRetainedResult(true);
stmts.add(new ExpressionStatement(new Assignment(new SimpleName(varElement), creation)));
}
}
use of com.google.devtools.j2objc.ast.SimpleName 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));
}
}
Aggregations