forked from redmouth/java2cpp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTypeInferencer.java
More file actions
73 lines (62 loc) · 2.78 KB
/
Copy pathTypeInferencer.java
File metadata and controls
73 lines (62 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package co.deepblue.java2cpp.type;
import co.deepblue.java2cpp.processor.AstNodeHelper;
import co.deepblue.java2cpp.processor.TypeArguments;
import co.deepblue.java2cpp.symbol.SymbolTreeParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.type.ArrayType;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
/**
* Created by levin on 17-5-11.
*/
public class TypeInferencer {
public static String inferenceTypeForMethodExpression(Type type) {
if (type instanceof ClassOrInterfaceType) {
ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) type;
String scopeStr = "";
if (classOrInterfaceType.getScope().isPresent()) {
scopeStr = inferenceTypeForMethodExpression(classOrInterfaceType.getScope().get()) + "::";
}
String argumentStr = "";
if (classOrInterfaceType.getTypeArguments().isPresent())
argumentStr = assembleTypeArguments(classOrInterfaceType.getTypeArguments().get());
return scopeStr + classOrInterfaceType.getName() + argumentStr + "*";
} else if (type instanceof ArrayType) {
CompilationUnit cu = AstNodeHelper.getCompilationUnit(type);
SymbolTreeParser.getInstance().addPredefinedIncludeToUnit(cu, "Array");
String componentName = TypeArguments.parseTypeArgument(((ArrayType) type).getComponentType());
return "Array<" + componentName + ">";
//return inferenceTypeForMethodExpression(((ArrayType) type).getComponentType()) + "*";
}
return TypeConverter.getInstance().mapJava2CppType(type.toString());
}
public static String assembleTypeArguments(NodeList<Type> typeArguments) {
if (typeArguments.size() > 0) {
StringBuilder builder = new StringBuilder();
builder.append("<");
builder.append(inferenceTypeForMethodExpression(typeArguments.get(0)));
for (int i=1; i<typeArguments.size(); i++) {
builder.append(", ");
builder.append(inferenceTypeForMethodExpression(typeArguments.get(i)));
}
builder.append(">");
return builder.toString();
}
return "";
}
public static String inferExpressionType(Expression expression) {
//todo
/*
1 Binary expression: 4 + 1, -2 + 1, x + y, 1 + x, x + 4
2 Method call: Math.square(x), a.length, a.size,
int l = 4L; //wrong
int l = 4; //wrong
long l = 4L; //correct
long l = 4; //correct
*/
return expression.toString();
}
int jint = 0;
}