Wed, 05 Aug 2009 07:43:50 -0700
6868553: 6867671 breaks some tests
Reviewed-by: mcimadamore
duke@1 | 1 | /* |
duke@1 | 2 | * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. Sun designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
duke@1 | 9 | * by Sun in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
duke@1 | 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
duke@1 | 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
duke@1 | 23 | * have any questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | |
duke@1 | 27 | package com.sun.tools.javah; |
duke@1 | 28 | |
duke@1 | 29 | import com.sun.javadoc.*; |
duke@1 | 30 | |
duke@1 | 31 | /** |
duke@1 | 32 | * A utility for mangling java identifiers into C names. Should make |
duke@1 | 33 | * this more fine grained and distribute the functionality to the |
duke@1 | 34 | * generators. |
duke@1 | 35 | * |
duke@1 | 36 | * @author Sucheta Dambalkar(Revised) |
duke@1 | 37 | */ |
duke@1 | 38 | class Mangle { |
duke@1 | 39 | |
duke@1 | 40 | public static class Type { |
duke@1 | 41 | |
duke@1 | 42 | public static final int CLASS = 1; |
duke@1 | 43 | public static final int FIELDSTUB = 2; |
duke@1 | 44 | public static final int FIELD = 3; |
duke@1 | 45 | public static final int JNI = 4; |
duke@1 | 46 | public static final int SIGNATURE = 5; |
duke@1 | 47 | public static final int METHOD_JDK_1 = 6; |
duke@1 | 48 | public static final int METHOD_JNI_SHORT = 7; |
duke@1 | 49 | public static final int METHOD_JNI_LONG = 8; |
duke@1 | 50 | }; |
duke@1 | 51 | |
duke@1 | 52 | |
duke@1 | 53 | public static final String mangle(String name, int mtype) { |
duke@1 | 54 | StringBuffer result = new StringBuffer(100); |
duke@1 | 55 | int length = name.length(); |
duke@1 | 56 | |
duke@1 | 57 | for (int i = 0; i < length; i++) { |
duke@1 | 58 | char ch = name.charAt(i); |
duke@1 | 59 | if (isalnum(ch)) { |
duke@1 | 60 | result.append(ch); |
duke@1 | 61 | } else if ((ch == '.') && |
duke@1 | 62 | mtype == Mangle.Type.CLASS) { |
duke@1 | 63 | result.append('_'); |
duke@1 | 64 | } else if (( ch == '$') && |
duke@1 | 65 | mtype == Mangle.Type.CLASS) { |
duke@1 | 66 | result.append('_'); |
duke@1 | 67 | result.append('_'); |
duke@1 | 68 | } else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) { |
duke@1 | 69 | result.append('_'); |
duke@1 | 70 | } else if (ch == '_' && mtype == Mangle.Type.CLASS) { |
duke@1 | 71 | result.append('_'); |
duke@1 | 72 | } else if (mtype == Mangle.Type.JNI) { |
duke@1 | 73 | String esc = null; |
duke@1 | 74 | if (ch == '_') |
duke@1 | 75 | esc = "_1"; |
duke@1 | 76 | else if (ch == '.') |
duke@1 | 77 | esc = "_"; |
duke@1 | 78 | else if (ch == ';') |
duke@1 | 79 | esc = "_2"; |
duke@1 | 80 | else if (ch == '[') |
duke@1 | 81 | esc = "_3"; |
duke@1 | 82 | if (esc != null) { |
duke@1 | 83 | result.append(esc); |
duke@1 | 84 | } else { |
duke@1 | 85 | result.append(mangleChar(ch)); |
duke@1 | 86 | } |
duke@1 | 87 | } else if (mtype == Mangle.Type.SIGNATURE) { |
duke@1 | 88 | if (isprint(ch)) { |
duke@1 | 89 | result.append(ch); |
duke@1 | 90 | } else { |
duke@1 | 91 | result.append(mangleChar(ch)); |
duke@1 | 92 | } |
duke@1 | 93 | } else { |
duke@1 | 94 | result.append(mangleChar(ch)); |
duke@1 | 95 | } |
duke@1 | 96 | } |
duke@1 | 97 | |
duke@1 | 98 | return result.toString(); |
duke@1 | 99 | } |
duke@1 | 100 | |
duke@1 | 101 | public static String mangleMethod(MethodDoc method, RootDoc root, ClassDoc clazz, |
duke@1 | 102 | int mtype) { |
duke@1 | 103 | StringBuffer result = new StringBuffer(100); |
duke@1 | 104 | result.append("Java_"); |
duke@1 | 105 | |
duke@1 | 106 | if (mtype == Mangle.Type.METHOD_JDK_1) { |
duke@1 | 107 | result.append(mangle(clazz.qualifiedName(), Mangle.Type.CLASS)); |
duke@1 | 108 | result.append('_'); |
duke@1 | 109 | result.append(mangle(method.name(), |
duke@1 | 110 | Mangle.Type.FIELD)); |
duke@1 | 111 | result.append("_stub"); |
duke@1 | 112 | return result.toString(); |
duke@1 | 113 | } |
duke@1 | 114 | |
duke@1 | 115 | /* JNI */ |
duke@1 | 116 | result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI)); |
duke@1 | 117 | result.append('_'); |
duke@1 | 118 | result.append(mangle(method.name(), |
duke@1 | 119 | Mangle.Type.JNI)); |
duke@1 | 120 | if (mtype == Mangle.Type.METHOD_JNI_LONG) { |
duke@1 | 121 | result.append("__"); |
duke@1 | 122 | String typesig = method.signature(); |
duke@1 | 123 | TypeSignature newTypeSig = new TypeSignature(root); |
duke@1 | 124 | String sig = newTypeSig.getTypeSignature(typesig, method.returnType()); |
duke@1 | 125 | sig = sig.substring(1); |
duke@1 | 126 | sig = sig.substring(0, sig.lastIndexOf(')')); |
duke@1 | 127 | sig = sig.replace('/', '.'); |
duke@1 | 128 | result.append(mangle(sig, Mangle.Type.JNI)); |
duke@1 | 129 | } |
duke@1 | 130 | |
duke@1 | 131 | return result.toString(); |
duke@1 | 132 | } |
duke@1 | 133 | //where |
duke@1 | 134 | private static String getInnerQualifiedName(ClassDoc clazz) { |
duke@1 | 135 | ClassDoc encl = clazz.containingClass(); |
duke@1 | 136 | if (encl == null) |
duke@1 | 137 | return clazz.qualifiedName(); |
duke@1 | 138 | else |
duke@1 | 139 | return getInnerQualifiedName(encl) + '$' + clazz.simpleTypeName(); |
duke@1 | 140 | } |
duke@1 | 141 | |
duke@1 | 142 | public static final String mangleChar(char ch) { |
duke@1 | 143 | String s = Integer.toHexString(ch); |
duke@1 | 144 | int nzeros = 5 - s.length(); |
duke@1 | 145 | char[] result = new char[6]; |
duke@1 | 146 | result[0] = '_'; |
duke@1 | 147 | for (int i = 1; i <= nzeros; i++) |
duke@1 | 148 | result[i] = '0'; |
duke@1 | 149 | for (int i = nzeros+1, j = 0; i < 6; i++, j++) |
duke@1 | 150 | result[i] = s.charAt(j); |
duke@1 | 151 | return new String(result); |
duke@1 | 152 | } |
duke@1 | 153 | |
duke@1 | 154 | /* Warning: Intentional ASCII operation. */ |
duke@1 | 155 | private static final boolean isalnum(char ch) { |
duke@1 | 156 | return ch <= 0x7f && /* quick test */ |
duke@1 | 157 | ((ch >= 'A' && ch <= 'Z') || |
duke@1 | 158 | (ch >= 'a' && ch <= 'z') || |
duke@1 | 159 | (ch >= '0' && ch <= '9')); |
duke@1 | 160 | } |
duke@1 | 161 | |
duke@1 | 162 | /* Warning: Intentional ASCII operation. */ |
duke@1 | 163 | private static final boolean isprint(char ch) { |
duke@1 | 164 | return ch >= 32 && ch <= 126; |
duke@1 | 165 | } |
duke@1 | 166 | } |