-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlockMangler.java
More file actions
659 lines (493 loc) · 24.4 KB
/
BlockMangler.java
File metadata and controls
659 lines (493 loc) · 24.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package qimpp;
import java.util.*;
import xtc.tree.GNode;
import xtc.tree.Node;
import xtc.tree.Visitor;
import xtc.tree.Printer;
import xtc.util.Tool;
/**
* Mangles Block Nodes into better Block nodes for ImplementationPrinter
*
* @author QIMPP
*/
public class BlockMangler {
public GNode cppClass;
public InheritanceTreeManager inheritanceTree;
public MethodResolver methodResolver;
/** BlockMangler constructor. */
public BlockMangler(
GNode cppClass,
InheritanceTreeManager itm,
MethodResolver mr
) {
this.cppClass = cppClass;
this.methodResolver = mr;
this.inheritanceTree = itm;
}
// replaces nOld GNode with nNew GNode
public void copyloc(GNode nOld, GNode nNew) {
nNew.setLocation(nOld);
}
// takes java block
public GNode mangle(GNode java) {
if (java.getProperty("Mangled") != null){
(new Exception()).printStackTrace(System.err);
System.exit(1);
}
java.setProperty("Mangled", new Boolean(true));
// Vivek: it doesn't seem like this is being used.
GNode cpp = GNode.create("Block");
/**
* The visitor that analyzes and modifies the block
*/
new Visitor() {
/**
* Determine if this is a class, a stackvar, field or the start of a fully
* qualified class name and set the proper properties of the node
*/
public String visitPrimaryIdentifier(GNode n){
String identifier = n.getString(0);
if (identifier.equals("R1")){
resolveClassField(identifier);
}
if (selectionExpressionBuilder != null){
selectionExpressionBuilder.insert(0, identifier);
}
GNode classDeclaration =
inheritanceTree.getClassDeclarationNode(identifier);
GNode stackVar = resolveScopes(n);
GNode classField = resolveClassField(identifier);
//
//
if (classDeclaration != null) {
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.QUALIFIED_CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, classDeclaration);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE,
GNode.create("Type",
Disambiguator.disambiguate(classDeclaration.getString(0)), null));
return Constants.CLASS_IDENTIFIER;
}
else if (stackVar != null){
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.STACKVAR_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, stackVar);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, stackVar.getGeneric(1));
return Constants.STACKVAR_IDENTIFIER;
}
else if (classField != null){
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.FIELD_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, classField);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, classField.getGeneric(1));
n.setProperty("class", Type.getClassTypeName(cppClass.getString(0)));
// Set the value of the reference to the value of the field declaration
n.set(0, classField.getString(0));
return Constants.FIELD_IDENTIFIER;
}
// It must be a fully qualified class
else {
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.QUALIFIED_CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, null);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, null);
return Constants.QUALIFIED_CLASS_IDENTIFIER;
}
}
public String visitBooleanLiteral(GNode n) {
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.PRIMITIVE_TYPE_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type", GNode.create("PrimitiveType", "boolean"), null));
return Constants.PRIMITIVE_TYPE_IDENTIFIER;
}
/**
* Set the appropriate properties for an IntegerLiteral
*/
public String visitIntegerLiteral(GNode n){
n.setProperty(Constants.IDENTIFIER_TYPE,
Constants.PRIMITIVE_TYPE_IDENTIFIER);
//TODO: Handle longs
if (n.getString(0).charAt(n.getString(0).length()-1) == 'l' ||
(n.getString(0).charAt(n.getString(0).length()-1) == 'L')) {
n.setProperty(Constants.IDENTIFIER_TYPE_NODE,
GNode.create("Type", GNode.create("PrimitiveType", "long"), null));
} else {
n.setProperty(Constants.IDENTIFIER_TYPE_NODE,
GNode.create("Type", GNode.create("PrimitiveType", "int"), null));
}
return Constants.PRIMITIVE_TYPE_IDENTIFIER;
}
/**
* Set the appropriate properties for a flp literal
*/
public String visitFloatingPointLiteral(GNode n){
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.PRIMITIVE_TYPE_IDENTIFIER);
//TODO: Handle float
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type", GNode.create("PrimitiveType", "double"), null));
return Constants.PRIMITIVE_TYPE_IDENTIFIER;
}
/**
* Set the appropriate properties for a char literal
*/
public String visitCharacterLiteral(GNode n){
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.PRIMITIVE_TYPE_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type", GNode.create("PrimitiveType", "char"), null));
return Constants.PRIMITIVE_TYPE_IDENTIFIER;
}
/**
* Set the appropriate properties for a string literal
*/
public String visitStringLiteral(GNode n){
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, inheritanceTree.getClassDeclarationNode("java.lang.String"));
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type", Disambiguator.disambiguate("java.lang.String"), null));
return Constants.CLASS_IDENTIFIER;
}
public String visitMultiplicativeExpression(GNode n){
// A multiplicative expression always returns a primitive type
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.PRIMITIVE_TYPE_IDENTIFIER);
dispatch(n.getGeneric(0));
dispatch(n.getGeneric(2));
String leftType = ((GNode)n.getGeneric(0).getProperty(Constants.IDENTIFIER_TYPE_NODE)).getGeneric(0).getString(0);
String rightType = ((GNode)n.getGeneric(2).getProperty(Constants.IDENTIFIER_TYPE_NODE)).getGeneric(0).getString(0);
String resultType = Type.compare(leftType, rightType);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE,
GNode.create("Type",
GNode.create("PrimitiveType", resultType), null));
return Constants.PRIMITIVE_TYPE_IDENTIFIER;
}
public String visitAdditiveExpression(GNode n){
if (n.get(0) instanceof String || n.get(2) instanceof String) {
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION,
inheritanceTree.getClassDeclarationNode("java.lang.String"));
n.setProperty(Constants.IDENTIFIER_TYPE_NODE,
GNode.create("Type",
Disambiguator.disambiguate("java.lang.String"), null));
return Constants.CLASS_IDENTIFIER;
}
dispatch(n.getGeneric(0));
dispatch(n.getGeneric(2));
GNode leftTypeNode =
(GNode) n.getGeneric(0).getProperty(Constants.IDENTIFIER_TYPE_NODE);
GNode rightTypeNode =
(GNode)n.getGeneric(2).getProperty(Constants.IDENTIFIER_TYPE_NODE);
if ((leftTypeNode != null &&
leftTypeNode.getGeneric(0).getName().equals("QualifiedIdentifier"))
|| (rightTypeNode != null &&
rightTypeNode.getGeneric(0).getName().equals("QualifiedIdentifier")))
//if (n.getGeneric(0).getName().equals("QualifiedIdentifier") ||
// n.getGeneric(2).getName().equals("QualifiedIdentifier"))
{
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION,
inheritanceTree.getClassDeclarationNode("java.lang.String"));
n.setProperty(Constants.IDENTIFIER_TYPE_NODE,
GNode.create("Type",
Disambiguator.disambiguate("java.lang.String"), null));
return Constants.CLASS_IDENTIFIER;
}
n.setProperty(Constants.IDENTIFIER_TYPE,
Constants.PRIMITIVE_TYPE_IDENTIFIER);
if (null != leftTypeNode && null != rightTypeNode) {
String leftType = leftTypeNode.getGeneric(0).getString(0);
String rightType = rightTypeNode.getGeneric(0).getString(0);
String resultType = Type.compare(leftType, rightType);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type",
GNode.create("PrimitiveType", resultType), null));
}
return Constants.PRIMITIVE_TYPE_IDENTIFIER;
}
public String visitThisExpression(GNode n){
// Just to be consistent for ThisExpressions
if (selectionExpressionBuilder != null){
selectionExpressionBuilder.insert(0, "__this");
}
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, cppClass);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type", Disambiguator.disambiguate(cppClass.getString(0)), null));
return Constants.CLASS_IDENTIFIER;
}
private int selectionExpressionDepth = 0;
private StringBuilder selectionExpressionBuilder;
/**
* Have the SelectionExpressions carry the innermost PrimaryIdentifier's type, except for the outermost one
* for a qualified identifier
*/
public String visitSelectionExpression(GNode n){
if (selectionExpressionDepth == 0)
selectionExpressionBuilder = new StringBuilder();
selectionExpressionDepth++;
n.setProperty(Constants.IDENTIFIER_TYPE, dispatch(n.getGeneric(0)));
selectionExpressionBuilder.append("." + n.getString(1));
//TODO: Debug code
if (n.getStringProperty(Constants.IDENTIFIER_TYPE) == null){
throw new NullPointerException();
}
// End debug code
n.setProperty(Constants.IDENTIFIER_DECLARATION, n.getGeneric(0).getProperty(Constants.IDENTIFIER_DECLARATION));
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, n.getGeneric(0).getProperty(Constants.IDENTIFIER_TYPE_NODE));
selectionExpressionDepth--;
String expression = selectionExpressionBuilder.toString();
// Bug out if it's System.out
if (expression.equals("System.out")) {
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.PRINT_IDENTIFIER);
//TODO:Hack
n.setProperty(Constants.IDENTIFIER_DECLARATION, new Object());
return Constants.PRINT_IDENTIFIER;
}
// Test if we're getting a field of ARRAY
if (!(n.getStringProperty(Constants.IDENTIFIER_TYPE).equals(Constants.QUALIFIED_CLASS_IDENTIFIER) && n.getProperty(Constants.IDENTIFIER_DECLARATION) == null)){
if (((GNode)n.getProperty(Constants.IDENTIFIER_TYPE_NODE)).getGeneric(1) != null){
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type", GNode.create("QualifiedIdentifier",
"__rt", "Array"), null));
}
}
// Part of the way in, we may find that we have a fully qualified type. In that case set the class declaration
if (n.getStringProperty(Constants.IDENTIFIER_TYPE).equals(Constants.QUALIFIED_CLASS_IDENTIFIER) && n.getProperty(Constants.IDENTIFIER_DECLARATION) == null){
GNode classDeclaration = inheritanceTree.getClassDeclarationNode(selectionExpressionBuilder.toString());
if (classDeclaration != null){
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.QUALIFIED_CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, classDeclaration);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, GNode.create("Type", Disambiguator.disambiguate(classDeclaration.getString(0)), null));
}
}
else if (n.getStringProperty(Constants.IDENTIFIER_TYPE) == Constants.STACKVAR_IDENTIFIER){
GNode foreignClass = inheritanceTree.getClassDeclarationNode(Disambiguator.getDotDelimitedName(
((GNode)n.getProperty(Constants.IDENTIFIER_TYPE_NODE)).getGeneric(0)));
GNode foreignFieldDeclaration = resolveClassField(n.getString(1), foreignClass);
String underscores = foreignClass.getString(0);
underscores = underscores.replace('.', '_');
n.set(1, underscores+"_"+n.getString(1));
n.set(1, foreignFieldDeclaration.getString(0));
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.FOREIGN_CLASS_FIELD_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION, foreignFieldDeclaration);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, foreignFieldDeclaration.getGeneric(1));
}
// If our child is a CLASS_IDENTIFIER, and we're still in a SelectionExpression, we must be referring to some accessible field
else if ((n.getStringProperty(Constants.IDENTIFIER_TYPE) == Constants.QUALIFIED_CLASS_IDENTIFIER
&& n.getProperty(Constants.IDENTIFIER_DECLARATION) != null)
|| n.getStringProperty(Constants.IDENTIFIER_TYPE) == Constants.CLASS_IDENTIFIER) {
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.FOREIGN_CLASS_FIELD_IDENTIFIER);
GNode foreignFieldDeclaration = resolveClassField(n.getString(1), (GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION));
// Debug
if (foreignFieldDeclaration == null) {
throw new RuntimeException("Failed to identify field " + n.getString(1));
}
// Reset the field to its proper name
String underscores = ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION)).getString(0);
underscores = underscores.replace('.', '_');
n.set(1, underscores+"_"+n.getString(1));
n.set(1, foreignFieldDeclaration.getString(0));
n.setProperty(Constants.IDENTIFIER_DECLARATION, foreignFieldDeclaration);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, foreignFieldDeclaration.getGeneric(1));
}
// If we're referring to some foreign class, we want to search it for this field's declaration
else if (n.getStringProperty(Constants.IDENTIFIER_TYPE).equals(Constants.FOREIGN_CLASS_FIELD_IDENTIFIER)){
GNode searchClassType = ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION)).getGeneric(1);
// Use the Type's QualifiedIdentifier's class
String searchClassName = Disambiguator.getDotDelimitedName(searchClassType.getGeneric(0));
GNode searchClassDeclaration = inheritanceTree.getClassDeclarationNode(searchClassName);
GNode fieldDeclaration = resolveClassField(n.getString(1), searchClassDeclaration);
if (fieldDeclaration == null) {
throw new NullPointerException();
}
String underscores = ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION)).getString(0);
underscores = underscores.replace('.', '_');
n.set(1, underscores+"_"+n.getString(1));
// Set the value of the reference to the value of the field declaration
n.setProperty(Constants.IDENTIFIER_DECLARATION, fieldDeclaration);
n.setProperty(Constants.IDENTIFIER_TYPE, fieldDeclaration.getGeneric(1));
}
// Bye this point we should have figured out what the selectionExpression is referring to
if (selectionExpressionDepth == 0 && n.getProperty(Constants.IDENTIFIER_DECLARATION) == null){
throw new RuntimeException("Selected unknown class or field!");
}
return n.getStringProperty(Constants.IDENTIFIER_TYPE);
}
public void visitInstanceOfExpression(GNode n) {
String rightSide =
Type.getClassTypeName(n.getGeneric(1).getGeneric(0));
n.set(1, rightSide);
visit(n);
}
public String visitCastExpression(GNode n){
visit(n);
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.CLASS_IDENTIFIER);
GNode classDeclaration = inheritanceTree.getClassDeclarationNode(Disambiguator.getDotDelimitedName(n.getGeneric(0).getGeneric(0)));
n.setProperty(Constants.IDENTIFIER_DECLARATION, classDeclaration);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, n.getGeneric(0));
return Constants.CLASS_IDENTIFIER;
}
public String visitCallExpression(GNode n){
visit(n);
GNode caller = n.getGeneric(0);
GNode callerType;
String callType;
if (caller != null){
if (caller.getProperty(Constants.IDENTIFIER_TYPE) == Constants.PRINT_IDENTIFIER){
//Ignore print expressions, they are evil :P
return null;
}
callerType = (GNode)caller.getProperty(Constants.IDENTIFIER_TYPE_NODE);
if (callerType.size() > 1 && null != callerType.getGeneric(1)) {
GNode newQualifiedIdentifier =
GNode.create("QualifiedIdentifier", "__rt", "Array");
GNode newTypeNode = GNode.create("Type", newQualifiedIdentifier, null);
callerType = newTypeNode;
}
if (caller.getProperty(Constants.IDENTIFIER_TYPE) == Constants.QUALIFIED_CLASS_IDENTIFIER){
callType = Constants.CALL_UNKNOWN;
}
// It must be a call from some object
else {
callType = Constants.CALL_UNKNOWN;
}
}
else {
callerType = GNode.create("Type", Disambiguator.disambiguate(cppClass.getString(0)), null);
// We cannot know if this is a static or dynamic call, we need MethodResolver to determine that
callType = Constants.CALL_UNKNOWN;
}
GNode argumentTypes = GNode.create("ArgumentTypes");
for ( Object o : n.getGeneric(3)) {
argumentTypes.add(((GNode)o).getProperty(Constants.IDENTIFIER_TYPE_NODE));
}
GNode callInfo = null;
try {
callInfo = MethodResolver.resolve(n.getString(2), callerType, argumentTypes, inheritanceTree, callType, cppClass);
}
catch (Exception e) {
e.printStackTrace(System.err);
System.exit(1);
}
// Rename the call
n.set(2, callInfo.getString(0));
GNode calledMethod = callInfo.getGeneric(2);
n.setProperty("static", calledMethod.getProperty("static"));
n.setProperty("private", calledMethod.getProperty("private"));
GNode returnType = callInfo.getGeneric(1);
if (returnType.getGeneric(0).getName().equals("QualifiedIdentifier")){
n.setProperty(Constants.IDENTIFIER_TYPE, Constants.CLASS_IDENTIFIER);
n.setProperty(Constants.IDENTIFIER_DECLARATION,
inheritanceTree.getClassDeclarationNode(Disambiguator
.getDotDelimitedName(returnType.getGeneric(0))));
}
else {
n.setProperty(Constants.IDENTIFIER_TYPE,
Constants.PRIMITIVE_TYPE_IDENTIFIER);
}
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, returnType);
return n.getStringProperty(Constants.IDENTIFIER_TYPE);
}
public void visitDeclarator(GNode n) {
if (null != n.getGeneric(1)) {
GNode fakePrimary = GNode.create("PrimaryIdentifier", n.getString(0));
fakePrimary.setProperty(Constants.SCOPE,
n.getProperty(Constants.SCOPE));
GNode fieldNode = resolveScopes(fakePrimary);
fieldNode.getGeneric(1).set(1, n.getGeneric(1));
}
visit(n);
}
public void visitSubscriptExpression(GNode n){
dispatch(n.getGeneric(0));
GNode primaryIdentifierType =
(GNode)n.getGeneric(0).getProperty(Constants.IDENTIFIER_TYPE_NODE);
// Make a Declarators of one dimension lower
GNode newDimensions = GNode.create("Dimensions");
for (int i = 1; i < primaryIdentifierType.getGeneric(1).size(); i++)
newDimensions.add("[");
//If there are none, make it null
if (newDimensions.size() == 0)
newDimensions = null;
GNode newTypeNode = GNode.create("Type", primaryIdentifierType.getGeneric(0), newDimensions);
n.setProperty(Constants.IDENTIFIER_TYPE_NODE, newTypeNode);
dispatch(n.getGeneric(1));
}
/**
* Set the appropriate properties for a new class expression so it can be nested
*/
public String visitNewClassExpression(GNode n) {
GNode classType = n.getGeneric(2);
dispatch(classType);
n.setProperty(Constants.IDENTIFIER_TYPE,
classType.getProperty(Constants.IDENTIFIER_TYPE));
n.setProperty(Constants.IDENTIFIER_DECLARATION,
classType.getProperty(Constants.IDENTIFIER_DECLARATION));
n.setProperty(Constants.IDENTIFIER_TYPE_NODE,
classType.getProperty(Constants.IDENTIFIER_TYPE_NODE));
return n.getStringProperty(Constants.IDENTIFIER_TYPE);
}
/*
public void tempVisitCallExpression(GNode n) {
if (n.getGeneric(0).getGeneric(0).getString(0).equals("System")
&& n.getGeneric(0).getString(1).equals("out")) {
String option = (n.getString(2).equals("println")) ? " << endl" : null;
GNode printBody = dispatch(n.getGeneric(3));
}
cpp.add(GNode.create("PrintExpression", option, printBody));
}
public void tempVisitArguments(GNode n) {
GNode body = GNode.create("PrintBody");
visit(n);
for (Object o : n) {
if (o instanceof Node) {
body.add((GNode)o);
}
else {
body.add((String)o);
}
}
cpp.add(body);
}
public void tempVisitAdditiveExpression(GNode n) {
visit(n);
GNode expr;
left = dispatch(n.getGeneric(0));
right = dispatch(n.getGeneric(2));
if (getType(left) == getType(right)) {
expr = GNode.create("AdditiveExpression", left, n.getString(1), right);
}
else {
expr = GNode.create("ConcatExpression", left, "<<", right);
}
cpp.add(expr);
}
*/
public void visit(Node n) {
for (Object o : n) if (o instanceof Node) dispatch((Node)o);
}
}.dispatch(java);
return null;
}
public GNode getType(GNode n) {
//declaration = n.getProperty(Constants.SCOPE).node();
return null;
}
private GNode resolveScopes(GNode primaryIdentifier){
SymbolTable.Scope scope = (SymbolTable.Scope)primaryIdentifier.getProperty(Constants.SCOPE);
if (scope == null) {
return null;
}
GNode result = (GNode)scope.lookup(primaryIdentifier.getString(0));
if (result == null){
}
return result;
}
private GNode resolveClassField(String fieldName){
GNode targetClass = cppClass;
GNode fieldDeclaration = null;
while (targetClass != null && !targetClass.getString(0).equals("java.lang.Object")){
HashMap<String, GNode> fieldNameMap = (HashMap<String, GNode>)cppClass.getProperty("FieldMap");
fieldDeclaration = fieldNameMap.get(fieldName);
if (fieldDeclaration != null){
fieldDeclaration.setProperty("ContainingClass", targetClass);
return fieldDeclaration;
}
targetClass = (GNode)targetClass.getProperty("ParentClassNode");
}
return fieldDeclaration;
}
// Overload to refer to another class
private GNode resolveClassField(String fieldName, GNode cppClass){
HashMap<String, GNode> fieldNameMap = (HashMap<String, GNode>)cppClass.getProperty("FieldMap");
GNode fieldDeclaration = fieldNameMap.get(fieldName);
return fieldDeclaration;
}
}