use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class OcniExtractor method endVisit.
@Override
public void endVisit(MethodDeclaration node) {
int modifiers = node.getModifiers();
if (Modifier.isNative(modifiers)) {
NativeStatement nativeStmt = extractNativeStatement(node);
if (nativeStmt != null) {
Block body = new Block();
body.addStatement(nativeStmt);
node.setBody(body);
node.removeModifiers(Modifier.NATIVE);
}
}
if (Modifier.isSynchronized(modifiers)) {
TypeElement declaringClass = ElementUtil.getDeclaringClass(node.getExecutableElement());
SynchronizedStatement syncStmt = new SynchronizedStatement(Modifier.isStatic(modifiers) ? new TypeLiteral(declaringClass.asType(), typeUtil) : new ThisExpression(declaringClass.asType()));
syncStmt.setBody(TreeUtil.remove(node.getBody()));
Block newBody = new Block();
newBody.addStatement(syncStmt);
node.setBody(newBody);
node.removeModifiers(Modifier.SYNCHRONIZED);
}
}
use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class AbstractMethodRewriter method endVisit.
@Override
public void endVisit(MethodDeclaration node) {
ExecutableElement methodElement = node.getExecutableElement();
if (!ElementUtil.isAbstract(methodElement)) {
return;
}
// JDT only adds the abstract bit to a MethodDeclaration node's modifiers if the abstract
// method is from a class. Since we want our code generator to go over an interface's
// method nodes for default method support and skip abstract methods, we add the bit if the
// method is from an interface.
TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
if (declaringClass.getKind().isInterface()) {
node.addModifiers(java.lang.reflect.Modifier.ABSTRACT);
return;
}
// we skip the stubbing out.
if (!translationUtil.needsReflection(declaringClass)) {
unit.setHasIncompleteProtocol();
unit.setHasIncompleteImplementation();
return;
}
Block body = new Block();
// Generate a body which throws a NSInvalidArgumentException.
String bodyCode = "// can't call an abstract method\n" + "[self doesNotRecognizeSelector:_cmd];";
if (!TypeUtil.isVoid(node.getReturnTypeMirror())) {
// Never executes, but avoids a gcc warning.
bodyCode += "\nreturn 0;";
}
body.addStatement(new NativeStatement(bodyCode));
node.setBody(body);
node.removeModifiers(java.lang.reflect.Modifier.ABSTRACT);
}
use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class AnnotationRewriter method addConstructor.
private void addConstructor(AnnotationTypeDeclaration node, Map<ExecutableElement, VariableElement> fieldElements) {
TypeElement type = node.getTypeElement();
String typeName = nameTable.getFullName(type);
FunctionDeclaration constructorDecl = new FunctionDeclaration("create_" + typeName, type.asType());
Block constructorBody = new Block();
constructorDecl.setBody(constructorBody);
List<Statement> stmts = constructorBody.getStatements();
stmts.add(new NativeStatement(UnicodeUtils.format("%s *self = AUTORELEASE([[%s alloc] init]);", typeName, typeName)));
for (ExecutableElement memberElement : ElementUtil.getSortedAnnotationMembers(type)) {
TypeMirror memberType = memberElement.getReturnType();
String propName = NameTable.getAnnotationPropertyName(memberElement);
String fieldName = nameTable.getVariableShortName(fieldElements.get(memberElement));
VariableElement param = GeneratedVariableElement.newParameter(propName, memberType, null);
constructorDecl.addParameter(new SingleVariableDeclaration(param));
String rhs = TypeUtil.isReferenceType(memberType) ? "RETAIN_(" + propName + ")" : propName;
stmts.add(new NativeStatement("self->" + fieldName + " = " + rhs + ";"));
}
stmts.add(new NativeStatement("return self;"));
node.addBodyDeclaration(constructorDecl);
}
use of com.google.devtools.j2objc.ast.NativeStatement in project j2objc by google.
the class SuperMethodInvocationRewriter method endVisit.
@Override
public void endVisit(CompilationUnit unit) {
for (SuperMethodElementPair superMethod : superMethods) {
String funcName = getSuperFunctionName(superMethod);
String signature = getSuperFunctionSignature(superMethod.method);
// Add declarations for the function pointers to call.
unit.addNativeBlock(NativeDeclaration.newOuterDeclaration(null, "static " + UnicodeUtils.format(signature, funcName) + ";"));
// Look up the implementations in the static initialization.
AbstractTypeDeclaration typeNode = typeMap.get(superMethod.type);
assert typeNode != null : "Type is expected to be in this compilation unit";
String superclassName = nameTable.getFullName(ElementUtil.getSuperclass(superMethod.type));
typeNode.addClassInitStatement(0, new NativeStatement(UnicodeUtils.format("%s = (%s)[%s instanceMethodForSelector:@selector(%s)];", funcName, UnicodeUtils.format(signature, ""), superclassName, nameTable.getMethodSelector(superMethod.method))));
}
}
use of com.google.devtools.j2objc.ast.NativeStatement 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);"));
}
Aggregations