forked from redmouth/java2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTreeParser.java
More file actions
411 lines (353 loc) · 17.4 KB
/
Copy pathSymbolTreeParser.java
File metadata and controls
411 lines (353 loc) · 17.4 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package co.deepblue.java2cpp.symbol;
import co.deepblue.java2cpp.processor.AstNodeHelper;
import co.deepblue.java2cpp.statement.StatementTypes;
import co.deepblue.java2cpp.util.StringUtils;
import co.deepblue.java2cpp.type.TypeConverter;
import co.deepblue.java2cpp.processor.ModifierProcessor;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.stmt.*;
import com.github.javaparser.ast.type.ArrayType;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;
import com.github.javaparser.ast.type.VoidType;
import java.util.*;
import static co.deepblue.java2cpp.symbol.EnumSymbolType.*;
/**
* Created by levin on 17-5-10.
*/
public class SymbolTreeParser {
static SymbolTreeParser instance_;
SymbolNode projRoot;
HashMap<String, List<SymbolNode>> global_symbol_map;
HashMap<String, CompilationUnit> cuMap;
ArrayList<CompilationUnit> cuList;
HashMap<String, Set<String>> includesMap;
HashMap<String, Set<String>> namespaceMap;
HashMap<String, String> predefinedCppInclude = new HashMap<>();
MethodDeclaration mainMethod;
CompilationUnit mainCU;
public static SymbolTreeParser getInstance() {
return instance_;
}
public static void createInstance(String projectName, HashMap<String, CompilationUnit> cuMap, ArrayList<CompilationUnit> cuList) {
if (instance_ == null) {
instance_ = new SymbolTreeParser(projectName, cuMap, cuList);
}
}
private SymbolTreeParser(String projectName, HashMap<String, CompilationUnit> cuMap, ArrayList<CompilationUnit> cuList) {
this.cuMap = cuMap;
this.cuList = cuList;
global_symbol_map = new HashMap<>();
includesMap = new HashMap<>();
namespaceMap = new HashMap<>();
projRoot = new SymbolNode(null,null, projectName, SYMBOL_PROJECT, null, null, null, true);
}
public SymbolNode addNodeToTree(SymbolNode node, SymbolNode parent) {
parent.addChild(node);
return node;
}
public void scanSymbols() {
for (CompilationUnit cu : cuList) {
if (cu.getPackageDeclaration().isPresent()) {
String packageName = cu.getPackageDeclaration().get().getName().asString();
NodeList<TypeDeclaration<?>> types = cu.getTypes();
if (types.size() > 0) {
TypeDeclaration typeDeclaration = types.get(0);
String classOrEnumName = typeDeclaration.getNameAsString();
SymbolNode node = new SymbolNode(projRoot, packageName, classOrEnumName, SYMBOL_TYPE_NONE, typeDeclaration, typeDeclaration, cu, true);
add_to_global_map(node.name, node);
if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
node.type = SYMBOL_TYPE_CLASSORINTERFACE;
buildClassSymbol((ClassOrInterfaceDeclaration)typeDeclaration, node);
} else if (typeDeclaration instanceof EnumDeclaration) {
node.type = SYMBOL_TYPE_ENUM;
EnumDeclaration ed = (EnumDeclaration) typeDeclaration;
buildEnumSymbol(ed, node);
if (ed.getMembers().size() <= 0) {
TypeConverter.getInstance().addSimpleEnumType(ed.getNameAsString(), packageName);
}
}
}
}
}
}
public SymbolNode addNode(TypeDeclaration declaration, EnumSet<Modifier> modifiers, EnumSymbolType type, TypeDeclaration parentDelcaration, SymbolNode parent) {
String name = declaration.getNameAsString();
SymbolNode node = new SymbolNode(parent, parent.packageName, name, type, declaration, parentDelcaration, null, false);
ModifierProcessor modifier = new ModifierProcessor();
modifier.processModifiers(modifiers);
node.isStatic = modifier.isStatic;
add_to_global_map(name, node);
return node;
}
public SymbolNode addNode(String name, BodyDeclaration declaration, EnumSet<Modifier> modifiers, EnumSymbolType type, TypeDeclaration parentDelcaration, SymbolNode parent) {
SymbolNode node = new SymbolNode(parent, parent.packageName, name, type, declaration, parentDelcaration, null, false);
ModifierProcessor modifier = new ModifierProcessor();
modifier.processModifiers(modifiers);
node.isStatic = modifier.isStatic;
add_to_global_map(name, node);
return node;
}
public SymbolNode addNode(String name, Node declaration, boolean isStatic, EnumSymbolType type, Node parentDelcaration, SymbolNode parent) {
SymbolNode node = new SymbolNode(parent, parent.packageName, name, type, declaration, parentDelcaration, null, false);
node.isStatic = isStatic;
add_to_global_map(name, node);
return node;
}
public void buildClassSymbol(ClassOrInterfaceDeclaration parentDelcaration, SymbolNode parent) {
/*
NodeList<TypeParameter> typeParameters = classOrInterfaceDeclaration.getTypeParameters(); //Template Parameters
if (typeParameters.size() > 0) {
for (TypeParameter parameter : typeParameters) {
classSymbol.template_parameters.add(parameter.getNameAsString());
}
}
*/
NodeList<BodyDeclaration<?>> members = parentDelcaration.getMembers();
for (BodyDeclaration<?> member : members) {
if (member instanceof MethodDeclaration) {
processMethod(parentDelcaration, (MethodDeclaration) member, parent);
} else if (member instanceof FieldDeclaration) {
processField(parentDelcaration, (FieldDeclaration)member, parent);
} else if (member instanceof ConstructorDeclaration) {
processConstructor(parentDelcaration, (ConstructorDeclaration)member, parent);
} else if (member instanceof ClassOrInterfaceDeclaration) {
SymbolNode node = addNode((TypeDeclaration) member, ((ClassOrInterfaceDeclaration) member).getModifiers(),
SYMBOL_TYPE_INNER_CLASS, parentDelcaration, parent);
buildClassSymbol((ClassOrInterfaceDeclaration) member, node);
} else if (member instanceof EnumDeclaration) {
SymbolNode node = addNode((TypeDeclaration) member, ((EnumDeclaration) member).getModifiers(), SYMBOL_TYPE_INNER_ENUM,
parentDelcaration, parent);
buildEnumSymbol((EnumDeclaration)member, node);
}
}
}
public void buildEnumSymbol(EnumDeclaration parentDelcaration, SymbolNode parent) {
NodeList<BodyDeclaration<?>> members = parentDelcaration.getMembers();
for (BodyDeclaration<?> member : members) {
if (member instanceof MethodDeclaration) {
processMethod(parentDelcaration, (MethodDeclaration) member, parent);
} else if (member instanceof FieldDeclaration) {
processField(parentDelcaration, (FieldDeclaration)member, parent);
} else if (member instanceof ConstructorDeclaration) {
processConstructor(parentDelcaration, (ConstructorDeclaration)member, parent);
} else if (member instanceof ClassOrInterfaceDeclaration) {
SymbolNode node = addNode((TypeDeclaration) member, ((ClassOrInterfaceDeclaration) member).getModifiers(),
SYMBOL_TYPE_INNER_CLASS, parentDelcaration, parent);
buildClassSymbol((ClassOrInterfaceDeclaration) member, node);
} else if (member instanceof EnumDeclaration) {
SymbolNode node = addNode((TypeDeclaration) member, ((EnumDeclaration) member).getModifiers(), SYMBOL_TYPE_INNER_ENUM,
parentDelcaration, parent);
buildEnumSymbol((EnumDeclaration)member, node);
}
}
if (parentDelcaration.getMembers().size() <= 0) {
TypeConverter.getInstance().addSimpleEnumType(parentDelcaration.getNameAsString(), parent.getPackageName());
}
}
public void processConstructor(TypeDeclaration parentDeclaration, ConstructorDeclaration constructor, SymbolNode parent) {
String name = constructor.getNameAsString();
SymbolNode node = addNode(name, constructor, constructor.getModifiers(), SYMBOL_TYPE_CLASS_CONSTRUCTOR, parentDeclaration, parent);
BlockStmt blockStmt = constructor.getBody();
NodeList<Statement> statements = blockStmt.getStatements();
for (Statement statement : statements) {
if (statement instanceof ExpressionStmt) {
Expression expression = ((ExpressionStmt) statement).getExpression();
if (expression instanceof VariableDeclarationExpr) {
processVariableDeclaration(constructor, (VariableDeclarationExpr)expression, node);
}
} else if (statement instanceof LocalClassDeclarationStmt) {
LocalClassDeclarationStmt lcd = (LocalClassDeclarationStmt) statement;
}
}
}
public void processMethod(TypeDeclaration parentDeclaration, MethodDeclaration method, SymbolNode parent) {
String name = method.getNameAsString();
ModifierProcessor modifier = new ModifierProcessor();
modifier.processModifiers(method.getModifiers());
boolean isPublicStaticVoidMain = false;
if (name.equals("main") && modifier.isStatic && modifier.isPublic) {
Type type = method.getType();
if (type instanceof VoidType) {
isPublicStaticVoidMain = true;
}
}
SymbolNode methodNode = addNode(name, method, method.getModifiers(), SYMBOL_TYPE_CLASS_METHOD, parentDeclaration, parent);
NodeList<Parameter> parameters = method.getParameters();
for (Parameter parameter : parameters) {
addNode(parameter.getNameAsString(), parameter, false, SYMBOL_TYPE_METHOD_OR_CONSTRUCTOR_PARAMETER, method, methodNode);
}
if (isPublicStaticVoidMain && parameters.size() == 1 && (parameters.get(0).getType() instanceof ArrayType)) {
ArrayType arrayType = (ArrayType) parameters.get(0).getType();
Type componentType = arrayType.getComponentType();
if ((componentType instanceof ClassOrInterfaceType) && ((ClassOrInterfaceType) componentType).getNameAsString().equals("String")) {
setMainMethod(method);
}
}
Optional<BlockStmt> blockStmt = method.getBody();
if (blockStmt.isPresent()) {
BlockStmt stmt = blockStmt.get();
NodeList<Statement> statements = stmt.getStatements();
for (Statement statement : statements) {
StatementTypes.registerStatementTypes(statement);
if (statement instanceof ExpressionStmt) {
Expression expression = ((ExpressionStmt) statement).getExpression();
if (expression instanceof VariableDeclarationExpr) {
processVariableDeclaration(method, (VariableDeclarationExpr)expression, methodNode);
}
} else if (statement instanceof LocalClassDeclarationStmt) {
LocalClassDeclarationStmt lcd = (LocalClassDeclarationStmt) statement;
} else if (statement instanceof ReturnStmt) {
}
}
}
}
public void processField(TypeDeclaration parentDeclaration, FieldDeclaration fieldDeclaration, SymbolNode parent) {
ModifierProcessor modifier = new ModifierProcessor();
modifier.processModifiers(fieldDeclaration.getModifiers());
for (VariableDeclarator vd : fieldDeclaration.getVariables()) {
String name = vd.getNameAsString();
addNode(name, vd, modifier.isStatic, SYMBOL_TYPE_CLASS_FIELD, parentDeclaration, parent);
}
}
public void processVariableDeclaration(BodyDeclaration parentDeclaration, VariableDeclarationExpr vExpr, SymbolNode parent) {
ModifierProcessor modifier = new ModifierProcessor();
modifier.processModifiers(vExpr.getModifiers());
NodeList<VariableDeclarator> variables = vExpr.getVariables();
for (VariableDeclarator variable : variables) {
String name = variable.getNameAsString();
addNode(name, variable, modifier.isStatic, SYMBOL_TYPE_METHOD_LOCAL_VARIABLE, parentDeclaration, parent);
}
}
private void add_to_global_map(String name, SymbolNode symbol) {
if (global_symbol_map.containsKey(name)) {
List<SymbolNode> symbolList = global_symbol_map.get(name);
symbolList.add(symbol);
} else {
List<SymbolNode> symbolList = new ArrayList<>();
symbolList.add(symbol);
global_symbol_map.put(name, symbolList);
}
}
public String getTypeScope(String name, CompilationUnit unit) {
List<SymbolNode> nodeList = global_symbol_map.get(name);
if (nodeList != null) {
for (SymbolNode node : nodeList) {
if (node.isInnerClassOrEnum()) {
String scope = SymbolNodeHelper.queryFileName(node, unit);
if (scope != null)
return scope;
}
}
}
return "";
}
private String findSymbol(String typeName) {
List<SymbolNode> nodeList = global_symbol_map.get(typeName);
if (nodeList != null) {
for (SymbolNode node : nodeList) {
if (node.isClassOrEnum()) {
String fullName = SymbolNodeHelper.queryFileName(node);
if (fullName != null) {
return fullName;
}
}
}
}
return null;
}
public void addIncludeForType(CompilationUnit unit, String typeName) {
String className = unit.getData(AstNodeHelper.FilenameDatakey);
Set<String> includes = includesMap.get(className);
if (includes != null) {
for (String include : includes) {
if (include.endsWith("." + typeName)) {
return;
}
}
}
String includeName = findSymbol(typeName);
addInclude(className, includeName);
}
public void addInclude(String className, String include) {
if (!StringUtils.isEmpty(include)) {
if (includesMap.containsKey(className)) {
Set<String> includes = includesMap.get(className);
includes.add(include);
} else {
Set<String> includes = new HashSet<>();
includes.add(include);
includesMap.put(className, includes);
}
}
}
public void addIncludeForUnit(CompilationUnit cu, String include) {
if (!StringUtils.isEmpty(include)) {
String className = cu.getData(AstNodeHelper.FilenameDatakey);
if (includesMap.containsKey(className)) {
Set<String> includes = includesMap.get(className);
includes.add(include);
} else {
Set<String> includes = new HashSet<>();
includes.add(include);
includesMap.put(className, includes);
}
}
}
public Set<String> getInclude(String fileName) {
if (includesMap.containsKey(fileName))
return includesMap.get(fileName);
return null;
}
public void addNamespace(String className, String namespace) {
if (namespaceMap.containsKey(className)) {
Set<String> namespaces = namespaceMap.get(className);
namespaces.add(namespace);
} else {
Set<String> namespaces = new HashSet<>();
namespaces.add(namespace);
namespaceMap.put(className, namespaces);
}
}
public Set<String> getNamespace(String fileName) {
if (namespaceMap.containsKey(fileName))
return namespaceMap.get(fileName);
return null;
}
public MethodDeclaration getMainMethod() {
return mainMethod;
}
public CompilationUnit getMainCU() {
return mainCU;
}
public void setMainMethod(MethodDeclaration method) {
this.mainMethod = method;
mainCU = AstNodeHelper.getCompilationUnit(method);
}
public void registerPredefinedInclude(String typeName, String include) {
predefinedCppInclude.put(typeName, include);
}
public void addPredefinedIncludeToUnit(CompilationUnit cu, String typeName) {
if (predefinedCppInclude.containsKey(typeName)) {
String include = predefinedCppInclude.get(typeName);
addIncludeForUnit(cu, include);
}
}
int totalNumberOfSymbols = 0;
public int getTotalNumberOfSymbols() {
if (totalNumberOfSymbols == 0) {
int total = 0;
for (Map.Entry<String, List<SymbolNode>> entry : global_symbol_map.entrySet()) {
total += entry.getValue().size();
}
totalNumberOfSymbols = total;
}
return totalNumberOfSymbols;
}
}