Mon, 26 Mar 2012 15:27:51 +0100
7151580: Separate DA/DU logic from exception checking logic in Flow.java
Summary: DA/DU analysis and exception checking analysis should live in two separate tree visitors
Reviewed-by: gafter, dlsmith, jjg
duke@1 | 1 | /* |
ohair@798 | 2 | * Copyright (c) 2002, 2010, Oracle and/or its affiliates. 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 |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle 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 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | |
duke@1 | 27 | package com.sun.tools.javah; |
duke@1 | 28 | |
jjg@416 | 29 | import javax.lang.model.element.ExecutableElement; |
jjg@416 | 30 | import javax.lang.model.element.TypeElement; |
jjg@416 | 31 | import javax.lang.model.element.VariableElement; |
jjg@416 | 32 | import javax.lang.model.util.Elements; |
jjg@416 | 33 | import javax.lang.model.util.Types; |
duke@1 | 34 | |
duke@1 | 35 | /** |
duke@1 | 36 | * A utility for mangling java identifiers into C names. Should make |
duke@1 | 37 | * this more fine grained and distribute the functionality to the |
duke@1 | 38 | * generators. |
duke@1 | 39 | * |
jjg@581 | 40 | * <p><b>This is NOT part of any supported API. |
jjg@416 | 41 | * If you write code that depends on this, you do so at your own |
jjg@416 | 42 | * risk. This code and its internal interfaces are subject to change |
jjg@416 | 43 | * or deletion without notice.</b></p> |
jjg@416 | 44 | * |
duke@1 | 45 | * @author Sucheta Dambalkar(Revised) |
duke@1 | 46 | */ |
jjg@416 | 47 | public class Mangle { |
duke@1 | 48 | |
duke@1 | 49 | public static class Type { |
duke@1 | 50 | public static final int CLASS = 1; |
duke@1 | 51 | public static final int FIELDSTUB = 2; |
jjg@416 | 52 | public static final int FIELD = 3; |
duke@1 | 53 | public static final int JNI = 4; |
duke@1 | 54 | public static final int SIGNATURE = 5; |
duke@1 | 55 | public static final int METHOD_JDK_1 = 6; |
duke@1 | 56 | public static final int METHOD_JNI_SHORT = 7; |
duke@1 | 57 | public static final int METHOD_JNI_LONG = 8; |
duke@1 | 58 | }; |
duke@1 | 59 | |
jjg@416 | 60 | private Elements elems; |
jjg@416 | 61 | private Types types; |
duke@1 | 62 | |
jjg@416 | 63 | Mangle(Elements elems, Types types) { |
jjg@416 | 64 | this.elems = elems; |
jjg@416 | 65 | this.types = types; |
jjg@416 | 66 | } |
jjg@416 | 67 | |
jjg@416 | 68 | public final String mangle(CharSequence name, int mtype) { |
duke@1 | 69 | StringBuffer result = new StringBuffer(100); |
duke@1 | 70 | int length = name.length(); |
duke@1 | 71 | |
duke@1 | 72 | for (int i = 0; i < length; i++) { |
duke@1 | 73 | char ch = name.charAt(i); |
duke@1 | 74 | if (isalnum(ch)) { |
duke@1 | 75 | result.append(ch); |
duke@1 | 76 | } else if ((ch == '.') && |
duke@1 | 77 | mtype == Mangle.Type.CLASS) { |
duke@1 | 78 | result.append('_'); |
duke@1 | 79 | } else if (( ch == '$') && |
duke@1 | 80 | mtype == Mangle.Type.CLASS) { |
duke@1 | 81 | result.append('_'); |
duke@1 | 82 | result.append('_'); |
duke@1 | 83 | } else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) { |
duke@1 | 84 | result.append('_'); |
duke@1 | 85 | } else if (ch == '_' && mtype == Mangle.Type.CLASS) { |
duke@1 | 86 | result.append('_'); |
duke@1 | 87 | } else if (mtype == Mangle.Type.JNI) { |
duke@1 | 88 | String esc = null; |
duke@1 | 89 | if (ch == '_') |
duke@1 | 90 | esc = "_1"; |
duke@1 | 91 | else if (ch == '.') |
duke@1 | 92 | esc = "_"; |
duke@1 | 93 | else if (ch == ';') |
duke@1 | 94 | esc = "_2"; |
duke@1 | 95 | else if (ch == '[') |
duke@1 | 96 | esc = "_3"; |
duke@1 | 97 | if (esc != null) { |
duke@1 | 98 | result.append(esc); |
duke@1 | 99 | } else { |
duke@1 | 100 | result.append(mangleChar(ch)); |
duke@1 | 101 | } |
duke@1 | 102 | } else if (mtype == Mangle.Type.SIGNATURE) { |
duke@1 | 103 | if (isprint(ch)) { |
duke@1 | 104 | result.append(ch); |
duke@1 | 105 | } else { |
duke@1 | 106 | result.append(mangleChar(ch)); |
duke@1 | 107 | } |
duke@1 | 108 | } else { |
duke@1 | 109 | result.append(mangleChar(ch)); |
duke@1 | 110 | } |
duke@1 | 111 | } |
duke@1 | 112 | |
duke@1 | 113 | return result.toString(); |
duke@1 | 114 | } |
duke@1 | 115 | |
jjg@416 | 116 | public String mangleMethod(ExecutableElement method, TypeElement clazz, |
jjg@712 | 117 | int mtype) throws TypeSignature.SignatureException { |
duke@1 | 118 | StringBuffer result = new StringBuffer(100); |
duke@1 | 119 | result.append("Java_"); |
duke@1 | 120 | |
duke@1 | 121 | if (mtype == Mangle.Type.METHOD_JDK_1) { |
jjg@416 | 122 | result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS)); |
duke@1 | 123 | result.append('_'); |
jjg@416 | 124 | result.append(mangle(method.getSimpleName(), |
duke@1 | 125 | Mangle.Type.FIELD)); |
duke@1 | 126 | result.append("_stub"); |
duke@1 | 127 | return result.toString(); |
duke@1 | 128 | } |
duke@1 | 129 | |
duke@1 | 130 | /* JNI */ |
duke@1 | 131 | result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI)); |
duke@1 | 132 | result.append('_'); |
jjg@416 | 133 | result.append(mangle(method.getSimpleName(), |
duke@1 | 134 | Mangle.Type.JNI)); |
duke@1 | 135 | if (mtype == Mangle.Type.METHOD_JNI_LONG) { |
duke@1 | 136 | result.append("__"); |
jjg@416 | 137 | String typesig = signature(method); |
jjg@416 | 138 | TypeSignature newTypeSig = new TypeSignature(elems); |
jjg@416 | 139 | String sig = newTypeSig.getTypeSignature(typesig, method.getReturnType()); |
duke@1 | 140 | sig = sig.substring(1); |
duke@1 | 141 | sig = sig.substring(0, sig.lastIndexOf(')')); |
duke@1 | 142 | sig = sig.replace('/', '.'); |
duke@1 | 143 | result.append(mangle(sig, Mangle.Type.JNI)); |
duke@1 | 144 | } |
duke@1 | 145 | |
duke@1 | 146 | return result.toString(); |
duke@1 | 147 | } |
duke@1 | 148 | //where |
jjg@416 | 149 | private String getInnerQualifiedName(TypeElement clazz) { |
jjg@416 | 150 | return elems.getBinaryName(clazz).toString(); |
duke@1 | 151 | } |
duke@1 | 152 | |
jjg@416 | 153 | public final String mangleChar(char ch) { |
duke@1 | 154 | String s = Integer.toHexString(ch); |
duke@1 | 155 | int nzeros = 5 - s.length(); |
duke@1 | 156 | char[] result = new char[6]; |
duke@1 | 157 | result[0] = '_'; |
duke@1 | 158 | for (int i = 1; i <= nzeros; i++) |
duke@1 | 159 | result[i] = '0'; |
duke@1 | 160 | for (int i = nzeros+1, j = 0; i < 6; i++, j++) |
duke@1 | 161 | result[i] = s.charAt(j); |
duke@1 | 162 | return new String(result); |
duke@1 | 163 | } |
duke@1 | 164 | |
jjg@416 | 165 | // Warning: duplicated in Gen |
jjg@416 | 166 | private String signature(ExecutableElement e) { |
jjg@416 | 167 | StringBuffer sb = new StringBuffer(); |
jjg@416 | 168 | String sep = "("; |
jjg@416 | 169 | for (VariableElement p: e.getParameters()) { |
jjg@416 | 170 | sb.append(sep); |
jjg@416 | 171 | sb.append(types.erasure(p.asType()).toString()); |
jjg@416 | 172 | sep = ","; |
jjg@416 | 173 | } |
jjg@416 | 174 | sb.append(")"); |
jjg@416 | 175 | return sb.toString(); |
jjg@416 | 176 | } |
jjg@416 | 177 | |
duke@1 | 178 | /* Warning: Intentional ASCII operation. */ |
duke@1 | 179 | private static final boolean isalnum(char ch) { |
duke@1 | 180 | return ch <= 0x7f && /* quick test */ |
duke@1 | 181 | ((ch >= 'A' && ch <= 'Z') || |
duke@1 | 182 | (ch >= 'a' && ch <= 'z') || |
duke@1 | 183 | (ch >= '0' && ch <= '9')); |
duke@1 | 184 | } |
duke@1 | 185 | |
duke@1 | 186 | /* Warning: Intentional ASCII operation. */ |
duke@1 | 187 | private static final boolean isprint(char ch) { |
duke@1 | 188 | return ch >= 32 && ch <= 126; |
duke@1 | 189 | } |
duke@1 | 190 | } |