提交 167230d2 authored 作者: Thomas Mueller's avatar Thomas Mueller

Java to C++ converter

上级 142c2e4b
...@@ -267,9 +267,14 @@ class FieldObj { ...@@ -267,9 +267,14 @@ class FieldObj {
Type type; Type type;
/** /**
* Whether this is a local field. * Whether this is a variable or parameter.
*/ */
boolean isLocal; boolean isVariable;
/**
* Whether this is a local field (not separately garbage collected).
*/
boolean isLocalField;
/** /**
* The field name. * The field name.
......
...@@ -23,21 +23,19 @@ public interface Expr { ...@@ -23,21 +23,19 @@ public interface Expr {
*/ */
class CallExpr implements Expr { class CallExpr implements Expr {
final JavaParser context;
final Expr expr;
final ArrayList<Expr> args = new ArrayList<Expr>(); final ArrayList<Expr> args = new ArrayList<Expr>();
final boolean isStatic; private final JavaParser context;
final String className; private final String className;
final String name; private final String name;
private Expr expr;
private ClassObj classObj; private ClassObj classObj;
private MethodObj method; private MethodObj method;
CallExpr(JavaParser context, Expr expr, String className, String name, boolean isStatic) { CallExpr(JavaParser context, Expr expr, String className, String name) {
this.context = context; this.context = context;
this.expr = expr; this.expr = expr;
this.className = className; this.className = className;
this.name = name; this.name = name;
this.isStatic = isStatic;
} }
private void initMethod() { private void initMethod() {
...@@ -50,6 +48,9 @@ class CallExpr implements Expr { ...@@ -50,6 +48,9 @@ class CallExpr implements Expr {
classObj = expr.getType().classObj; classObj = expr.getType().classObj;
} }
method = classObj.getMethod(name, args); method = classObj.getMethod(name, args);
if (method.isStatic) {
expr = null;
}
} }
public String toString() { public String toString() {
...@@ -165,7 +166,7 @@ class LiteralExpr implements Expr { ...@@ -165,7 +166,7 @@ class LiteralExpr implements Expr {
public String toString() { public String toString() {
if ("null".equals(literal)) { if ("null".equals(literal)) {
return JavaParser.toCType(type) + "()"; return JavaParser.toCType(type, true) + "()";
} }
return literal; return literal;
} }
...@@ -301,7 +302,7 @@ class NewExpr implements Expr { ...@@ -301,7 +302,7 @@ class NewExpr implements Expr {
} }
buff.append("))"); buff.append("))");
} else { } else {
buff.append("new " + JavaParser.toC(classObj.toString())); buff.append("ptr<" + JavaParser.toC(classObj.toString()) + ">(new " + JavaParser.toC(classObj.toString()));
buff.append("("); buff.append("(");
int i = 0; int i = 0;
for (Expr a : args) { for (Expr a : args) {
...@@ -310,7 +311,7 @@ class NewExpr implements Expr { ...@@ -310,7 +311,7 @@ class NewExpr implements Expr {
} }
buff.append(a); buff.append(a);
} }
buff.append(")"); buff.append("))");
} }
return buff.toString(); return buff.toString();
} }
...@@ -458,9 +459,6 @@ class VariableExpr implements Expr { ...@@ -458,9 +459,6 @@ class VariableExpr implements Expr {
private void init() { private void init() {
if (field == null) { if (field == null) {
if (base == null) {
System.out.println("??");
}
Type t = base.getType(); Type t = base.getType();
if (t.arrayLevel > 0) { if (t.arrayLevel > 0) {
if ("length".equals(name)) { if ("length".equals(name)) {
......
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.java;
/**
* This annotation marks fields that are not shared and therefore don't need to
* be garbage collected separately.
*/
public @interface Local {
// empty
}
...@@ -44,7 +44,7 @@ class ReturnStatement extends StatementBase { ...@@ -44,7 +44,7 @@ class ReturnStatement extends StatementBase {
if (expr.getType().isSimplePrimitive()) { if (expr.getType().isSimplePrimitive()) {
return "return " + expr + ";"; return "return " + expr + ";";
} }
return "return " + JavaParser.toCType(expr.getType()) + "(" + expr + ");"; return "return " + JavaParser.toCType(expr.getType(), true) + "(" + expr + ");";
} }
} }
...@@ -253,7 +253,7 @@ class VarDecStatement extends StatementBase { ...@@ -253,7 +253,7 @@ class VarDecStatement extends StatementBase {
public String toString() { public String toString() {
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
buff.append(JavaParser.toCType(type)).append(' '); buff.append(JavaParser.toCType(type, true)).append(' ');
StringBuilder assign = new StringBuilder(); StringBuilder assign = new StringBuilder();
for (int i = 0; i < variables.size(); i++) { for (int i = 0; i < variables.size(); i++) {
if (i > 0) { if (i > 0) {
......
...@@ -30,13 +30,16 @@ public class Test extends TestBase { ...@@ -30,13 +30,16 @@ public class Test extends TestBase {
// chmod +x test // chmod +x test
// ./test // ./test
// (for "mixed declarations and code") // include files:
// /usr/include/c++/4.2.1/tr1/stdio.h
// /usr/include/stdio.h
// TODO initialize fields
// not supported yet: // not supported yet:
// exceptions // exceptions
// HexadecimalFloatingPointLiteral // HexadecimalFloatingPointLiteral
// int x()[] { return null; } // int x()[] { return null; }
// annotations
// import static // import static
// import * // import *
// initializer blocks // initializer blocks
...@@ -66,6 +69,7 @@ public class Test extends TestBase { ...@@ -66,6 +69,7 @@ public class Test extends TestBase {
parser.parse("src/tools/org/h2", "java.lang.String"); parser.parse("src/tools/org/h2", "java.lang.String");
parser.parse("src/tools/org/h2", "java.lang.Math"); parser.parse("src/tools/org/h2", "java.lang.Math");
parser.parse("src/tools/org/h2", "java.lang.Integer"); parser.parse("src/tools/org/h2", "java.lang.Integer");
parser.parse("src/tools/org/h2", "java.lang.Long");
parser.parse("src/tools/org/h2", "java.lang.StringBuilder"); parser.parse("src/tools/org/h2", "java.lang.StringBuilder");
parser.parse("src/tools/org/h2", "java.io.PrintStream"); parser.parse("src/tools/org/h2", "java.io.PrintStream");
parser.parse("src/tools/org/h2", "java.lang.System"); parser.parse("src/tools/org/h2", "java.lang.System");
......
/*
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.java.lang;
/**
* A java.lang.Long implementation.
*/
public class Long {
public static final long MIN_VALUE = 1L << 63;
public static final long MAX_VALUE = (1L << 63) - 1;
/**
* Convert a value to a String.
*
* @param x the value
* @return the String
*/
public static String toString(long x) {
// c: wchar_t ch[30];
// c: swprintf(ch, 30, L"%ld", x);
// c: return STRING(ch);
// c: return;
if (x == MIN_VALUE) {
return String.wrap("-9223372036854775808");
}
char[] ch = new char[30];
int i = 30 - 1, count = 0;
boolean negative;
if (x < 0) {
negative = true;
x = -x;
} else {
negative = false;
}
for (; i >= 0; i--) {
ch[i] = (char) ('0' + (x % 10));
x /= 10;
count++;
if (x == 0) {
break;
}
}
if (negative) {
ch[--i] = '-';
count++;
}
return new String(ch, i, count);
}
}
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
package org.h2.java.lang; package org.h2.java.lang;
import org.h2.java.Ignore; import org.h2.java.Ignore;
import org.h2.java.Local;
/* c: /* c:
...@@ -37,7 +38,7 @@ protected: ...@@ -37,7 +38,7 @@ protected:
jint refCount; jint refCount;
public: public:
RefBase() { RefBase() {
refCount = 1; refCount = 0;
} }
void reference() { void reference() {
refCount++; refCount++;
...@@ -47,6 +48,8 @@ public: ...@@ -47,6 +48,8 @@ public:
delete this; delete this;
} }
} }
virtual ~RefBase() {
}
}; };
template <class T> class ptr { template <class T> class ptr {
T* pointer; T* pointer;
...@@ -137,6 +140,7 @@ public class String { ...@@ -137,6 +140,7 @@ public class String {
/** /**
* The character array. * The character array.
*/ */
@Local
char[] chars; char[] chars;
private int hash; private int hash;
...@@ -152,18 +156,17 @@ public class String { ...@@ -152,18 +156,17 @@ public class String {
} }
public int hashCode() { public int hashCode() {
if (hash == 0) { int h = hash;
if (chars.length == 0) { if (h == 0) {
return 0; int size = chars.length;
} if (size != 0) {
int h = 0; for (int i = 0; i < size; i++) {
for (char c : chars) { h = h * 31 + chars[i];
h = h * 31 + c;
} }
hash = h; hash = h;
return h;
} }
return hash; }
return h;
} }
/** /**
......
...@@ -56,4 +56,21 @@ public class System { ...@@ -56,4 +56,21 @@ public class System {
java.lang.System.arraycopy(src, srcPos, dest, destPos, length); java.lang.System.arraycopy(src, srcPos, dest, destPos, length);
} }
/**
* Get the current time in milliseconds since 1970-01-01.
*
* @return the milliseconds
*/
public static long nanoTime() {
/* c:
#if CLOCKS_PER_SEC == 1000000
return (jlong) clock() * 1000;
#else
return (jlong) clock() * 1000000 / CLOCKS_PER_SEC;
#endif
*/
// c: return;
return java.lang.System.nanoTime();
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论