use of com.google.devtools.j2objc.ast.StringLiteral in project j2objc by google.
the class EnumRewriter method addNonArcInitialization.
private void addNonArcInitialization(EnumDeclaration node) {
TypeElement type = node.getTypeElement();
int baseTypeCount = 0;
List<Statement> sizeStatements = new ArrayList<>();
List<Statement> initStatements = new ArrayList<>();
TypeMirror voidType = typeUtil.getVoid();
VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
int i = 0;
for (EnumConstantDeclaration constant : node.getEnumConstants()) {
VariableElement varElement = constant.getVariableElement();
String varName = ElementUtil.getName(varElement);
ExecutableElement methodElement = constant.getExecutableElement();
TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
boolean isAnonymous = valueType != type;
String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
if (isAnonymous) {
sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
} else {
baseTypeCount++;
}
initStatements.add(new ExpressionStatement(new CommaExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))), new NativeExpression("ptr += " + sizeName, voidType))));
String initName = nameTable.getFullFunctionName(methodElement);
FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
initFunc.addArgument(new SimpleName(localEnum));
TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
initFunc.addArgument(new StringLiteral(varName, typeUtil));
initFunc.addArgument(new NumberLiteral(i++, typeUtil));
initStatements.add(new ExpressionStatement(initFunc));
}
List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
if (baseTypeCount == 0) {
stmts.add(new NativeStatement("size_t allocSize = 0;"));
} else {
stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
}
stmts.addAll(sizeStatements);
stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
stmts.add(new VariableDeclarationStatement(localEnum, null));
stmts.addAll(initStatements);
}
use of com.google.devtools.j2objc.ast.StringLiteral in project j2objc by google.
the class JavaToIOSMethodTranslator method visit.
@Override
public boolean visit(ClassInstanceCreation node) {
// translate any embedded method invocations
if (node.getExpression() != null) {
node.getExpression().accept(this);
}
for (Expression e : node.getArguments()) {
e.accept(this);
}
if (node.getAnonymousClassDeclaration() != null) {
node.getAnonymousClassDeclaration().accept(this);
}
ExecutableElement method = node.getExecutableElement();
String key = Mappings.getMethodKey(method, typeUtil);
String selector = Mappings.STRING_CONSTRUCTOR_TO_METHOD_MAPPINGS.get(key);
if (selector != null) {
assert !node.hasRetainedResult();
if (key.equals("java.lang.String.<init>(Ljava/lang/String;)V")) {
// Special case: replace new String(constant) to constant (avoid clang warning).
Expression arg = node.getArgument(0);
if (arg instanceof StringLiteral) {
node.replaceWith(arg.copy());
return false;
}
}
ExecutableElement newElement = GeneratedExecutableElement.newMappedMethod(selector, method);
MethodInvocation newInvocation = new MethodInvocation(new ExecutablePair(newElement), new SimpleName(ElementUtil.getDeclaringClass(method)));
TreeUtil.copyList(node.getArguments(), newInvocation.getArguments());
node.replaceWith(newInvocation);
}
return true;
}
use of com.google.devtools.j2objc.ast.StringLiteral in project j2objc by google.
the class TreeConverter method convertStringLiteral.
private static TreeNode convertStringLiteral(org.eclipse.jdt.core.dom.StringLiteral node) {
StringLiteral newNode = new StringLiteral();
convertExpression(node, newNode);
return newNode.setLiteralValue(node.getLiteralValue()).setTypeMirror(BindingConverter.getType(node.resolveTypeBinding()));
}
use of com.google.devtools.j2objc.ast.StringLiteral in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(InfixExpression node) {
InfixExpression.Operator op = node.getOperator();
List<Expression> operands = node.getOperands();
assert operands.size() >= 2;
if ((op.equals(InfixExpression.Operator.EQUALS) || op.equals(InfixExpression.Operator.NOT_EQUALS))) {
Expression lhs = operands.get(0);
Expression rhs = operands.get(1);
if (lhs instanceof StringLiteral || rhs instanceof StringLiteral) {
if (!(lhs instanceof StringLiteral)) {
// In case the lhs can't call isEqual.
lhs = operands.get(1);
rhs = operands.get(0);
}
buffer.append(op.equals(InfixExpression.Operator.NOT_EQUALS) ? "![" : "[");
lhs.accept(this);
buffer.append(" isEqual:");
rhs.accept(this);
buffer.append("]");
return false;
}
}
String opStr = ' ' + op.toString() + ' ';
boolean isFirst = true;
for (Expression operand : operands) {
if (!isFirst) {
buffer.append(opStr);
}
isFirst = false;
operand.accept(this);
}
return false;
}
use of com.google.devtools.j2objc.ast.StringLiteral in project j2objc by google.
the class AnnotationRewriter method createDescriptionMethod.
private MethodDeclaration createDescriptionMethod(TypeElement type) {
ExecutableElement descriptionElement = GeneratedExecutableElement.newMethodWithSelector("description", typeUtil.getJavaString().asType(), type);
MethodDeclaration descriptionMethod = new MethodDeclaration(descriptionElement);
descriptionMethod.setHasDeclaration(false);
Block descriptionBody = new Block();
descriptionMethod.setBody(descriptionBody);
descriptionBody.addStatement(new ReturnStatement(new StringLiteral("@" + elementUtil.getBinaryName(type) + "()", typeUtil)));
return descriptionMethod;
}
Aggregations