-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCPPAST.java
890 lines (762 loc) · 32.9 KB
/
CPPAST.java
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
/*
* xtc - The eXTensible Compiler
* Copyright (C) 2012 Robert Grimm
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package qimpp;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.PrintWriter;
import java.util.HashMap;
import xtc.lang.JavaFiveParser;
import xtc.parser.ParseException;
import xtc.parser.Result;
import xtc.tree.GNode;
import xtc.tree.Node;
import xtc.tree.Visitor;
import xtc.tree.Location;
import xtc.tree.Printer;
import xtc.util.Tool;
import java.util.ArrayList;
/**
* A translator from (a subset of) Java to (a subset of) C++.
*
* @author QIMPP
* @version $Revision$
*/
public class CPPAST {
public GNode compilationUnit, directives, declarations, classes;
HashMap<String, GNode> classesMap;
HashMap<String, GNode> currentFieldMap;
HashMap<String, ArrayList<GNode> > currentMethodMap;
/** Constructor */
public CPPAST() {
compilationUnit = GNode.create("CompilationUnit");
compilationUnit.addNode(createDefaultDirectives());
declarations = GNode.create("Declarations");
compilationUnit.addNode(declarations);
classes = GNode.create("Classes");
compilationUnit.addNode(classes);
classesMap = new HashMap<String, GNode>();
//setup HashMaps as needed
setupMaps();
}
/** Creates default directives in AST. */
GNode createDefaultDirectives() {
directives = GNode.create("Directives");
directives.addNode(GNode.create("Pragma")).getGeneric(directives.size()-1).add("once");
GNode includeDirectives = GNode.create("IncludeDirectives");
includeDirectives.addNode(GNode.create("QuotedForm")).getGeneric(includeDirectives.size()-1).add("java_lang");
includeDirectives.addNode(GNode.create("AngleBracketForm")).getGeneric(includeDirectives.size()-1).add("stdint");
directives.addNode(includeDirectives);
return directives;
}
/*Methods to add:
*GNode addClass(String name) - adds a class and a struct to the tree returns a GNode to the class
*GNode addField(String name, String type, GNode class) - adds a field to a class returns the GNode of the field
*GNode addMethod(String name, String returnType, GNode class) - adds a method to a class returns the method GNode
*GNode addMethodInstruction(GNode instruction, GNode method) - adds instruction to method
*GNode addMethodParameter(String paramType, String param, GNode method) - adds a parameter to method
*
*GNode getClass(String name) - gets a GNode to a class by it's name
*GNode getMethod(String name, GNode* class) - gets a GNode to a method by it's name
*GNode getField(String name, GNode* class) - gets a GNode to a Field by it's name
*
*void removeMethod(String name, GNode* class) - removes a method from a class
*void printAST() - prints the AST for debugging
*/
/**
* Add class node.
*
* @param name Name of node.
* @param parent Parent node name.
* @returns class node.
*/
GNode addClass(String name, String parent){
//Add to Structs
GNode declaration = GNode.create("Declaration");
declaration.add(name);
declaration.addNode(GNode.create("Struct"));
declaration.add(2, null);
declarations.addNode(declaration);
//Add to Classes with Name node, Fields node, ImplementedMethods node, and InheritedMethods node
GNode classNode = GNode.create("ClassDeclaration");
classNode.add(name);
if(parent != null){
classNode.addNode(GNode.create("Parent")).getNode(classNode.size()-1).add(parent);
} else {
classNode.addNode(GNode.create("Parent")).getNode(classNode.size()-1).add(generateObjectType());
}
classNode.addNode(GNode.create("Constructors"));
classNode.addNode(GNode.create("Fields"));
classNode.addNode(GNode.create("Methods"));
currentFieldMap = new HashMap<String,GNode>();
classNode.setProperty("FieldMap", currentFieldMap);
currentMethodMap = new HashMap<String, ArrayList<GNode> >();
classNode.setProperty("MethodMap", currentMethodMap);
classes.addNode(classNode);
return classNode;
}
/**
* Add class node.
*
* @param name Name of node.
* @returns class node.
*/
GNode addClass(String name){
return addClass(name, null);
}
//Adding, getting, and removing fields
/**
* Add field node.
*
* @param fullName Name of node, prepended with the fully qualified class name of the class that owns it
* @param ambigName the ambiguous name, no prepend, to be entered into the field map
* @param type Type node.
* @param classNode ClassDeclaration node.
* @returns field node.
*/
GNode addField(String fullName, String ambigName, GNode type, GNode classNode, boolean isStatic){
//Get the fields node
//String classFieldName = classNode.getString(0) + "_" + name;
GNode fieldNode = GNode.create("FieldDeclaration");
fieldNode.add(fullName);
fieldNode.addNode(type);
classNode.getGeneric(3).addNode(fieldNode);
currentFieldMap.put(ambigName, fieldNode);
if(isStatic)
fieldNode.setProperty("static", true);
return fieldNode;
}
/**
* Overload without adding the name to the field map
*/
GNode addField(String name, GNode type, GNode classNode, boolean isStatic){
GNode fieldNode = GNode.create("FieldDeclaration");
fieldNode.add(name);
fieldNode.add(type);
classNode.getGeneric(3).addNode(fieldNode);
if(isStatic)
fieldNode.setProperty("static", true);
return fieldNode;
}
/**
* Add constructor node.
*
* @param classNode The class node.
* @returns constructor node.
*/
GNode addConstructor(GNode classNode){
GNode constructorNode = GNode.create("ConstructorDeclaration");
constructorNode.add(null);
constructorNode.addNode(GNode.create("Block"));
classNode.getGeneric(2).addNode(constructorNode);
return constructorNode;
}
/**
* Add constructor instruction node.
*
* @param instruction Instruction node.
* @param constructor Constructor node.
*/
void addConstructorInstruction(GNode instruction, GNode constructor) {
constructor.getGeneric(1).addNode(instruction);
}
/**
* Add constructor parameter node.
*
* @param paramType Parameter type node.
* @param param Parameter name.
* @param constructor constructor.
*/
void addConstructorParameter(GNode paramType, String param, GNode constructor) {
if(constructor.getGeneric(0) == null) constructor.add(0, GNode.create("Parameters"));
GNode formalParameter = GNode.create("FormalParameter");
formalParameter.add(param);
formalParameter.addNode(paramType);
constructor.getGeneric(0).addNode(formalParameter);
}
/**
* Set constructor parameters with parameters node.
*
* @param parameters Parameters node.
* @param constructor Constructors node.
*/
void setConstructorParameters(GNode parameters, GNode constructor){
constructor.remove(2);
constructor.add(2, parameters);
}
/**
* Set constructor instructions with block node.
*
* @param block Block node.
* @param constructor Constructor node.
*/
void setConstructorInstructions(GNode block, GNode constructor){
constructor.remove(1);
constructor.add(1, block);
}
/**
* Add method node.
*
* @param name Name of method node.
* @param returnType Return type node for method.
* @param classNode Class node.
* @returns method node.
*/
GNode addMethod(String name, GNode returnType, GNode classNode, GNode parameters) {
// If this is the first method we are adding, add Object's methods first
if(classNode.getGeneric(4).size() == 0){
addAllInheritedMethods(generateObjectMethods(), classNode);
}
GNode methodNode = GNode.create("ImplementedMethodDeclaration");
methodNode.add(name);
methodNode.addNode(GNode.create("ReturnType")).getGeneric(methodNode.size()-1).add(returnType.getGeneric(0));
methodNode.add(GNode.create("FormalParameters"));
methodNode.addNode(GNode.create("Block"));
//Find if a method exists with the same name and input. If it does, overwrite it with the new implemented method
boolean overridingMethod = false;
GNode methodsNode = classNode.getGeneric(4);
setMethodParameters(parameters, methodNode);
for(int i = 0; i < methodsNode.size(); i++){
GNode method = methodsNode.getGeneric(i);
if(method.getName().equals("InheritedMethodContainer")) method = method.getGeneric(0);
//TODO: Check for exact match
if(Type.getCppMangledMethodName(method).equals(Type.getCppMangledMethodName(methodNode))){
methodsNode.set(i, methodNode);
overridingMethod = true;
break;
}
}
//Otherwise just add it to the bottom of the methods
if(!overridingMethod)
methodsNode.addNode(methodNode);
return methodNode;
}
/**
* Add method node. Overload for inherited methods
*
* @param name Name of method node.
* @param returnType Return type node for method.
* @param classNode Class node.
* @param from Name of class the method inherits from.
* @returns method node.
*/
GNode addMethod(String name, GNode returnType, GNode classNode, String from, GNode parameters) {
GNode methodNode = GNode.create("InheritedMethodDeclaration");
methodNode.add(name);
methodNode.addNode(GNode.create("ReturnType")).getGeneric(methodNode.size()-1).add(returnType.getGeneric(0));
methodNode.add(GNode.create("FormalParameters"));
methodNode.addNode(GNode.create("From")).getNode(methodNode.size()-1).add(from);
//Find if a method exists with the same name and input. If it does, overwrite it with the new implemented method
boolean overridingMethod = false;
GNode methodsNode = classNode.getGeneric(4);
for(int i = 0; i < methodsNode.size(); i++){
GNode method = methodsNode.getGeneric(i);
if(method.getName().equals("InheritedMethodContainer")) method = method.getGeneric(0);
if(Type.getCppMangledMethodName(method).equals(Type.getCppMangledMethodName(methodNode))){
methodsNode.set(i, methodNode);
overridingMethod = true;
break;
}
}
//Otherwise just add it to the bottom of the methods
if(!overridingMethod)
methodsNode.addNode(methodNode);
return methodNode;
}
/**
* Add method instruction node.
*
* @param instruction Instruction node.
* @param method Method node.
*/
void addMethodInstruction(GNode instruction, GNode method) {
method.getGeneric(3).addNode(instruction);
}
/**
* Set method instruction node.
*
* @param block Block node.
* @param method Method node.
*/
void setMethodInstructions(GNode block, GNode method) {
method.remove(3);
method.add(3, block);
}
/*
* Add method parameter.
*
* @param paramType Parameter type node.
* @param param Parameter name.
* @param method Method node.
* @returns formal parameter node.
*/
GNode addMethodParameter(GNode paramType, String param, GNode method) {
GNode formalParameter = GNode.create("FormalParameter");
formalParameter.add(param);
formalParameter.addNode(paramType);
method.getGeneric(2).addNode(formalParameter);
return formalParameter;
}
//Take in parent methods and class, add parent methods as InheritedMethod nodes to class
void addAllInheritedMethods(GNode parentMethods, GNode currentClass){
for(int i = 0; i < parentMethods.size(); i++){
//By default just assume the method itself is inherited and already has a container
GNode inheritedMethod = parentMethods.getGeneric(i);
if(!parentMethods.getGeneric(i).getName().equals("InheritedMethodContainer")){
//Create a inherited method container
inheritedMethod = GNode.create("InheritedMethodContainer");
//Add it to the container
inheritedMethod.addNode(parentMethods.getGeneric(i));
//Insert the parent in a from node in the inherited container
inheritedMethod.add(GNode.create("From")).getGeneric(1).addNode(currentClass.getGeneric(1));
}
//Add the parent method to the class. If it is inherited from further up the tree than the parent it will already be formatted as an inherited method
// Make sure we don't add private or static methods
if (inheritedMethod.getProperty("static") == null && inheritedMethod.getProperty("private") == null)
currentClass.getGeneric(4).addNode(inheritedMethod);
}
}
void addAllInheritedFields(GNode parentClassNode, GNode currentClass){
currentClass.setProperty("FieldMap", new HashMap<String,GNode>((HashMap<String,GNode>)parentClassNode.getProperty("FieldMap")));
currentFieldMap = (HashMap<String,GNode>)currentClass.getProperty("FieldMap");
for(Object fieldobj : parentClassNode.getGeneric(3)){
GNode field = (GNode)fieldobj;
if (field.getProperty("static") == null)
addField(field.getString(0), field.getGeneric(1), currentClass, null != field.getProperty("static"));
}
}
/**
* Add print expression.
*
* @param option An option, if any.
* @param args Arguments node.
* @returns print expression node.
*/
GNode addPrintExpression(String option, GNode args) {
GNode print = GNode.create("PrintExpression");
print.add("cout");
print.add(GNode.create("Option", option));
print.add(args);
return print;
}
/**
* Set method parameters.
*
* @param parameters Parameters node.
* @param method Method node.
*/
void setMethodParameters(GNode parameters, GNode method){
method.remove(2);
method.add(2, parameters);
}
/**
* Get class node from name.
*
* @param name Class name.
* @returns class node.
*/
GNode getClass(String name) {
return classesMap.get(name);
}
/**
* Get the index of a field with name in the class given by classNode.
*
* @param name Field name.
* @param classNode Class node.
* @returns index of class node.
*/
int getFieldIndex(String name, GNode classNode){
GNode fieldsOfClass = classNode.getGeneric(1);
for(int i = 0; i < fieldsOfClass.size(); i++){
if(getGNodeName(fieldsOfClass.getGeneric(i)).equals(name))
return i;
}
return -1;
}
/**
* Remove field of a class node.
*
* @param name Field name.
* @param classNode Class node.
*/
void removeField(String name, GNode classNode){
int fieldIndex = getFieldIndex(name, classNode);
if(fieldIndex != -1) classNode.getGeneric(1).remove(fieldIndex);
}
//Adding, getting, and removing methods
/**
* Get index of inherited method.
*
* @param name Inherited method name.
* @param classNode Class node.
* @return index of inherited method node.
*/
int getInheritedMethodIndex(String name, GNode classNode) {
GNode inheritedMethods = classNode.getGeneric(5);
for(int i=0; i < inheritedMethods.size(); i++){
if(getGNodeName(inheritedMethods.getGeneric(i)).equals(name))
return i;
}
return -1;
}
/**
* Remove inherited method.
*
* @param name Inherited method name.
* @param classNode Class node.
*/
void removeInheritedMethod(GNode methodDeclaration, GNode classNode) {
final String name = methodDeclaration.getString(3);
int methodIndex = getInheritedMethodIndex(name, classNode);
if(methodIndex != -1) classNode.getGeneric(5).remove(methodIndex);
//
new Visitor () {
public void visitInheritedMethods( GNode n ) {
//
for (Object o : n){
Boolean matches = false;
if (o instanceof Node){
matches = (Boolean)dispatch((Node)o);
//
}
if ( matches == true ) {
n.remove(n.indexOf(o));
//
}
}
}
public Boolean visitMethodDeclaration ( GNode n ) {
//
if (n.getString(0).equals(name)){
return true;
}
else {
return false;
}
}
public void visit(Node n) {
////
for (Object o : n) if (o instanceof Node) dispatch((Node)o);
}
}.dispatch(classNode);
}
//Utility methods
GNode generateObjectType(){
GNode qi = GNode.create("QualifiedIdentifier");
qi.add("java").add("lang").add("Object");
GNode type = GNode.create("Type");
type.add(qi);
type.add(null);
return type;
}
GNode generateObjectClassDeclaration(){
GNode methods = generateObjectMethods();
GNode objectDec =
GNode.create("ClassDeclaration", "java.lang.Object", null, null, null, methods);
return objectDec;
}
//Utility methods
GNode generateArrayType(){
GNode qi = GNode.create("QualifiedIdentifier");
qi.add("__rt").add("Array");
GNode type = GNode.create("Type");
type.add(qi);
type.add(null);
return type;
}
GNode generateArrayClassDeclaration(){
GNode methods = generateArrayMethods(); // duplicate
GNode lenField = GNode.create("FieldDeclaration", "length",
GNode.create("Type", GNode.create("PrimitiveType", "int")));
GNode fields = GNode.create("FieldDeclarations", lenField );
GNode ArrayDec =
GNode.create("ClassDeclaration", "rt.Array", null, null, fields, methods);
HashMap<String, GNode> arrFieldMap = new HashMap<String, GNode>();
arrFieldMap.put("length", lenField);
ArrayDec.setProperty("FieldMap", arrFieldMap);
return ArrayDec;
}
GNode generateStringClassDeclaration(){
GNode methods = generateStringMethods();
GNode stringDec = GNode.create("ClassDeclaration", "java.lang.String", null, null, null, methods);
return stringDec;
}
GNode generateClassClassDeclaration(){
GNode methods = generateClassMethods();
GNode objectDec = GNode.create("ClassDeclaration", "java.lang.Class", null, null, null, methods);
return objectDec;
}
GNode generateObjectMethods() {
GNode objectMethods = GNode.create("Methods");
GNode objectType = generateObjectType();
//Hashcode
GNode objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("hashCode");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("int");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//equals
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("equals");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters")).getGeneric(2).addNode(GNode.create("FormalParameter")).getGeneric(0).add("obj").addNode(objectType);
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//getClass
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("getClass");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).getGeneric(0).add("java").add("lang").add("Class");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//toString
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("toString");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).getGeneric(0).add("java").add("lang").add("String");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
return objectMethods;
}
GNode generateArrayMethods() {
GNode objectMethods = GNode.create("Methods");
GNode objectType = generateObjectType();
//Hashcode
GNode objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("hashCode");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("int");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//equals
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("equals");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters")).getGeneric(2).addNode(GNode.create("FormalParameter")).getGeneric(0).add("obj").addNode(objectType);
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//getClass
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("getClass");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).getGeneric(0).add("java").add("lang").add("Class");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//toString
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("toString");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).getGeneric(0).add("java").add("lang").add("String");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
return objectMethods;
}
GNode generateStringMethods(){
GNode objectMethods = GNode.create("Methods");
GNode objectType = generateObjectType();
//Hashcode
GNode objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("hashCode");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("int");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//equals
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("equals");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters")).getGeneric(2).addNode(GNode.create("FormalParameter")).getGeneric(0).add("obj").addNode(objectType);
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//getClass
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("getClass");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).getGeneric(0).add("java").add("lang").add("Class");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//toString
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("toString");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).getGeneric(0).add("java").add("lang").add("String");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("length");
objectMethod.add(GNode.create("ReturnType", GNode.create("PrimitiveType", "int"), null));
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("charAt");
objectMethod.add(GNode.create("ReturnType", GNode.create("PrimitiveType", "char"), null));
objectMethod.addNode(GNode.create("FormalParameters", GNode.create("FormalParameter", "i", GNode.create("Type", GNode.create("PrimitiveType", "int")))));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
return objectMethods;
}
GNode generateClassMethods(){
GNode objectMethods = GNode.create("Methods");
GNode objectType = generateObjectType();
//Hashcode
GNode objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("hashCode");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("int");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//equals
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("equals");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters")).getGeneric(2).addNode(GNode.create("FormalParameter")).getGeneric(0).add("obj").addNode(objectType);
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//getClass
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("getClass");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).getGeneric(0).add("java").add("lang").add("Class");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//toString
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("toString");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).add(null).getGeneric(0).add("java").add("lang").add("String");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//getName
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("getName");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).add(null).getGeneric(0).add("java").add("lang").add("String");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//getParentClass
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("getSuperclass");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("QualifiedIdentifier")).add(null).getGeneric(0).add("java").add("lang").add("Class");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//isPrimitive
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("isPrimitive");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).add(null).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//isArray
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("isArray");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).add(null).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//getComponentType
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("isArray");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).add(null).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters"));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
//isInstance
objectMethod = GNode.create("ImplementedMethodDeclaration");
objectMethod.add("isInstance");
objectMethod.add(GNode.create("ReturnType")).getGeneric(1).add(GNode.create("PrimitiveType")).add(null).getGeneric(0).add("boolean");
objectMethod.addNode(GNode.create("FormalParameters", GNode.create("FormalParameter", "i", GNode.create("Type", GNode.create("QualifiedIdentifier", "java", "lang", "Class"), null))));
//objectMethod.addNode(GNode.create("From")).getGeneric(3).addNode(objectType);
objectMethod.addNode(GNode.create("Block"));
objectMethods.add(objectMethod);
return objectMethods;
}
//Utility methods
/**
* Get name of node.
*
* @param n GNode.
* @return name of node.
*/
String getGNodeName(GNode n){
return n.getString(0);
}
HashMap<String, Integer> GNodeNameToTypeLoc;
HashMap<String, Boolean> GNodeNameToHasUnderscores;
HashMap<String, String> JavaPrimitiveTypeToCPP;
void setupMaps(){
GNodeNameToTypeLoc = new HashMap<String, Integer>();
GNodeNameToTypeLoc.put("Parent", 0);
GNodeNameToTypeLoc.put("FieldDeclaration", 1);
GNodeNameToTypeLoc.put("MethodDeclaration", 1);
GNodeNameToTypeLoc.put("FormalParameter", 1);
GNodeNameToHasUnderscores = new HashMap<String, Boolean>();
GNodeNameToHasUnderscores.put("Parent", true);
GNodeNameToHasUnderscores.put("FieldDeclaration", false);
GNodeNameToHasUnderscores.put("MethodDeclaration", false);
GNodeNameToHasUnderscores.put("FormalParameter", false);
JavaPrimitiveTypeToCPP = new HashMap<String, String>();
JavaPrimitiveTypeToCPP.put("long", "signed int65_t");
JavaPrimitiveTypeToCPP.put("int", "int32_t");
JavaPrimitiveTypeToCPP.put("short", "signed int16_t");
JavaPrimitiveTypeToCPP.put("byte", "signed int8_t");
JavaPrimitiveTypeToCPP.put("float", "float");
JavaPrimitiveTypeToCPP.put("double", "double");
JavaPrimitiveTypeToCPP.put("char", "char");
}
String getGNodeType(GNode n){
GNode typeNode = n.getGeneric(GNodeNameToTypeLoc.get(n.getName()));
GNode identifier = n.getGeneric(0);
if(identifier.getName().equals("PrimitiveType")){
return JavaPrimitiveTypeToCPP.get(identifier.getGeneric(0));
} else if(identifier.getName().equals("QualifiedIdentifier")){
boolean hasUnderscore = GNodeNameToHasUnderscores.get(n.getName());
}
return typeNode.getName();
}
public void printAST(){
Printer p = new Printer(System.out);
p.format(compilationUnit).flush();
}
}