use of com.google.devtools.j2objc.ast.SimpleName 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.SimpleName 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);
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class EnumRewriter method addSimpleNonArcInitialization.
private void addSimpleNonArcInitialization(EnumDeclaration node) {
List<EnumConstantDeclaration> constants = node.getEnumConstants();
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", constants.size())));
stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
stmts.add(new VariableDeclarationStatement(localEnum, null));
StringBuffer sb = new StringBuffer("id names[] = {\n ");
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
sb.append("@\"" + ElementUtil.getName(constant.getVariableElement()) + "\", ");
}
sb.append("\n};");
stmts.add(new NativeStatement(sb.toString()));
TypeMirror intType = typeUtil.getInt();
GeneratedVariableElement loopCounterElement = GeneratedVariableElement.newLocalVar("i", intType, TreeUtil.getEnclosingElement(node));
VariableDeclarationExpression loopCounter = new VariableDeclarationExpression().setType(Type.newType(loopCounterElement.asType())).addFragment(new VariableDeclarationFragment(loopCounterElement, TreeUtil.newLiteral(0, typeUtil)));
Expression loopTest = new InfixExpression().setOperator(InfixExpression.Operator.LESS).setTypeMirror(intType).addOperand(new SimpleName(loopCounterElement)).addOperand(TreeUtil.newLiteral(constants.size(), typeUtil));
Expression loopUpdater = new PostfixExpression(loopCounterElement, PostfixExpression.Operator.INCREMENT);
Block loopBody = new Block();
stmts.add(new ForStatement().addInitializer(loopCounter).setExpression(loopTest).addUpdater(loopUpdater).setBody(loopBody));
String enumClassName = nameTable.getFullName(node.getTypeElement());
loopBody.addStatement(new NativeStatement("(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr), ptr += objSize);"));
loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class TreeConverter method convertFieldAccess.
private TreeNode convertFieldAccess(JCTree.JCFieldAccess node) {
String fieldName = node.name.toString();
SourcePosition pos = getPosition(node);
JCTree.JCExpression selected = node.getExpression();
if (fieldName.equals("this")) {
return new ThisExpression().setQualifier((Name) convert(selected)).setTypeMirror(node.sym.asType());
}
if ("super".equals(getMemberName(selected))) {
SuperFieldAccess newNode = new SuperFieldAccess().setVariableElement((VariableElement) node.sym).setName(convertSimpleName(node.sym, node.type, pos));
if (selected.getKind() == Kind.MEMBER_SELECT) {
newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) selected).getExpression()));
}
return newNode;
}
if (node.getIdentifier().toString().equals("class")) {
return new TypeLiteral(node.type).setType((Type) convertType(selected.type, pos, false).setPosition(getPosition(node)));
}
if (selected.getKind() == Kind.IDENTIFIER && (!node.sym.getKind().isField() || ElementUtil.isConstant((VariableElement) node.sym))) {
if (selected.toString().equals("this")) {
// Just return the constant.
return new SimpleName(node.sym);
}
JCIdent ident = (JCTree.JCIdent) selected;
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier(convertSimpleName(ident.sym, ident.type, pos)).setElement(node.sym);
}
if (selected.getKind() == Kind.MEMBER_SELECT) {
TreeNode newSelected = convertFieldAccess((JCTree.JCFieldAccess) selected).setPosition(pos);
if (newSelected.getKind() == TreeNode.Kind.QUALIFIED_NAME) {
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((QualifiedName) newSelected).setElement(node.sym);
}
}
if (ElementUtil.isConstant((VariableElement) node.sym) && ElementUtil.isStatic(node.sym) && !(selected.getKind() == Kind.METHOD_INVOCATION)) {
return new QualifiedName().setName(convertSimpleName(node.sym, node.type, pos)).setQualifier((Name) convert(selected)).setElement(node.sym);
}
return new FieldAccess().setVariableElement((VariableElement) node.sym).setExpression((Expression) convert(selected)).setName(convertSimpleName(node.sym, node.type, pos).setTypeMirror(node.type));
}
use of com.google.devtools.j2objc.ast.SimpleName in project j2objc by google.
the class TreeConverter method convertBreakStatement.
private TreeNode convertBreakStatement(JCTree.JCBreak node) {
BreakStatement newNode = new BreakStatement();
Object label = node.getLabel();
if (label != null) {
newNode.setLabel((SimpleName) new SimpleName(label.toString()).setPosition(getPosition(node)));
}
return newNode;
}
Aggregations