Skip to content

Commit

Permalink
Complete type system migration in LambdaRewriter.
Browse files Browse the repository at this point in the history
	Change on 2016/10/24 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=137103354
  • Loading branch information
kstanger authored and tomball committed Nov 4, 2016
1 parent 5a1f4ca commit 384a940
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
Expand Up @@ -558,8 +558,7 @@ private void printMethodDeclaration(MethodDeclaration m, boolean isCompanionClas
JavadocGenerator.printDocComment(getBuilder(), m.getJavadoc());
print(getMethodSignature(m));
String methodName = nameTable.getMethodSelector(methodBinding);
if (!m.isConstructor() && !BindingUtil.isSynthetic(m.getModifiers())
&& NameTable.needsObjcMethodFamilyNoneAttribute(methodName)) {
if (!m.isConstructor() && NameTable.needsObjcMethodFamilyNoneAttribute(methodName)) {
// Getting around a clang warning.
// clang assumes that methods with names starting with new, alloc or copy
// return objects of the same type as the receiving class, regardless of
Expand Down
Expand Up @@ -131,8 +131,8 @@ private boolean canFunctionize(MethodDeclaration node) {
int modifiers = node.getModifiers();

// Never functionize these types of methods.
if (Modifier.isStatic(modifiers) || Modifier.isAbstract(modifiers)
|| BindingUtil.isSynthetic(modifiers) || m.isAnnotationMember()) {
if (Modifier.isStatic(modifiers) || Modifier.isAbstract(modifiers) || !node.hasDeclaration()
|| m.isAnnotationMember()) {
return false;
}

Expand Down Expand Up @@ -254,9 +254,9 @@ public void endVisit(ClassInstanceCreation node) {
@Override
public void endVisit(MethodDeclaration node) {
IMethodBinding binding = node.getMethodBinding();
// Don't functionize synthetic methods like dealloc or __annotations, since
// Don't functionize certain ObjC methods like dealloc or __annotations, since
// they are added by the translator and need to remain in method form.
if (BindingUtil.isSynthetic(binding)) {
if (!node.hasDeclaration()) {
return;
}
boolean isInstanceMethod = !BindingUtil.isStatic(binding) && !binding.isConstructor();
Expand Down
Expand Up @@ -40,10 +40,8 @@
import com.google.devtools.j2objc.ast.TypeMethodReference;
import com.google.devtools.j2objc.ast.UnitTreeVisitor;
import com.google.devtools.j2objc.ast.VariableDeclaration;
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.types.GeneratedMethodBinding;
import com.google.devtools.j2objc.types.GeneratedExecutableElement;
import com.google.devtools.j2objc.types.GeneratedVariableElement;
import com.google.devtools.j2objc.types.IOSMethodBinding;
import com.google.devtools.j2objc.util.CaptureInfo;
import com.google.devtools.j2objc.util.ElementUtil;
import com.google.devtools.j2objc.util.TypeUtil;
Expand Down Expand Up @@ -81,6 +79,7 @@ private class RewriteContext {
private ExecutableElement functionalInterface;
private ExecutableType functionalInterfaceType;
private TypeDeclaration typeDecl;
private GeneratedExecutableElement implElement;
private MethodDeclaration implDecl;
private ClassInstanceCreation creation;

Expand Down Expand Up @@ -120,25 +119,22 @@ private void createTypeDeclaration() {

private void createImplementation() {
String selector = nameTable.getMethodSelector(functionalInterface);
IOSMethodBinding implBinding = IOSMethodBinding.newMappedMethod(
selector, BindingConverter.unwrapExecutableElement(functionalInterface));
implBinding.setDeclaringClass(BindingConverter.unwrapTypeElement(lambdaType));
implBinding.removeModifiers(java.lang.reflect.Modifier.ABSTRACT);
implDecl = new MethodDeclaration(implBinding);
implElement = GeneratedExecutableElement.newMethodWithSelector(
selector, functionalInterface.getReturnType(), lambdaType);
implDecl = new MethodDeclaration(implElement);
typeDecl.addBodyDeclaration(implDecl);
}

private void createCreation() {
GeneratedMethodBinding constructorBinding = GeneratedMethodBinding.newConstructor(
BindingConverter.unwrapTypeElement(lambdaType), java.lang.reflect.Modifier.PRIVATE,
typeEnv);
ExecutableElement constructorElement =
GeneratedExecutableElement.newConstructor(lambdaType, typeUtil);

// Add the implicit constructor to call.
MethodDeclaration constructorDecl = new MethodDeclaration(constructorBinding);
MethodDeclaration constructorDecl = new MethodDeclaration(constructorElement);
constructorDecl.setBody(new Block());
typeDecl.addBodyDeclaration(constructorDecl);

creation = new ClassInstanceCreation(constructorBinding, Type.newType(lambdaType.asType()));
creation = new ClassInstanceCreation(constructorElement, Type.newType(lambdaType.asType()));
creation.setExpression(TreeUtil.remove(node.getLambdaOuterArg()));
TreeUtil.moveList(node.getLambdaCaptureArgs(), creation.getCaptureArgs());
}
Expand Down Expand Up @@ -180,7 +176,9 @@ private Block asImplementationBlock(Expression expr) {

private void rewriteLambdaExpression(LambdaExpression node) {
for (VariableDeclaration decl : node.getParameters()) {
implDecl.addParameter(new SingleVariableDeclaration(decl.getVariableElement()));
VariableElement var = decl.getVariableElement();
implElement.addParameter(var);
implDecl.addParameter(new SingleVariableDeclaration(var));
}
setImplementationBody(TreeUtil.remove(node.getBody()));
}
Expand All @@ -193,6 +191,7 @@ private Iterator<VariableElement> createParameters() {
GeneratedVariableElement param = new GeneratedVariableElement(
getParamName(i++), type, ElementKind.PARAMETER, null);
params.add(param);
implElement.addParameter(param);
implDecl.addParameter(new SingleVariableDeclaration(param));
}
return params.iterator();
Expand Down
Expand Up @@ -20,6 +20,8 @@
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.util.BindingUtil;
import com.google.devtools.j2objc.util.ElementUtil;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.util.TypeUtil;
import java.util.Collection;
import java.util.List;
import javax.lang.model.element.AnnotationValue;
Expand All @@ -28,6 +30,7 @@
import javax.lang.model.element.ElementVisitor;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.TypeParameterElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
Expand Down Expand Up @@ -75,6 +78,13 @@ public static GeneratedExecutableElement newMethodWithSelector(
selector, selector, ElementKind.METHOD, returnType, enclosingElement, false);
}

public static GeneratedExecutableElement newConstructor(
TypeElement enclosingElement, TypeUtil typeUtil) {
return new GeneratedExecutableElement(
NameTable.INIT_NAME, null, ElementKind.CONSTRUCTOR, typeUtil.getVoidType(),
enclosingElement, false);
}

/**
* Clone a method element, so parameters can be added to it.
*/
Expand Down

0 comments on commit 384a940

Please sign in to comment.