current header and cpp file fix

This commit is contained in:
aFedasenka 2012-10-12 00:06:33 +02:00
parent df62e3c8d3
commit 9bedc21b7c
7 changed files with 2232 additions and 56 deletions

View file

@ -18,16 +18,16 @@ package com.google.devtools.j2cpp;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import com.google.devtools.j2objc.J2ObjC;
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;
@ -39,15 +39,15 @@ import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.Statement;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.devtools.j2cpp.gen.CppStatementGenerator;
import com.google.devtools.j2objc.J2ObjC;
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;
/**
* Tests code generation. A string containing the source code for a list of Java
@ -184,7 +184,7 @@ public abstract class GenerationTest extends TestCase {
}
protected String generateStatement(Statement statement) {
return StatementGenerator.generate(statement,
return CppStatementGenerator.generate(statement,
Collections.<IVariableBinding>emptySet(), false, SourceBuilder.BEGINNING_OF_FILE).trim();
}

View file

@ -83,14 +83,12 @@ public class CppHeaderGenerator extends CppSourceFileGenerator{
String typeName = NameTableCpp.getFullName(node);
String superName = NameTable.getSuperClassName(node);
//DONE if extends superclass other than Object
//TODO if implements interfaces
if ((superName != null) && (!superName.equals("NSObject"))) {
printf(": public %s", superName);
}
printConstantDefines(node);
//TODO if implements interfaces
printf("class %s", typeName);
if ((superName != null) && (!superName.equals("NSObject"))) {
printf(" : %s", superName);
}
println(" {");
printInstanceVariables(node.getFields());
@ -271,7 +269,7 @@ public class CppHeaderGenerator extends CppSourceFileGenerator{
}
protected String createForwardDeclaration(String typeName, boolean isInterface) {
return String.format("@%s %s;", isInterface ? "TODO: change to abstract class \ninterface" : "class", typeName);
return String.format("%s %s;", isInterface ? "TODO: change to abstract class \ninterface" : "class", NameTableCpp.basicTypeToCpp(typeName));
}
protected String createImport(ImportCollector.Import imp) {

View file

@ -44,8 +44,7 @@ import com.google.common.collect.Sets;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.Options;
import com.google.devtools.j2objc.gen.HiddenFieldDetector;
import com.google.devtools.j2objc.gen.ObjectiveCSourceFileGenerator;
import com.google.devtools.j2objc.gen.StatementGenerator;
import com.google.devtools.j2cpp.gen.CppStatementGenerator;
import com.google.devtools.j2objc.types.IOSMethod;
import com.google.devtools.j2objc.types.ImplementationImportCollector;
import com.google.devtools.j2objc.types.ImportCollector;
@ -53,12 +52,13 @@ import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.ErrorReportingASTVisitor;
import com.google.devtools.j2objc.util.NameTable;
import com.google.devtools.j2cpp.gen.CppSourceFileGenerator;
/**
* Generates C++ implementation (.m) files from compilation units.
*
* @author Tom Ball
*/
public class CppImplementationGenerator extends ObjectiveCSourceFileGenerator {
public class CppImplementationGenerator extends CppSourceFileGenerator {
private Set<IVariableBinding> fieldHiders;
private final String suffix;
@ -307,8 +307,7 @@ public class CppImplementationGenerator extends ObjectiveCSourceFileGenerator {
@SuppressWarnings("unchecked")
List<Expression> args = constant.arguments(); // safe by definition
String name = NameTable.getName(constant.getName());
String constantTypeName =
NameTable.getFullName(Types.getMethodBinding(constant).getDeclaringClass());
String constantTypeName = NameTable.getFullName(Types.getMethodBinding(constant).getDeclaringClass());
printf(" %s_%s = [[%s alloc] init", typeName, name, constantTypeName);
boolean isSimpleEnum = constantTypeName.equals(typeName);
@ -316,7 +315,7 @@ public class CppImplementationGenerator extends ObjectiveCSourceFileGenerator {
if (args.isEmpty() && isSimpleEnum) {
printf("WithNSString:@\"%s_%s\" withInt:%d];\n", typeName.replace("Enum", ""), name, i);
} else {
String argString = StatementGenerator.generateArguments(Types.getMethodBinding(constant),
String argString = CppStatementGenerator.generateArguments(Types.getMethodBinding(constant),
args, fieldHiders, getBuilder().getCurrentLine());
print(argString);
if (args.isEmpty()) {
@ -337,7 +336,7 @@ public class CppImplementationGenerator extends ObjectiveCSourceFileGenerator {
@SuppressWarnings("unchecked")
List<Statement> stmts = initializeMethod.getBody().statements(); // safe by definition
for (Statement s : stmts) {
printf(" %s", StatementGenerator.generate(s, fieldHiders, false,
printf(" %s", CppStatementGenerator.generate(s, fieldHiders, false,
getBuilder().getCurrentLine()));
}
}
@ -499,17 +498,17 @@ public class CppImplementationGenerator extends ObjectiveCSourceFileGenerator {
}
private String generateStatement(Statement stmt, boolean asFunction, boolean inConstructor) {
return StatementGenerator.generate(stmt, fieldHiders, asFunction,
return CppStatementGenerator.generate(stmt, fieldHiders, asFunction,
getBuilder().getCurrentLine());
}
private String generateStatement(Statement stmt, boolean asFunction) {
return StatementGenerator.generate(stmt, fieldHiders, asFunction,
return CppStatementGenerator.generate(stmt, fieldHiders, asFunction,
getBuilder().getCurrentLine());
}
private String generateExpression(Expression expr) {
return StatementGenerator.generate(expr, fieldHiders, false, getBuilder().getCurrentLine());
return CppStatementGenerator.generate(expr, fieldHiders, false, getBuilder().getCurrentLine());
}
private void printMainMethod(MethodDeclaration m, String typeName,
@ -591,7 +590,7 @@ public class CppImplementationGenerator extends ObjectiveCSourceFileGenerator {
if (!imports.isEmpty()) {
Set<String> importStmts = Sets.newTreeSet();
for (ImportCollector.Import imp : imports) {
importStmts.add(String.format("#include \"%s.h\"", imp.getImportFileName()));
importStmts.add(String.format("#include <%s.h>", imp.getImportFileName()));
}
for (String stmt : importStmts) {
println(stmt);

View file

@ -124,7 +124,7 @@ public abstract class CppSourceFileGenerator extends SourceFileGenerator {
baseDeclaration = "- (NSUInteger)hash";
} else {
baseDeclaration = String.format("%s (%s)%s", isStatic ? "static" : "",
NameTableCpp.javaRefToObjC(method.getReturnType2()), mappedMethod.getName());
NameTableCpp.javaRefToCpp(method.getReturnType2()), mappedMethod.getName());
}
sb.append(baseDeclaration);
@ -163,7 +163,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 " : "", NameTableCpp.javaRefToObjC(m.getReturnType2()), methodName);
String baseDeclaration = String.format(" %s %s %s", isStatic ? "static " : "", NameTableCpp.basicTypeToCpp(NameTableCpp.javaRefToCpp(m.getReturnType2())), methodName);
sb.append(baseDeclaration);
@SuppressWarnings("unchecked")
List<SingleVariableDeclaration> params = m.parameters(); // safe by definition
@ -205,7 +205,7 @@ public abstract class CppSourceFileGenerator extends SourceFileGenerator {
String type = isTypeVariable ?
NameTable.getParameterTypeName(NameTable.ID_TYPE, typeBinding) :
NameTable.getParameterTypeName(NameTable.javaTypeToObjC(param.getType(), true), typeBinding);
sb.append(" ").append(type).append(" ").append(fieldName);
sb.append(" ").append(NameTableCpp.basicTypeToCpp(type)).append(typeBinding.isPrimitive() ? "" : "*").append(" ").append(fieldName);
if (i<nParams-1) {
sb.append(",");
} else {

View file

@ -38,12 +38,12 @@ public class NameTableCpp {
/**
* Return the Objective-C equivalent name for a Java primitive type.
*/
public static String primitiveTypeToObjC(PrimitiveType type) {
public static String primitiveTypeToCpp(PrimitiveType type) {
PrimitiveType.Code code = type.getPrimitiveTypeCode();
return primitiveTypeToObjC(code.toString());
return primitiveTypeToCpp(code.toString());
}
private static String primitiveTypeToObjC(String javaName) {
private static String primitiveTypeToCpp(String javaName) {
if (javaName.equals("boolean")) {
return "bool"; // defined in NSObject.h
}
@ -70,12 +70,32 @@ public class NameTableCpp {
return javaName;
}
public static String basicTypeToCpp(String javaName) {
// TODO populate
if (javaName.equals("String")) {
return "string";
}
if (javaName.equals("NSString *")) {
return "string";
}
if (javaName.equals("JavaUtilList")) {
return "vector";
}
if (javaName.equals("LongInt")) {
return "long long";
}
if (javaName.equals("IOSCharArray *")) {
return "wchar_t []";
}
return javaName;
}
/**
* Convert a Java type into an equivalent Objective-C type.
* Convert a Java type into an equivalent C++ type.
*/
public static String javaTypeToObjC(Type type, boolean includeInterfaces) {
public static String javaTypeToCpp(Type type, boolean includeInterfaces) {
if (type instanceof PrimitiveType) {
return primitiveTypeToObjC((PrimitiveType) type);
return primitiveTypeToCpp((PrimitiveType) type);
}
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getType(); // erase parameterized type
@ -88,10 +108,10 @@ public class NameTableCpp {
}
}
ITypeBinding binding = Types.getTypeBinding(type);
return javaTypeToObjC(binding, includeInterfaces);
return javaTypeToCpp(binding, includeInterfaces);
}
public static String javaTypeToObjC(ITypeBinding binding, boolean includeInterfaces) {
public static String javaTypeToCpp(ITypeBinding binding, boolean includeInterfaces) {
if (binding.isInterface() && !includeInterfaces || binding == Types.resolveIOSType("id") ||
binding == Types.resolveIOSType("NSObject")) {
return NameTable.ID_TYPE;
@ -106,15 +126,15 @@ public class NameTableCpp {
return NameTable.getFullName(binding);
}
public static String javaRefToObjC(Type type) {
return javaRefToObjC(Types.getTypeBinding(type));
public static String javaRefToCpp(Type type) {
return javaRefToCpp(Types.getTypeBinding(type));
}
public static String javaRefToObjC(ITypeBinding type) {
public static String javaRefToCpp(ITypeBinding type) {
if (type.isPrimitive()) {
return primitiveTypeToObjC(type.getName());
return primitiveTypeToCpp(type.getName());
}
String typeName = javaTypeToObjC(type, false);
String typeName = javaTypeToCpp(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));
@ -136,7 +156,7 @@ public class NameTableCpp {
public static String getFullName(ITypeBinding binding) {
if (binding.isPrimitive()) {
return primitiveTypeToObjC(binding.getName());
return primitiveTypeToCpp(binding.getName());
}
binding = Types.mapType(binding.getErasure()); // Make sure type variables aren't included.
String suffix = binding.isEnum() ? "Enum" : "";