author | vasya |
Mon, 14 Dec 2015 20:18:19 +0100 | |
changeset 34752 | 9c262a013456 |
parent 25874 | 83c19f00452c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14263 | 2 |
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javah; |
|
27 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
28 |
import javax.lang.model.element.ExecutableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
29 |
import javax.lang.model.element.TypeElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
30 |
import javax.lang.model.element.VariableElement; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
31 |
import javax.lang.model.util.Elements; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
32 |
import javax.lang.model.util.Types; |
10 | 33 |
|
34 |
/** |
|
35 |
* A utility for mangling java identifiers into C names. Should make |
|
36 |
* this more fine grained and distribute the functionality to the |
|
37 |
* generators. |
|
38 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
39 |
* <p><b>This is NOT part of any supported API. |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
40 |
* If you write code that depends on this, you do so at your own |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
41 |
* risk. This code and its internal interfaces are subject to change |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
42 |
* or deletion without notice.</b></p> |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
43 |
* |
10 | 44 |
* @author Sucheta Dambalkar(Revised) |
45 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
46 |
public class Mangle { |
10 | 47 |
|
48 |
public static class Type { |
|
49 |
public static final int CLASS = 1; |
|
50 |
public static final int FIELDSTUB = 2; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
51 |
public static final int FIELD = 3; |
10 | 52 |
public static final int JNI = 4; |
53 |
public static final int SIGNATURE = 5; |
|
54 |
public static final int METHOD_JDK_1 = 6; |
|
55 |
public static final int METHOD_JNI_SHORT = 7; |
|
56 |
public static final int METHOD_JNI_LONG = 8; |
|
22163 | 57 |
} |
10 | 58 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
59 |
private Elements elems; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
60 |
private Types types; |
10 | 61 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
62 |
Mangle(Elements elems, Types types) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
63 |
this.elems = elems; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
64 |
this.types = types; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
65 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
66 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
67 |
public final String mangle(CharSequence name, int mtype) { |
14263 | 68 |
StringBuilder result = new StringBuilder(100); |
10 | 69 |
int length = name.length(); |
70 |
||
71 |
for (int i = 0; i < length; i++) { |
|
72 |
char ch = name.charAt(i); |
|
73 |
if (isalnum(ch)) { |
|
74 |
result.append(ch); |
|
75 |
} else if ((ch == '.') && |
|
76 |
mtype == Mangle.Type.CLASS) { |
|
77 |
result.append('_'); |
|
78 |
} else if (( ch == '$') && |
|
79 |
mtype == Mangle.Type.CLASS) { |
|
80 |
result.append('_'); |
|
81 |
result.append('_'); |
|
82 |
} else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) { |
|
83 |
result.append('_'); |
|
84 |
} else if (ch == '_' && mtype == Mangle.Type.CLASS) { |
|
85 |
result.append('_'); |
|
86 |
} else if (mtype == Mangle.Type.JNI) { |
|
87 |
String esc = null; |
|
88 |
if (ch == '_') |
|
89 |
esc = "_1"; |
|
90 |
else if (ch == '.') |
|
91 |
esc = "_"; |
|
92 |
else if (ch == ';') |
|
93 |
esc = "_2"; |
|
94 |
else if (ch == '[') |
|
95 |
esc = "_3"; |
|
96 |
if (esc != null) { |
|
97 |
result.append(esc); |
|
98 |
} else { |
|
99 |
result.append(mangleChar(ch)); |
|
100 |
} |
|
101 |
} else if (mtype == Mangle.Type.SIGNATURE) { |
|
102 |
if (isprint(ch)) { |
|
103 |
result.append(ch); |
|
104 |
} else { |
|
105 |
result.append(mangleChar(ch)); |
|
106 |
} |
|
107 |
} else { |
|
108 |
result.append(mangleChar(ch)); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
return result.toString(); |
|
113 |
} |
|
114 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
115 |
public String mangleMethod(ExecutableElement method, TypeElement clazz, |
6930 | 116 |
int mtype) throws TypeSignature.SignatureException { |
14263 | 117 |
StringBuilder result = new StringBuilder(100); |
10 | 118 |
result.append("Java_"); |
119 |
||
120 |
if (mtype == Mangle.Type.METHOD_JDK_1) { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
121 |
result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS)); |
10 | 122 |
result.append('_'); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
123 |
result.append(mangle(method.getSimpleName(), |
10 | 124 |
Mangle.Type.FIELD)); |
125 |
result.append("_stub"); |
|
126 |
return result.toString(); |
|
127 |
} |
|
128 |
||
129 |
/* JNI */ |
|
130 |
result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI)); |
|
131 |
result.append('_'); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
132 |
result.append(mangle(method.getSimpleName(), |
10 | 133 |
Mangle.Type.JNI)); |
134 |
if (mtype == Mangle.Type.METHOD_JNI_LONG) { |
|
135 |
result.append("__"); |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
136 |
String typesig = signature(method); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
137 |
TypeSignature newTypeSig = new TypeSignature(elems); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
138 |
String sig = newTypeSig.getTypeSignature(typesig, method.getReturnType()); |
10 | 139 |
sig = sig.substring(1); |
140 |
sig = sig.substring(0, sig.lastIndexOf(')')); |
|
141 |
sig = sig.replace('/', '.'); |
|
142 |
result.append(mangle(sig, Mangle.Type.JNI)); |
|
143 |
} |
|
144 |
||
145 |
return result.toString(); |
|
146 |
} |
|
147 |
//where |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
148 |
private String getInnerQualifiedName(TypeElement clazz) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
149 |
return elems.getBinaryName(clazz).toString(); |
10 | 150 |
} |
151 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
152 |
public final String mangleChar(char ch) { |
10 | 153 |
String s = Integer.toHexString(ch); |
154 |
int nzeros = 5 - s.length(); |
|
155 |
char[] result = new char[6]; |
|
156 |
result[0] = '_'; |
|
157 |
for (int i = 1; i <= nzeros; i++) |
|
158 |
result[i] = '0'; |
|
159 |
for (int i = nzeros+1, j = 0; i < 6; i++, j++) |
|
160 |
result[i] = s.charAt(j); |
|
161 |
return new String(result); |
|
162 |
} |
|
163 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
164 |
// Warning: duplicated in Gen |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
165 |
private String signature(ExecutableElement e) { |
14263 | 166 |
StringBuilder sb = new StringBuilder(); |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
167 |
String sep = "("; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
168 |
for (VariableElement p: e.getParameters()) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
169 |
sb.append(sep); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
170 |
sb.append(types.erasure(p.asType()).toString()); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
171 |
sep = ","; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
172 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
173 |
sb.append(")"); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
174 |
return sb.toString(); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
175 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
176 |
|
10 | 177 |
/* Warning: Intentional ASCII operation. */ |
22163 | 178 |
private static boolean isalnum(char ch) { |
10 | 179 |
return ch <= 0x7f && /* quick test */ |
180 |
((ch >= 'A' && ch <= 'Z') || |
|
181 |
(ch >= 'a' && ch <= 'z') || |
|
182 |
(ch >= '0' && ch <= '9')); |
|
183 |
} |
|
184 |
||
185 |
/* Warning: Intentional ASCII operation. */ |
|
22163 | 186 |
private static boolean isprint(char ch) { |
10 | 187 |
return ch >= 32 && ch <= 126; |
188 |
} |
|
189 |
} |