Skip to content

Commit

Permalink
Change FunctionBinding's declaringClass from TypeMirror to TypeElement.
Browse files Browse the repository at this point in the history
	Change on 2016/09/29 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=134667348
  • Loading branch information
kstanger authored and tomball committed Sep 29, 2016
1 parent 773974f commit b4bf716
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 45 deletions.
Expand Up @@ -37,15 +37,15 @@
import com.google.devtools.j2objc.types.GeneratedVariableBinding;
import com.google.devtools.j2objc.types.IOSMethodBinding;
import com.google.devtools.j2objc.types.IOSTypeBinding;
import com.google.devtools.j2objc.util.ElementUtil;
import com.google.devtools.j2objc.util.TranslationUtil;
import com.google.devtools.j2objc.util.UnicodeUtils;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.List;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.Modifier;

/**
* Rewrites array creation into a method invocation on an IOSArray class.
Expand Down Expand Up @@ -233,25 +233,25 @@ public boolean visit(Assignment node) {
@Override
public void endVisit(ArrayAccess node) {
TypeMirror componentType = node.getTypeMirror();
TypeMirror iosArrayBinding = typeEnv.resolveArrayType(componentType);
TypeElement iosArrayElement = typeEnv.resolveArrayType(componentType);

node.replaceWith(newArrayAccess(
node, componentType, iosArrayBinding, TranslationUtil.isAssigned(node)));
node, componentType, iosArrayElement, TranslationUtil.isAssigned(node)));
}

private Expression newArrayAccess(
ArrayAccess arrayAccessNode, TypeMirror componentType, TypeMirror iosArrayBinding,
ArrayAccess arrayAccessNode, TypeMirror componentType, TypeElement iosArrayElement,
boolean assignable) {
String funcName = iosArrayBinding.toString() + "_Get";
String funcName = ElementUtil.getName(iosArrayElement) + "_Get";
TypeMirror returnType = componentType;
TypeMirror declaredReturnType =
componentType.getKind().isPrimitive() ? componentType : typeEnv.resolveIOSTypeMirror("id");
componentType.getKind().isPrimitive() ? componentType : typeEnv.getIdTypeMirror();
if (assignable) {
funcName += "Ref";
returnType = declaredReturnType = typeEnv.getPointerType(componentType);
}
FunctionBinding binding = new FunctionBinding(funcName, declaredReturnType, iosArrayBinding);
binding.addParameters(iosArrayBinding, typeEnv.resolveJavaTypeMirror("int"));
FunctionBinding binding = new FunctionBinding(funcName, declaredReturnType, iosArrayElement);
binding.addParameters(iosArrayElement.asType(), typeEnv.resolveJavaTypeMirror("int"));
FunctionInvocation invocation = new FunctionInvocation(binding, returnType);
invocation.addArgument(arrayAccessNode.getArray().copy());
invocation.addArgument(arrayAccessNode.getIndex().copy());
Expand All @@ -274,10 +274,10 @@ private FunctionInvocation newArrayAssignment(
funcName = "IOSObjectArray_SetAndConsume";
value = retainedValue;
}
TypeMirror objArrayType = typeEnv.resolveIOSTypeMirror("IOSObjectArray");
TypeMirror idType = typeEnv.resolveIOSTypeMirror("id");
TypeElement objArrayType = typeEnv.getObjectArrayElement();
TypeMirror idType = typeEnv.getIdTypeMirror();
FunctionBinding binding = new FunctionBinding(funcName, idType, objArrayType);
binding.addParameters(objArrayType, typeEnv.resolveJavaTypeMirror("int"), idType);
binding.addParameters(objArrayType.asType(), typeEnv.resolveJavaTypeMirror("int"), idType);
FunctionInvocation invocation = new FunctionInvocation(binding, componentType);
List<Expression> args = invocation.getArguments();
args.add(TreeUtil.remove(arrayAccessNode.getArray()));
Expand Down
Expand Up @@ -50,6 +50,7 @@
import com.google.devtools.j2objc.util.BindingUtil;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.util.TranslationUtil;
import com.google.devtools.j2objc.util.TypeUtil;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
Expand Down Expand Up @@ -168,7 +169,7 @@ private void rewriteBoxedAssignment(Assignment node) {
String funcName = "JreBoxed" + getAssignFunctionName(node.getOperator())
+ TranslationUtil.getOperatorFunctionModifier(lhs)
+ NameTable.capitalize(primitiveType.toString());
FunctionBinding binding = new FunctionBinding(funcName, type, type);
FunctionBinding binding = new FunctionBinding(funcName, type, TypeUtil.asTypeElement(type));
binding.addParameters(pointerType, primitiveType);
FunctionInvocation invocation = new FunctionInvocation(binding, type);
invocation.addArgument(new PrefixExpression(
Expand Down Expand Up @@ -390,7 +391,7 @@ private void rewriteBoxedPrefixOrPostfix(TreeNode node, Expression operand, Stri
TypeMirror pointerType = typeEnv.getPointerType(type);
funcName = "JreBoxed" + funcName + TranslationUtil.getOperatorFunctionModifier(operand)
+ NameTable.capitalize(typeEnv.getPrimitiveType(type).toString());
FunctionBinding binding = new FunctionBinding(funcName, type, type);
FunctionBinding binding = new FunctionBinding(funcName, type, TypeUtil.asTypeElement(type));
binding.addParameters(pointerType);
FunctionInvocation invocation = new FunctionInvocation(binding, type);
invocation.addArgument(new PrefixExpression(
Expand Down
Expand Up @@ -117,7 +117,7 @@ private Expression rewriteFloatToIntegralCast(

private FunctionInvocation createCastCheck(ITypeBinding type, Expression expr) {
type = type.getErasure();
TypeMirror idType = typeEnv.resolveIOSTypeMirror("id");
TypeMirror idType = typeEnv.getIdTypeMirror();
FunctionInvocation invocation = null;
if ((type.isInterface() && !type.isAnnotation())
|| (type.isArray() && !type.getComponentType().isPrimitive())) {
Expand Down
Expand Up @@ -224,8 +224,7 @@ private void addDefaultMethodShim(String selector, ExecutablePair method) {
// if required.
TypeElement declaringClass = ElementUtil.getDeclaringClass(method.elem);
String name = nameTable.getFullFunctionName(method.elem);
FunctionBinding fb = new FunctionBinding(
name, method.elem.getReturnType(), declaringClass.asType());
FunctionBinding fb = new FunctionBinding(name, method.elem.getReturnType(), declaringClass);
fb.addParameters(declaringClass.asType());
fb.addParameters(((ExecutableType) method.elem.asType()).getParameterTypes());
FunctionInvocation invocation = new FunctionInvocation(fb, method.type.getReturnType());
Expand Down
Expand Up @@ -111,7 +111,7 @@ private Statement createReleaseStatement(IVariableBinding var) {
if (Options.useARC()) {
ITypeBinding voidType = typeEnv.resolveJavaType("void");
FunctionBinding binding = new FunctionBinding("JreRelease", voidType, null);
binding.addParameters(typeEnv.resolveIOSTypeMirror("id"));
binding.addParameters(typeEnv.getIdTypeMirror());
FunctionInvocation invocation = new FunctionInvocation(binding, voidType);
invocation.addArgument(new SimpleName(var));
return new ExpressionStatement(invocation);
Expand Down
Expand Up @@ -409,7 +409,7 @@ private void addNilCheck(Expression node) {
if (var != null) {
addSafeVar(var);
}
TypeMirror idType = typeEnv.resolveIOSTypeMirror("id");
TypeMirror idType = typeEnv.getIdTypeMirror();
FunctionBinding binding = new FunctionBinding("nil_chk", idType, null);
binding.addParameters(idType);
FunctionInvocation nilChkInvocation = new FunctionInvocation(binding, node.getTypeBinding());
Expand Down
Expand Up @@ -150,7 +150,7 @@ private void rewriteVolatileLoad(Expression node) {
IVariableBinding var = TreeUtil.getVariableBinding(node);
if (var != null && BindingUtil.isVolatile(var) && !TranslationUtil.isAssigned(node)) {
TypeMirror type = node.getTypeMirror();
TypeMirror idType = typeEnv.resolveIOSTypeMirror("id");
TypeMirror idType = typeEnv.getIdTypeMirror();
TypeMirror declaredType = type.getKind().isPrimitive() ? type : idType;
String funcName = "JreLoadVolatile" + NameTable.capitalize(declaredType.toString());
FunctionBinding binding = new FunctionBinding(funcName, declaredType, null);
Expand Down Expand Up @@ -409,7 +409,7 @@ private void rewriteStringAppend(Assignment node) {
List<Expression> operands = getStringAppendOperands(node);
Expression lhs = node.getLeftHandSide();
TypeMirror lhsType = lhs.getTypeMirror();
TypeMirror idType = typeEnv.resolveIOSTypeMirror("id");
TypeMirror idType = typeEnv.getIdTypeMirror();
String funcName = "JreStrAppend" + TranslationUtil.getOperatorFunctionModifier(lhs);
FunctionBinding binding = new FunctionBinding(funcName, idType, null);
binding.addParameters(
Expand Down
Expand Up @@ -35,6 +35,7 @@
import com.google.devtools.j2objc.types.FunctionBinding;
import com.google.devtools.j2objc.util.BindingUtil;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.util.TypeUtil;
import com.google.devtools.j2objc.util.UnicodeUtils;

import org.eclipse.jdt.core.dom.IMethodBinding;
Expand Down Expand Up @@ -113,7 +114,8 @@ public void endVisit(SuperMethodInvocation node) {
// Handle default method invocation: SomeInterface.super.method(...)
if (BindingUtil.isDefault(method)) {
FunctionBinding binding = new FunctionBinding(
nameTable.getFullFunctionName(method), exprType, typeEnv.getIdTypeMirror());
nameTable.getFullFunctionName(method), exprType,
BindingConverter.getTypeElement(method.getDeclaringClass()));
binding.addParameters(typeEnv.getIdTypeMirror());
binding.addParameters(method.getParameterTypes());
FunctionInvocation invocation = new FunctionInvocation(binding, exprType);
Expand All @@ -140,7 +142,7 @@ public void endVisit(SuperMethodInvocation node) {
superMethods.add(superMethod);

FunctionBinding binding = new FunctionBinding(
getSuperFunctionName(superMethod), exprType, qualifierType);
getSuperFunctionName(superMethod), exprType, TypeUtil.asTypeElement(qualifierType));
binding.addParameters(qualifierType, typeEnv.getIdTypeMirror());
binding.addParameters(method.getParameterTypes());
FunctionInvocation invocation = new FunctionInvocation(binding, exprType);
Expand Down
Expand Up @@ -18,6 +18,7 @@
import com.google.devtools.j2objc.jdt.BindingConverter;
import java.util.ArrayList;
import java.util.List;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;

/**
Expand All @@ -32,14 +33,13 @@ public class FunctionBinding {
// a retained result.
private final String retainedResultName;
private final TypeMirror returnType;
// TODO(kstanger): This should be and element.
private final TypeMirror declaringClass;
private final TypeElement declaringClass;
private List<TypeMirror> parameterTypes = new ArrayList<>();
private boolean isVarargs = false;

public FunctionBinding(
String name, String retainedResultName, TypeMirror returnType,
TypeMirror declaringClass) {
TypeElement declaringClass) {
this.name = name;
this.retainedResultName = retainedResultName;
this.returnType = returnType;
Expand All @@ -49,15 +49,15 @@ public FunctionBinding(
public FunctionBinding(String name, String retainedResultName, ITypeBinding returnType,
ITypeBinding declaringClass) {
this(name, retainedResultName, BindingConverter.getType(returnType),
BindingConverter.getType(declaringClass));
BindingConverter.getTypeElement(declaringClass));
}

public FunctionBinding(String name, ITypeBinding returnType, ITypeBinding declaringClass) {
this(name, null, BindingConverter.getType(returnType),
BindingConverter.getType(declaringClass));
BindingConverter.getTypeElement(declaringClass));
}

public FunctionBinding(String name, TypeMirror returnType, TypeMirror declaringClass) {
public FunctionBinding(String name, TypeMirror returnType, TypeElement declaringClass) {
this(name, null, returnType, declaringClass);
}

Expand All @@ -73,7 +73,7 @@ public TypeMirror getReturnType() {
return returnType;
}

public TypeMirror getDeclaringClass() {
public TypeElement getDeclaringClass() {
return declaringClass;
}

Expand Down
Expand Up @@ -50,17 +50,15 @@
import com.google.devtools.j2objc.ast.VariableDeclarationStatement;
import com.google.devtools.j2objc.jdt.BindingConverter;
import com.google.devtools.j2objc.util.BindingUtil;

import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.Set;

import javax.lang.model.element.TypeElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Modifier;

/**
* Collects the set of imports needed to resolve type references in an
Expand Down Expand Up @@ -96,6 +94,12 @@ private void addImports(Type type) {
}
}

private void addImports(TypeElement type) {
if (type != null) {
addImports(type.asType());
}
}

private void addImports(TypeMirror type) {
Import.addImports(BindingConverter.unwrapTypeMirrorIntoTypeBinding(type), imports, unit);
}
Expand Down
Expand Up @@ -251,10 +251,6 @@ public ITypeBinding resolveIOSType(String name) {
return iosBindingMap.get(name);
}

public TypeMirror resolveIOSTypeMirror(String name) {
return BindingConverter.getType(iosBindingMap.get(name));
}

public boolean isJavaObjectType(ITypeBinding type) {
return javaObjectType.equals(type);
}
Expand All @@ -280,10 +276,14 @@ public IOSTypeBinding resolveArrayType(ITypeBinding binding) {
return arrayBinding != null ? arrayBinding : IOSObjectArray;
}

public TypeMirror resolveArrayType(TypeMirror mirror) {
public TypeElement resolveArrayType(TypeMirror mirror) {
IOSTypeBinding arrayBinding = arrayBindingMap.get(
BindingConverter.unwrapTypeMirrorIntoBinding(mirror));
return BindingConverter.getType(arrayBinding != null ? arrayBinding : IOSObjectArray);
return BindingConverter.getTypeElement(arrayBinding != null ? arrayBinding : IOSObjectArray);
}

public TypeElement getObjectArrayElement() {
return BindingConverter.getTypeElement(IOSObjectArray);
}

public boolean isJavaVoidType(ITypeBinding type) {
Expand Down

0 comments on commit b4bf716

Please sign in to comment.