-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImplementationPrinter.java
More file actions
1446 lines (1222 loc) · 40.5 KB
/
ImplementationPrinter.java
File metadata and controls
1446 lines (1222 loc) · 40.5 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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package qimpp;
import java.util.Iterator;
import java.util.HashMap;
import xtc.tree.LineMarker;
import xtc.tree.Node;
import xtc.tree.GNode;
import xtc.tree.Pragma;
import xtc.tree.Printer;
import xtc.tree.SourceIdentity;
import xtc.tree.Token;
import xtc.tree.Visitor;
/**
* A pretty printer for C++ implementation.
*
* @author QIMPP
*/
public class ImplementationPrinter extends Visitor {
/**
* The printer.
*/
protected final Printer printer;
/**
* The current class in the traversal.
*/
protected String currentClass;
/**
* The GNode of the current class
*/
protected GNode currentClassNode;
/**
* The namespace of the current class (with a trailing :: for convenience)
*/
protected String currentNamespace;
/**
* The main method that is printed at the end of the C++ file.
*/
protected GNode mainMethod;
/**
* The flag for whether to line up declarations and statements with
* their source locations.
*/
protected final boolean lineUp;
/** The operator precedence level for the current expression. */
protected int precedence;
/** The flag for whether we just printed a declaration. */
protected boolean isDeclaration;
/** The flag for whether we just printed a statement. */
protected boolean isStatement;
/** The flag for whether the last statement ended with an open line. */
protected boolean isOpenLine;
/**
* The flag for whether the current statement requires nesting or
* for whether the current declaration is nested within a for
* statement.
*/
protected boolean isNested;
/**
* The flag for whether this statement is the else clause of an
* if-else statement.
*/
protected boolean isIfElse;
/**
* The flag for whether you are in the main method
*
*/
protected boolean inMain;
/**
* The flag for whether we're making a reference to a class name or an instance
*/
protected boolean isTypeStaticReference;
/**
* The list precedence level. This level corresponds to the
* assignment expression nonterminal.
*/
public static final int PREC_LIST = 10;
/**
* The base precedence level. This level corresponds to the
* expression nonterminal.
*/
public static final int PREC_BASE = 0;
/** The flag for any statement besides an if or if-else statement. */
public static final int STMT_ANY = 0;
/** The flag for an if statement. */
public static final int STMT_IF = 1;
/** The flag for an if-else statement. */
public static final int STMT_IF_ELSE = 2;
/** The inheritance tree */
public InheritanceTreeManager inheritanceTree;
/** The root of the CPP AST*/
public GNode compilationUnit;
/**
* Create a new C++ printer.
*
* @param printer The printer.
* @param lineUp The flag for whether to line up declarations and
* statements with their source locations.
*/
public ImplementationPrinter(Printer printer, InheritanceTreeManager inheritanceTree, GNode compilationUnit) {
this.printer = printer;
this.lineUp = true;
this.inheritanceTree = inheritanceTree;
this.compilationUnit = compilationUnit;
printer.register(this);
}
/**
* Enter an expression contexti (Java AST). The new context has the specified
* precedence level.
*
* @see #exitContext(int)
*
* @param prec The precedence level for the expression context.
* @return The previous precedence level.
*/
protected int enterContext(int prec) {
int old = precedence;
precedence = prec;
return old;
}
/**
* Enter an expression context. The new context is appropriate for
* an operand opposite the associativity of the current operator.
* For example, when printing an additive expression, this method
* should be called before printing the second operand, as additive
* operators associate left-to-right.
*
* @see #exitContext(int)
*
* @return The previous precedence level.
*/
protected int enterContext() {
int old = precedence;
precedence += 1;
return old;
}
/**
* Exit an expression context.
*
* @see #enterContext(int)
* @see #enterContext()
*
* @param prec The previous precedence level.
*/
protected void exitContext(int prec) {
precedence = prec;
}
/**
* Print an expression as a truth value. This method prints the
* specified node. If that node represents an assignment expression
* and {@link #EXTRA_PARENTHESES} is <code>true</code>, this method
* adds an extra set of parentheses around the expression to avoid
* gcc warnings.
*
* @param n The node to print.
*/
protected void formatAsTruthValue(Node n) {
if (GNode.cast(n).hasName("AssignmentExpression")) {
printer.p('(').p(n).p(')');
} else {
printer.p(n);
}
}
/**
* Start a new statement (C AST). This method and the corresponding {@link
* #prepareNested()} and {@link #endStatement(boolean)} methods
* provide a reasonable default for newlines and indentation when
* printing statements. They manage the {@link #isDeclaration},
* {@link #isStatement}, {@link #isOpenLine}, {@link #isNested}, and
* {@link #isIfElse} flags.
*
* @param kind The kind of statement, which must be one of the
* three statement flags defined by this class.
* @param node The statement's node.
* @return The flag for whether the current statement is nested.
*/
protected boolean startStatement(int kind, Node node) {
if (isIfElse && ((STMT_IF == kind) || (STMT_IF_ELSE == kind))) {
isNested = false;
} else {
if (lineUp) {
if (isOpenLine) printer.pln();
printer.lineUp(node);
} else {
if (isDeclaration || isOpenLine) {
printer.pln();
}
}
if (isNested) {
printer.incr();
}
}
isOpenLine = false;
boolean nested = isNested;
isNested = false;
return nested;
}
// Java AST
protected boolean startStatement(int kind) {
if (isIfElse && ((STMT_IF == kind) || (STMT_IF_ELSE == kind))) {
isNested = false;
}
else {
if (isOpenLine) printer.pln();
if (isDeclaration) printer.pln();
if (isNested) printer.incr();
}
isOpenLine = false;
boolean nested = isNested;
isNested = false;
return nested;
}
/**
* Prepare for a nested statement.
*
* @see #startStatement
*/
protected void prepareNested() {
isDeclaration = false;
isStatement = false;
isOpenLine = true;
isNested = true;
}
protected void endStatement(boolean nested) {
if (nested) {
printer.decr();
}
isDeclaration = false;
isStatement = true;
}
/**
* Start printing an expression at the specified operator precedence
* level.
*
* @see #endExpression(int)
*
* @param prec The expression's precedence level.
* @return The previous precedence level.
*/
protected int startExpression(int prec) {
if (prec < precedence) {
printer.p('(');
}
int old = precedence;
precedence = prec;
return old;
}
/**
* Stop printing an expression.
*
* @see #startExpression(int)
*
* @param prec The previous precedence level.
*/
protected void endExpression(int prec) {
if (precedence < prec) {
printer.p(')');
}
precedence = prec;
}
/**
* Print empty square brackets for the given number of dimensions.
*
* @param n Number of dimensions to print.
*/
protected void formatDimensions(final int n) {
for (int i=0; i<n; i++) printer.p("[]");
}
/** Visit the specified compilation unit node. */
public void visitCompilationUnit(GNode n) {
printer.p("#include <iostream>\n");
printer.p("#include <sstream>\n");
printer.p("#include <string>\n");
printer.p("#include \"out.h\"\n\n");
printer.pln();
visit(n);
printer.flush();
}
/** Visit the specified define preprocessing directive node. */
public void visitDefineDirective(GNode n) {
// Do nothing for now.
}
/** Visit the specified using preprocessing node. */
public void visitUsing(GNode n) {
// Do nothing for now.
}
/** Visit the specified namespace node. */
public void visitNamespace(GNode n) {
// Do nothing for now.
}
//TODO: HACK
boolean inClassDeclaration = false;
/** Visit the specified class declaration node. */
public void visitClassDeclaration(GNode n) {
this.currentClass = getClassName(n.getString(0));
this.currentNamespace = getNamespace(n.getString(0));
this.currentClassNode = n;
// .class
printer.p("java::lang::Class").p(" ").p(currentNamespace).p("__").p(this.currentClass)
.pln("::__class() {");
printer.incr();
indentOut()
.p("static java::lang::Class k = ")
.p("new java::lang::__Class(__rt::literal(\"")
.p(this.currentClassNode.getString(0)).p("\"), ");
//TODO: HACK
isTypeStaticReference = true;
dispatch(n.getGeneric(1));
isTypeStaticReference = false;
printer.pln("::__class());");
indentOut().pln("return k;").pln("}\n");
// vtable
printer.p(currentNamespace).p("__").p(this.currentClass).p("_VT ")
.p(currentNamespace).p("__").p(this.currentClass).pln("::__vtable;\n");
printer.decr();
visit(n.getGeneric(2));
//visit(n.getGeneric(3));
visit(n.getGeneric(4));
printer.flush();
printer.pln();
}
//TODO:HACK - We want a consistent syntax for "this" in constructor
boolean inConstructor = false;
/** Visit the specified constructor declaration node. */
public void visitConstructorDeclaration(GNode n){
// class constructor
inConstructor = true;
printer.p(currentNamespace).p("__").p(this.currentClass).p("::__")
.p(this.currentClass)
.p("() : __vptr(&__vtable) ");
printer.incr();
indentOut();
dispatch(n.getGeneric(1));
if (n.getGeneric(1) == null || !n.getGeneric(1).getName().equals("Block")){
printer.p("{}");
}
printer.decr();
printer.pln();
inConstructor = false;
}
/** Visit the specified parent class node. */
public void visitParent(GNode n) {
visit(n);
}
/** Refrain from going deeper when visiting an inherited method
* - we don't need to print its implementation */
public void visitInheritedMethodContainer(GNode n){
return;
}
boolean inMethod = false;
boolean didMain = false;
boolean staticMethod = false;
/**
* Visit the specified method declaration node.
* Only visited in implemented methods.
*/
public void visitImplementedMethodDeclaration(GNode n) {
inMain = false;
inMethod = true;
if (n.getString(0).equals("main")) {
//TODO:HACK
// We only want the main method of the entry class to be the official main
// so set this only once
// Also, this will be confused by methods called main with the wrong signature in
// Java.
if (!didMain){
mainMethod = n;
inMain = true;
didMain = true;
}
}
dispatch(n.getGeneric(1)); // return type
if (!inMain) {
printer.p(" ").p(currentNamespace).p("__").p(this.currentClass);
printer.p("::").p(Type.getCppMangledMethodName(n)); // method name
if (n.getProperty("static") != null)
staticMethod = true;
//Print the FormalParameters
dispatch(n.getGeneric(2));
staticMethod = false;
}
else {
printer.p(" main(int argc, char** argv)"); // method name
}
dispatch(n.getGeneric(3)); // block
printer.flush();
inMethod = false;
inMain = false;
}
boolean printedInitializers;
public void visitBlock(GNode n) {
printer.pln(" {");
printer.incr();
if (inMain && !printedInitializers){
StaticInitializerPrinter sip = new StaticInitializerPrinter(printer);
sip.dispatch(compilationUnit);
printedInitializers = true;
}
indentOut();
visit(n); // block
printer.decr();
printer.pln("}\n");
}
//TODO: HACK
boolean inPrintStatement = false;
public void visitArguments(GNode n){
for (int i = 0; i < n.size(); i++){
if (n.get(i) instanceof String){
printer.p(n.getString(i));
}
else {
dispatch(n.getNode(i));
}
if ( i < (n.size() - 1))
printer.p(", ");
}
}
// TODO: HACK
boolean inCallExpression = true;
public void visitCallExpression(GNode n) {
//If we're using a local call
inCallExpression = true;
boolean staticCall = false;
if (n.getGeneric(0) == null){
//Print the call
if (n.getProperty("static") != null && n.getProperty("private") != null){
printer.p(" __this->__vptr->");
} else {
printer.p(Type.getClassTypeName(currentClassNode.getString(0))).p("::");
}
printer.p(n.getString(2));
printer.p("(");
// Print the parameters
if (n.getProperty("static") == null){
printer.p(" __this ");
if (n.getGeneric(3).size()!= 0)
printer.p(", ");
}
dispatch(n.getGeneric(3));
printer.p(")");
}
else if (n.getGeneric(0).getProperty(Constants.IDENTIFIER_TYPE) == Constants.PRINT_IDENTIFIER)
{
indentOut().p("std::cout << ");
inPrintStatement = true;
inCallExpression = false;
if(0 == n.getGeneric(3).size()) printer.p("\"\"");
else visit(n);
inCallExpression = true;
inPrintStatement = false;
// printer.p(")");
if (n.getString(2).equals("println")){
printer.p(" << std::endl");
}
}
else {
if (n.getProperty("static") == null){
//TODO: Chained calls
// Print the correct call here
// Get the type of the calling expression or field name, and make a _this to reference it
// It is necessarily an instance, and should be associated with a QualifiedIdentifier
GNode callingTypeNode = (GNode)n.getGeneric(0).getProperty(Constants.IDENTIFIER_TYPE_NODE);
printer.p("({ ");
dispatch(callingTypeNode);
printer.p(" _this = ");
//Print the nested expression
dispatch(n.getGeneric(0));
//End the expression;
printer.p(" ;");
// Print the actual call
if (n.getProperty("private") == null){
printer.p(" _this->__vptr->");
}
else{
printer.p(" _this->");
}
printer.p(n.getString(2)).p("( _this ");
// We don't want to print the comma if there are not more arguments
if (n.getGeneric(3).size()!= 0){
printer.p(", ");
dispatch(n.getGeneric(3));
}
printer.p(");").p(" })");
}
else {
GNode callingTypeNode = (GNode)n.getGeneric(0).getProperty(Constants.IDENTIFIER_TYPE_NODE);
isTypeStaticReference = true;
dispatch(callingTypeNode);
isTypeStaticReference = false;
printer.p("::").p(n.getString(2)).p("(");
dispatch(n.getGeneric(3));
printer.p(")");
}
}
inCallExpression = false;
printer.flush();
}
/**
* Visit the specified class instantiation, and print internal types in
* varying modes, static for the instantiated type, instance for the argument
* types
*/
public void visitNewClassExpression(GNode n){
//Indicate that the reference to the type is the underscore name, not an instance
isTypeStaticReference = true;
printer.p(" new ");
// Dispatch on the Type node
dispatch(n.getGeneric(2));
printer.p("(");
isTypeStaticReference = false;
dispatch(n.getGeneric(3));
printer.p(")");
}
/**
* Visit the specified array instantiantiation
*/
public void visitNewArrayExpression(GNode n){
int arrayDimCount = 0;
int concreteDimCount;
// Total dimensions is the number of ConcreteDimensions + the number of Dimensions
arrayDimCount += n.getGeneric(1).size();
concreteDimCount = arrayDimCount;
if (n.getGeneric(2) != null){
arrayDimCount += n.getGeneric(2).size();
}
printer.p(" ({ ")
.p(" int32_t dim = ");
// Get the first dimension in the concrete dimensions node.
dispatch(n.getGeneric(1).getGeneric(0));
printer.p(";");
for (int i = 0; i < arrayDimCount; i++)
printer.p(" __rt::Ptr<__rt::Array< ");
// Dispatch on the Type node
dispatch(n.getGeneric(0));
for (int i = 0; i < arrayDimCount; i++)
printer.p(" > > ");
printer.p(" temp = new __rt::Array< ");
for (int i = 1; i < arrayDimCount; i++)
printer.p(" __rt::Ptr<__rt::Array< ");
// Dispatch on the Type node
dispatch(n.getGeneric(0));
for (int i = 1; i < arrayDimCount; i++)
printer.p(" > > ");
printer.p("> (dim); ");
if (concreteDimCount > 1){
// Print the for loop to fill it, recursing on a smaller type and dimensions node
printer.p("for (int32_t i = 0; i < dim; i++){\n temp->__data[i] = ");
GNode newConcreteDimensions = GNode.create("ConcreteDimensions");
GNode newDimensions = GNode.create("Dimensions");
for (int i = 1; i < concreteDimCount; i++)
newConcreteDimensions.add(n.getGeneric(1).get(i));
for (int i = 1; i < (arrayDimCount - concreteDimCount); i++)
newDimensions.add(n.getGeneric(1));
GNode newArrayExpression = GNode.create("NewArrayExpression", n.getGeneric(0), newConcreteDimensions, newDimensions, null);
visitNewArrayExpression(newArrayExpression);
printer.p(" ; } ; ");
}
// Make sure the returned value is the array
printer.p(" temp ; })");
}
boolean inReturnType = false;
/** Visit the specified return type node. */
public void visitReturnType(GNode n) {
inReturnType = true;
try {
if (n.get(0) != null) {
visit(n);
}
} catch (Exception e) {
e.printStackTrace();
}
inReturnType = false;
}
/** Visit the specified from class node. */
public void visitFrom(GNode n) {
visit(n);
}
boolean dontCheckNull;
/** Visit the specified expression node. */
public void visitExpression(GNode n) {
dontCheckNull = true;
dispatch(n.getGeneric(0));
dontCheckNull = false;
printer.p(' ').p(n.getString(1)).p(' ');
dispatch(n.getGeneric(2));
}
public void visitExpressionStatement(GNode n) {
visit(n);
printer.pln(";");
}
int selectionExpressionDepth = 0;
/** Visit a SelectionExpression node, and print at the most shallow level */
public void visitSelectionExpression(GNode n){
// Don't do anything for print commands
if (n.getProperty(Constants.IDENTIFIER_TYPE) == Constants.PRINT_IDENTIFIER)
return;
String childIdentifierType = n.getGeneric(0).getStringProperty(Constants.IDENTIFIER_TYPE);
GNode childIdentifierDeclaration = (GNode) n.getGeneric(0).getProperty(Constants.IDENTIFIER_DECLARATION);
if(childIdentifierType == Constants.QUALIFIED_CLASS_IDENTIFIER){
printer.p(Type.getClassTypeName(n.getGeneric(0).getString(0)));
} else {
selectionExpressionDepth++;
GNode type = (GNode)dispatch(n.getGeneric(0));
selectionExpressionDepth--;
}
// Note: Speeding up String comparisons since we're using constants. USE THEM!
if (childIdentifierType == Constants.CLASS_IDENTIFIER
|| childIdentifierType == Constants.STACKVAR_IDENTIFIER
|| childIdentifierType == Constants.FIELD_IDENTIFIER
|| childIdentifierType == Constants.FOREIGN_CLASS_FIELD_IDENTIFIER)
{
printer.p("->").p(n.getString(1));
}
else if ( childIdentifierType == Constants.QUALIFIED_CLASS_IDENTIFIER ) {
printer.p("::").p(n.getString(1));
}
}
public void visitSubscriptExpression(GNode n){
dispatch(n.getGeneric(0));
printer.p("->__data[");
dispatch(n.getGeneric(1));
printer.p("]");
}
/**
* Visit the specified primary identifier node.
*
* @return A node containing the name, the Type, and whether this variable
* is stack allocated GNode("Name", Type(...), true)
*/
public void visitPrimaryIdentifier(GNode n) {
boolean isQualifiedIdentifier = false;
GNode typeNode = (GNode)n.getProperty(Constants.IDENTIFIER_TYPE_NODE);
if (typeNode != null && typeNode.getGeneric(0).getName().equals("QualifiedIdentifier"))
isQualifiedIdentifier = true;
if (inCallExpression && !inConstructor && !dontCheckNull && isQualifiedIdentifier) {
printer.p("({").p(" __rt::checkNotNull(");
if (null != n.getProperty(Constants.IDENTIFIER_DECLARATION) && null == ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION))
.getProperty("static") &&
n.getProperty(Constants.IDENTIFIER_TYPE) == Constants.FIELD_IDENTIFIER) {
if (inConstructor)
printer.p("this->");
else
printer.p("__this->");
} else if (null != n.getProperty(Constants.IDENTIFIER_DECLARATION) && null != ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION))
.getProperty("static")) {
GNode fieldDeclaration = (GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION);
String className = ((GNode)fieldDeclaration.getProperty("ContainingClass")).getString(0);
printer.p(Type.getClassTypeName(className)).p("::");
} /*else if (null != n.getProperty(Constants.IDENTIFIER_DECLARATION) && null != ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION)).getProperty("static")){
int colonIndex = n.getString(0).indexOf("::");
if(colonIndex != -1){
String className = n.getString(0).substring(0, n.getString(0).indexOf("::"));
n.set(0, Type.getClassTypeName(className) + n.getString(0).substring(n.getString(0).indexOf("::")));
}
}*/
//TODO: change this
//GNode typeNode = (GNode)n.getProperty(Constants.IDENTIFIER_TYPE_NODE);
//if (inPrintStatement && typeNode != null) {
// if (typeNode.getGeneric(0).getString(0).equals("boolean")) {
//
// printer.p("str(");
// }
//}
// Make sure to delimit fully-qualified names correctly
printer.p(n.getString(0).replace(".", "::"));
printer.p("); ");
}
if (typeNode != null && typeNode.getGeneric(0).getString(0).equals("byte")
&& (inPrintStatement || inConcatExpression)) {
printer.p("(int)");
}
if (null == ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION))
.getProperty("static") &&
n.getProperty(Constants.IDENTIFIER_TYPE) == Constants.FIELD_IDENTIFIER) {
if (inConstructor) printer.p("this->");
else printer.p("__this->");
} else if ( null != n.getProperty(Constants.IDENTIFIER_DECLARATION) && null != ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION))
.getProperty("static")) {
GNode fieldDeclaration = (GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION);
String className = ((GNode)fieldDeclaration.getProperty("ContainingClass")).getString(0);
printer.p(Type.getClassTypeName(className)).p("::");
} /* else if (null != n.getProperty(Constants.IDENTIFIER_DECLARATION) && null != ((GNode)n.getProperty(Constants.IDENTIFIER_DECLARATION)).getProperty("static")){
int colonIndex = n.getString(0).indexOf("::");
if(colonIndex != -1){
String className = n.getString(0).substring(0, n.getString(0).indexOf("::"));
n.set(0, Type.getClassTypeName(className) + n.getString(0).substring(n.getString(0).indexOf("::")));
}
}*/
printer.p(n.getString(0).replace(".", "::"));
//if (inPrintStatement && typeNode != null) {
// if (typeNode.getGeneric(0).getString(0).equals("boolean")) {
// printer.p(")");
// }
//}
if (inCallExpression && !inConstructor && !dontCheckNull && isQualifiedIdentifier) {
printer.p("; })");
}
}
public void visitThisExpression(GNode n){
if (inConstructor)
printer.p("this");
else
printer.p("__this");
}
/** Visit the specified instance node. */
public void visitInstance(GNode n) {
printer.p("__this->");
visit(n);
}
/** Visit the specified string literal node. */
public void visitStringLiteral(GNode n) {
final int prec = startExpression(160);
// if (!inPrintStatement)
printer.p("__rt::literal(");
printer.p(n.getString(0));
//if (!inPrintStatement)
printer.p(")");
endExpression(prec);
}
/** Visit the specified boolean literal. */
public void visitBooleanLiteral(GNode n) {
final int prec = startExpression(160);
printer.p(n.getString(0));
endExpression(prec);
}
/** Visit the specified formal parameters node. */
public void visitFormalParameters(GNode n) {
printer.p('(');
boolean firstArg = true;
if (!staticMethod){
printer.p(this.currentClass).p(" __this");
firstArg = false;
}
for (Iterator<?> iter = n.iterator(); iter.hasNext(); ) {
if(!firstArg)
printer.p(", ");
firstArg = false;
printer.p((Node)iter.next());
}
printer.p(')');
}
/** Visit the specified type node. */
public void visitType(GNode n) {
GNode dimensions = n.getGeneric(1);
if (dimensions != null) {
for (int i = 0; i < dimensions.size(); i++) {
printer.p(" __rt::Ptr<__rt::Array<");
}
}
visit(n);
if (dimensions != null) {
for (int i = 0; i < dimensions.size(); i++) {
printer.p(" > > ");
}
}
}
/** Visit the specified field declaration. */
public void visitFieldDeclaration(GNode n) {
printer.indent().p(n.getNode(0)).p(n.getNode(1)).p(' ').p(n.getNode(2)).
p(';').pln();
isDeclaration = true;
isOpenLine = false;
}
/** Visit the specified primitive type node. */
public void visitPrimitiveType(GNode n) {
printer.p(Type.primitiveType(n.getString(0)));
}
/** Visit the specified qualified identifier node. */
public void visitQualifiedIdentifier(GNode n) {
for (Iterator<?> iter = n.iterator(); iter.hasNext(); ) {
String identifierName = (String)iter.next();
if (iter.hasNext()) {
printer.p(identifierName);
printer.p("::");
}
else {
//TODO: HACK
if ( isTypeStaticReference ) {
//(inMethod && !inReturnType) || (inClassDeclaration)) {
printer.p("__").p(identifierName);
}
else {
printer.p(identifierName);
}
}
}
}
/** Visit the specified formal parameter node. */
public void visitFormalParameter(GNode n) {
dispatch(n.getGeneric(1));
printer.p(' ').p(n.getString(0));
}
/** Visit the specified break statement node. */
public void visitBreakStatement(GNode n) {
printer.pln("break;\n");
}
/** Visit the specified continue statement node. */
public void visitContinueStatement(GNode n) {
printer.pln("continue;\n");
}
/** Visit the specified return statement node. */
public void visitReturnStatement(GNode n) {
printer.p("return");
if (null != n.getNode(0)) {
printer.p(' ');
dispatch(n.getNode(0));
}
printer.p(";\n");
}
/** Visit the specified print expression node. */
public void visitPrintExpression(GNode n) {
printer.p("cout <<");
visit(n);
printer.pln(";\n");
}
/** Visit the specified option node. */
public void visitOption(GNode n) {
// Do nothing for now
}
/** Visit the specified arguments. */
/*
public void visitArguments(GNode n) {
if (!inPrintStatement)
printer.p('(');
for (Iterator<Object> iter = n.iterator(); iter.hasNext(); ) {
final int prec = enterContext(PREC_LIST);
printer.p((Node)iter.next());
exitContext(prec);
if (iter.hasNext()) printer.p(", ");
}
if(!inPrintStatement)
printer.p(')');
}
*/
// TODO: CHANGE THIS BACK
/** Visit the specified arguments node. */
/* public void visitArguments(GNode n) {
visit(n); // one string literal for now
} */
/** Visit the specified string concatination expression node. */
public void visitStringConcatExpression(GNode n) {
printer.p("new java::lang::__String(");
for (Iterator<?> iter = n.iterator(); iter.hasNext(); ) {
dispatch((Node)iter.next());
printer.p("->data");
if (iter.hasNext()) {
printer.p(" + ");
}
}
}
boolean inConcatExpression = false;
/** Visit the specified additive expression. */