use of com.google.devtools.j2objc.types.FunctionElement 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.types.FunctionElement in project j2objc by google.
the class Functionizer method newFunctionElement.
private FunctionElement newFunctionElement(ExecutableElement method) {
TypeElement declaringClass = ElementUtil.getDeclaringClass(method);
FunctionElement element = new FunctionElement(nameTable.getFullFunctionName(method), method.getReturnType(), declaringClass);
if (ElementUtil.isConstructor(method) || !ElementUtil.isStatic(method)) {
element.addParameters(declaringClass.asType());
}
transferParams(method, element, declaringClass);
return element;
}
use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.
the class Functionizer method makeFunction.
/**
* Create an equivalent function declaration for a given method.
*/
private FunctionDeclaration makeFunction(MethodDeclaration method) {
ExecutableElement elem = method.getExecutableElement();
TypeElement declaringClass = ElementUtil.getDeclaringClass(elem);
boolean isInstanceMethod = !ElementUtil.isStatic(elem) && !ElementUtil.isConstructor(elem);
FunctionDeclaration function = new FunctionDeclaration(nameTable.getFullFunctionName(elem), elem.getReturnType());
function.setJniSignature(signatureGenerator.createJniFunctionSignature(elem));
function.setLineNumber(method.getLineNumber());
if (!ElementUtil.isStatic(elem)) {
VariableElement var = GeneratedVariableElement.newParameter(NameTable.SELF_NAME, declaringClass.asType(), null);
function.addParameter(new SingleVariableDeclaration(var));
}
TreeUtil.copyList(method.getParameters(), function.getParameters());
function.setModifiers(method.getModifiers() & Modifier.STATIC);
if (ElementUtil.isPrivate(elem) || (isInstanceMethod && !ElementUtil.isDefault(elem))) {
function.addModifiers(Modifier.PRIVATE);
} else {
function.addModifiers(Modifier.PUBLIC);
}
if (Modifier.isNative(method.getModifiers())) {
function.addModifiers(Modifier.NATIVE);
return function;
}
function.setBody(TreeUtil.remove(method.getBody()));
if (ElementUtil.isStatic(elem)) {
// Add class initialization invocation, since this may be the first use of this class.
String initName = UnicodeUtils.format("%s_initialize", nameTable.getFullName(declaringClass));
TypeMirror voidType = typeUtil.getVoid();
FunctionElement initElement = new FunctionElement(initName, voidType, declaringClass);
FunctionInvocation initCall = new FunctionInvocation(initElement, voidType);
function.getBody().addStatement(0, new ExpressionStatement(initCall));
} else {
FunctionConverter.convert(function);
}
return function;
}
use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.
the class Functionizer method newAllocatingConstructorElement.
private FunctionElement newAllocatingConstructorElement(ExecutableElement method) {
TypeElement declaringClass = ElementUtil.getDeclaringClass(method);
FunctionElement element = new FunctionElement(nameTable.getReleasingConstructorName(method), nameTable.getAllocatingConstructorName(method), declaringClass.asType(), declaringClass);
transferParams(method, element, declaringClass);
return element;
}
use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.
the class Functionizer method endVisit.
@Override
public void endVisit(ClassInstanceCreation node) {
ExecutableElement element = node.getExecutableElement();
TypeElement type = ElementUtil.getDeclaringClass(element);
FunctionElement funcElement = newAllocatingConstructorElement(element);
FunctionInvocation invocation = new FunctionInvocation(funcElement, node.getTypeMirror());
invocation.setHasRetainedResult(node.hasRetainedResult() || options.useARC());
List<Expression> args = invocation.getArguments();
Expression outerExpr = node.getExpression();
if (outerExpr != null) {
args.add(TreeUtil.remove(outerExpr));
} else if (captureInfo.needsOuterParam(type)) {
args.add(new ThisExpression(ElementUtil.getDeclaringClass(type).asType()));
}
Expression superOuterArg = node.getSuperOuterArg();
if (superOuterArg != null) {
args.add(TreeUtil.remove(superOuterArg));
}
TreeUtil.moveList(node.getCaptureArgs(), args);
TreeUtil.moveList(node.getArguments(), args);
node.replaceWith(invocation);
assert funcElement.getParameterTypes().size() == args.size();
}
Aggregations