Test run fix. Cpp types fix. NameTable.java to NameTableCpp.java rename

This commit is contained in:
aFedasenka 2012-10-08 21:31:20 +02:00
parent 3869e78d97
commit 98319a2d73
7 changed files with 182 additions and 96 deletions

View file

@ -40,7 +40,7 @@ import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import com.google.devtools.j2cpp.util.NameTable;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.Plugin;
import com.google.devtools.j2objc.gen.ObjectiveCHeaderGenerator;

View file

@ -26,7 +26,7 @@ import org.eclipse.jface.text.Document;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.devtools.j2cpp.util.NameTable;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.gen.ObjectiveCHeaderGenerator;

View file

@ -9,7 +9,7 @@ import org.eclipse.jdt.core.dom.CompilationUnit;
import com.google.common.io.Files;
import com.google.devtools.j2cpp.gen.CppHeaderGenerator;
import com.google.devtools.j2objc.J2ObjC.Language;
//import com.google.devtools.j2objc.J2ObjC.Language;
//import com.google.devtools.j2objc.gen.ObjectiveCImplementationGenerator;
public abstract class CppGenerationTest extends GenerationTest {

View file

@ -20,14 +20,14 @@ package com.google.devtools.j2cpp;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.J2ObjC.Language;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.gen.ObjectiveCHeaderGenerator;
import com.google.devtools.j2objc.gen.ObjectiveCImplementationGenerator;
import com.google.devtools.j2objc.gen.SourceBuilder;
import com.google.devtools.j2objc.gen.StatementGenerator;
import junit.framework.TestCase;
import org.eclipse.jdt.core.compiler.IProblem;

View file

@ -31,7 +31,7 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.devtools.j2cpp.util.NameTable;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.types.HeaderImportCollector;
@ -411,7 +411,7 @@ public class CppHeaderGenerator extends CppSourceFileGenerator{
private void printInstanceVariables(FieldDeclaration[] fields) {
indent();
String lastAccess = "@protected";
String lastAccess = "protected";
for (FieldDeclaration field : fields) {
if ((field.getModifiers() & Modifier.STATIC) == 0) {
@SuppressWarnings("unchecked")
@ -433,14 +433,14 @@ public class CppHeaderGenerator extends CppSourceFileGenerator{
print("__weak ");
}
ITypeBinding varType = Types.getTypeBinding(vars.get(0));
String objcType = NameTable.javaRefToObjC(varType);
boolean needsAsterisk = !varType.isPrimitive() && !objcType.matches("id|id<.*>|Class");
if (needsAsterisk && objcType.endsWith(" *")) {
String cppType = NameTable.javaRefToObjC(varType);
boolean needsAsterisk = !varType.isPrimitive() && !cppType.matches("id|id<.*>|Class");
if (needsAsterisk && cppType.endsWith(" *")) {
// Strip pointer from type, as it will be added when appending fragment.
// This is necessary to create "Foo *one, *two;" declarations.
objcType = objcType.substring(0, objcType.length() - 2);
cppType = cppType.substring(0, cppType.length() - 2);
}
print(objcType);
print(cppType);
print(' ');
for (Iterator<?> it = field.fragments().iterator(); it.hasNext(); ) {
VariableDeclarationFragment f = (VariableDeclarationFragment) it.next();
@ -459,45 +459,6 @@ public class CppHeaderGenerator extends CppSourceFileGenerator{
unindent();
}
private void printProperties(FieldDeclaration[] fields) {
int nPrinted = 0;
for (FieldDeclaration field : fields) {
if ((field.getModifiers() & Modifier.STATIC) == 0) {
ITypeBinding type = Types.getTypeBinding(field.getType());
@SuppressWarnings("unchecked")
List<VariableDeclarationFragment> vars = field.fragments(); // safe by definition
for (VariableDeclarationFragment var : vars) {
if (var.getName().getIdentifier().startsWith("this$") && superDefinesVariable(var)) {
// Don't print, as it shadows an inner field in a super class.
continue;
}
print("@property (nonatomic, ");
IVariableBinding varBinding = Types.getVariableBinding(var);
if (type.isPrimitive()) {
print("assign");
} else if (Types.isWeakReference(varBinding) ||
(varBinding.getName().startsWith("this$") &&
Types.hasWeakAnnotation(varBinding.getDeclaringClass()))) {
print(Options.useARC() ? "weak" : "assign");
} else if (type.isEqualTo(Types.getNSString())) {
print("copy");
} else {
print(Options.useARC() ? "strong" : "retain");
}
String typeString = NameTable.javaRefToObjC(type);
if (!typeString.endsWith("*")) {
typeString += " ";
}
println(String.format(") %s%s;", typeString, NameTable.getName(var.getName())));
nPrinted++;
}
}
}
if (nPrinted > 0) {
newline();
}
}
private void printConstantDefines(AbstractTypeDeclaration node) {
ITypeBinding type = Types.getTypeBinding(node);
boolean hadConstant = false;
@ -583,17 +544,17 @@ public class CppHeaderGenerator extends CppSourceFileGenerator{
if (Options.inlineFieldAccess()) {
// Need direct access to fields possibly from inner classes that are
// promoted to top level classes, so must make all fields public.
return "@public";
return "public";
}
if ((modifiers & Modifier.PUBLIC) > 0) {
return "@public";
return "public";
}
if ((modifiers & Modifier.PROTECTED) > 0) {
return "@protected";
return "protected";
}
if ((modifiers & Modifier.PRIVATE) > 0) {
return "@private";
return "private";
}
return "@package";
}

View file

@ -19,7 +19,8 @@ import org.eclipse.jdt.core.dom.TypeDeclaration;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import com.google.common.collect.Lists;
import com.google.devtools.j2cpp.util.NameTable;
import com.google.devtools.j2cpp.util.NameTableCpp;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2objc.gen.SourceFileGenerator;
import com.google.devtools.j2objc.types.IOSMethod;
import com.google.devtools.j2objc.types.IOSParameter;
@ -124,7 +125,7 @@ public abstract class CppSourceFileGenerator extends SourceFileGenerator {
baseDeclaration = "- (NSUInteger)hash";
} else {
baseDeclaration = String.format("%s (%s)%s", isStatic ? "static" : "",
NameTable.javaRefToCpp(method.getReturnType2()), mappedMethod.getName());
NameTableCpp.javaRefToObjC(method.getReturnType2()), mappedMethod.getName());
}
sb.append(baseDeclaration);
@ -163,7 +164,7 @@ public abstract class CppSourceFileGenerator extends SourceFileGenerator {
boolean isStatic = Modifier.isStatic(m.getModifiers());
IMethodBinding binding = Types.getMethodBinding(m);
String methodName = NameTable.getName(binding);
String baseDeclaration = String.format("\t %s %s %s", isStatic ? "static " : "", NameTable.javaRefToCpp(m.getReturnType2()), methodName);
String baseDeclaration = String.format("\t %s %s %s", isStatic ? "static " : "", NameTableCpp.javaRefToObjC(m.getReturnType2()), methodName);
sb.append(baseDeclaration);
@SuppressWarnings("unchecked")
List<SingleVariableDeclaration> params = m.parameters(); // safe by definition
@ -204,7 +205,7 @@ public abstract class CppSourceFileGenerator extends SourceFileGenerator {
boolean isTypeVariable = typeBinding.isTypeVariable();
String type = isTypeVariable ?
NameTable.getParameterTypeName(NameTable.ID_TYPE, typeBinding) :
NameTable.getParameterTypeName(NameTable.javaTypeToCpp(param.getType(), true), typeBinding);
NameTable.getParameterTypeName(NameTable.javaTypeToObjC(param.getType(), true), typeBinding);
sb.append(" ").append(type).append(" ").append(fieldName);
if (i<nParams-1) {
sb.append(",");
@ -240,11 +241,6 @@ public abstract class CppSourceFileGenerator extends SourceFileGenerator {
return name;
}
private String parameterKeyword(Type type, ITypeBinding typeBinding) {
String typeName = NameTable.javaTypeToCpp(type, true);
return parameterKeyword(typeName, typeBinding);
}
/**
* Returns a parameter name, which consists of a prefix ("with") and
* a type name that doesn't conflict with core names. For example,
@ -281,38 +277,6 @@ public abstract class CppSourceFileGenerator extends SourceFileGenerator {
return false;
}
/**
* Returns a function declaration string from a specified class and method.
*/
protected String makeFunctionDeclaration(AbstractTypeDeclaration cls,
MethodDeclaration method) {
StringBuffer sb = new StringBuffer();
Type returnType = method.getReturnType2();
ITypeBinding binding = Types.getTypeBinding(returnType);
if (binding.isEnum()) {
sb.append(NameTable.javaTypeToCpp(returnType, true));
} else {
sb.append(NameTable.javaRefToCpp(returnType));
}
sb.append(' ');
sb.append(NameTable.makeFunctionName(cls, method));
sb.append('(');
for (Iterator<?> iterator = method.parameters().iterator(); iterator.hasNext(); ) {
Object o = iterator.next();
if (o instanceof SingleVariableDeclaration) {
SingleVariableDeclaration param = (SingleVariableDeclaration) o;
String fieldType = NameTable.javaRefToCpp(param.getType());
String fieldName = param.getName().getIdentifier();
sb.append(String.format("%s %s", fieldType, fieldName));
if (iterator.hasNext()) {
sb.append(", ");
}
}
}
sb.append(')');
return sb.toString();
}
/**
* Returns true if a superclass also defines this variable.
*/

View file

@ -0,0 +1,161 @@
/*
* Copyright 2011 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.devtools.j2cpp.util;
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
import org.eclipse.jdt.core.dom.ArrayType;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.IPackageBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.ParameterizedType;
import org.eclipse.jdt.core.dom.PrimitiveType;
import org.eclipse.jdt.core.dom.Type;
import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.NameTable;
/**
* Singleton service for type/method/variable name support.
*
* @author Tom Ball
*/
public class NameTableCpp {
/**
* Return the Objective-C equivalent name for a Java primitive type.
*/
public static String primitiveTypeToObjC(PrimitiveType type) {
PrimitiveType.Code code = type.getPrimitiveTypeCode();
return primitiveTypeToObjC(code.toString());
}
private static String primitiveTypeToObjC(String javaName) {
if (javaName.equals("boolean")) {
return "bool"; // defined in NSObject.h
}
if (javaName.equals("byte")) {
// TODO change to appropriate type
return "signed short";
}
if (javaName.equals("char")) {
return "wchar_t";
}
if (javaName.equals("short")) {
return "signed short";
}
if (javaName.equals("long")) {
return "signed long";
}
if (javaName.equals("float")) {
return "float";
}
if (javaName.equals("double")) {
return "double";
}
// type name unchanged for int, float, double, and void
return javaName;
}
/**
* Convert a Java type into an equivalent Objective-C type.
*/
public static String javaTypeToObjC(Type type, boolean includeInterfaces) {
if (type instanceof PrimitiveType) {
return primitiveTypeToObjC((PrimitiveType) type);
}
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getType(); // erase parameterized type
}
if (type instanceof ArrayType) {
ITypeBinding arrayBinding = Types.getTypeBinding(type);
if (arrayBinding != null) {
ITypeBinding elementType = arrayBinding.getElementType();
return Types.resolveArrayType(elementType).getName();
}
}
ITypeBinding binding = Types.getTypeBinding(type);
return javaTypeToObjC(binding, includeInterfaces);
}
public static String javaTypeToObjC(ITypeBinding binding, boolean includeInterfaces) {
if (binding.isInterface() && !includeInterfaces || binding == Types.resolveIOSType("id") ||
binding == Types.resolveIOSType("NSObject")) {
return NameTable.ID_TYPE;
}
if (binding.isTypeVariable()) {
binding = binding.getErasure();
if (Types.isJavaObjectType(binding) || binding.isInterface()) {
return NameTable.ID_TYPE;
}
// otherwise fall-through
}
return NameTable.getFullName(binding);
}
public static String javaRefToObjC(Type type) {
return javaRefToObjC(Types.getTypeBinding(type));
}
public static String javaRefToObjC(ITypeBinding type) {
if (type.isPrimitive()) {
return primitiveTypeToObjC(type.getName());
}
String typeName = javaTypeToObjC(type, false);
if (typeName.equals(NameTable.ID_TYPE) || Types.isJavaVoidType(type)) {
if (type.isInterface()) {
return String.format("%s<%s>", NameTable.ID_TYPE, NameTable.getFullName(type));
}
return NameTable.ID_TYPE;
}
return typeName + " *";
}
/**
* Return the full name of a type, including its package. For outer types,
* is the type's full name; for example, java.lang.Object's full name is
* "JavaLangObject". For inner classes, the full name is their outer class'
* name plus the inner class name; for example, java.util.ArrayList.ListItr's
* name is "JavaUtilArrayList_ListItr".
*/
public static String getFullName(AbstractTypeDeclaration typeDecl) {
return getFullName(Types.getTypeBinding(typeDecl));
}
public static String getFullName(ITypeBinding binding) {
if (binding.isPrimitive()) {
return primitiveTypeToObjC(binding.getName());
}
binding = Types.mapType(binding.getErasure()); // Make sure type variables aren't included.
String suffix = binding.isEnum() ? "Enum" : "";
String prefix = "";
IMethodBinding outerMethod = binding.getDeclaringMethod();
if (outerMethod != null && !binding.isAnonymous()) {
prefix += "_" + outerMethod.getName();
}
ITypeBinding outerBinding = binding.getDeclaringClass();
if (outerBinding != null) {
while (outerBinding.isAnonymous()) {
prefix += "_" + outerBinding.getName();
outerBinding = outerBinding.getDeclaringClass();
}
String baseName = getFullName(outerBinding) + prefix + '_' + NameTable.getName(binding);
return outerBinding.isEnum() ? baseName : baseName + suffix;
}
IPackageBinding pkg = binding.getPackage();
String pkgName = pkg != null ? NameTable.getPrefix(pkg.getName()) : "";
return pkgName + binding.getName() + suffix;
}
}