-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayTemplatePrinter.java
More file actions
82 lines (65 loc) · 2.33 KB
/
ArrayTemplatePrinter.java
File metadata and controls
82 lines (65 loc) · 2.33 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
package qimpp;
import java.util.Iterator;
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;
import java.util.HashMap;
public class ArrayTemplatePrinter extends Visitor {
Printer printer;
/**
* Constructor
* @param printer the printer for the implementation file
*/
ArrayTemplatePrinter(Printer printer) {
this.printer = printer;
printer.register(this);
}
//TODO: Figure out a better way to do this or centralize this method
/**
* Convert a non-underscored name to an underscored name
* @param cppQualifiedName the fully-qualified classname delimited by ::
*/
static String getUnderscoredName(String cppQualifiedName) {
String[] nameParts = cppQualifiedName.split("::");
StringBuilder underscoredName = new StringBuilder();
for (int i = 0; i < nameParts.length - 1; i++) {
underscoredName.append(nameParts[i]);
underscoredName.append("::");
}
underscoredName.append("__");
underscoredName.append(nameParts[nameParts.length - 1]);
return underscoredName.toString();
}
/** Visit the class declaration, print the relevant info and go no deeper */
public void visitClassDeclaration( GNode n ) {
String name = n.getString(0);
name = name.replaceAll("\\.", "::");
GNode parentType = n.getGeneric(1).getGeneric(0).getGeneric(0);
String parent = Disambiguator.getDotDelimitedName(parentType);
parent = parent.replaceAll("\\.", "::");
printer.pln().p("namespace __rt{").pln().incr();
printer.indent().pln("template<>");
printer.indent().p("java::lang::Class").p(" __rt::Array< ")
.p(name).p(" >::__class() {").pln();
printer.incr();
printer.indent().p("static java::lang::Class k =").pln()
.indent().p("new java::lang::__Class(literal(\"[L")
.p(name.replace("::", ".")).p(";\"),").pln()
.indent().p("Array< ").p(parent).p(" >::__class(),")
.p(getUnderscoredName(name)).p("::__class());")
.pln()
.indent().p("return k;").pln()
.indent().p("}").pln();
printer.decr().decr().p("}").pln();
printer.flush();
}
/** Visit the specified Node. */
public void visit(Node n) {
for (Object o : n) if (o instanceof Node) dispatch((Node)o);
}
}