0N/A/*
797N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage com.sun.tools.javah;
0N/A
415N/Aimport javax.lang.model.element.ExecutableElement;
415N/Aimport javax.lang.model.element.TypeElement;
415N/Aimport javax.lang.model.element.VariableElement;
415N/Aimport javax.lang.model.util.Elements;
415N/Aimport javax.lang.model.util.Types;
0N/A
0N/A/**
0N/A * A utility for mangling java identifiers into C names. Should make
0N/A * this more fine grained and distribute the functionality to the
0N/A * generators.
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
415N/A * If you write code that depends on this, you do so at your own
415N/A * risk. This code and its internal interfaces are subject to change
415N/A * or deletion without notice.</b></p>
415N/A *
0N/A * @author Sucheta Dambalkar(Revised)
0N/A */
415N/Apublic class Mangle {
0N/A
0N/A public static class Type {
0N/A public static final int CLASS = 1;
0N/A public static final int FIELDSTUB = 2;
415N/A public static final int FIELD = 3;
0N/A public static final int JNI = 4;
0N/A public static final int SIGNATURE = 5;
0N/A public static final int METHOD_JDK_1 = 6;
0N/A public static final int METHOD_JNI_SHORT = 7;
0N/A public static final int METHOD_JNI_LONG = 8;
0N/A };
0N/A
415N/A private Elements elems;
415N/A private Types types;
0N/A
415N/A Mangle(Elements elems, Types types) {
415N/A this.elems = elems;
415N/A this.types = types;
415N/A }
415N/A
415N/A public final String mangle(CharSequence name, int mtype) {
0N/A StringBuffer result = new StringBuffer(100);
0N/A int length = name.length();
0N/A
0N/A for (int i = 0; i < length; i++) {
0N/A char ch = name.charAt(i);
0N/A if (isalnum(ch)) {
0N/A result.append(ch);
0N/A } else if ((ch == '.') &&
0N/A mtype == Mangle.Type.CLASS) {
0N/A result.append('_');
0N/A } else if (( ch == '$') &&
0N/A mtype == Mangle.Type.CLASS) {
0N/A result.append('_');
0N/A result.append('_');
0N/A } else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) {
0N/A result.append('_');
0N/A } else if (ch == '_' && mtype == Mangle.Type.CLASS) {
0N/A result.append('_');
0N/A } else if (mtype == Mangle.Type.JNI) {
0N/A String esc = null;
0N/A if (ch == '_')
0N/A esc = "_1";
0N/A else if (ch == '.')
0N/A esc = "_";
0N/A else if (ch == ';')
0N/A esc = "_2";
0N/A else if (ch == '[')
0N/A esc = "_3";
0N/A if (esc != null) {
0N/A result.append(esc);
0N/A } else {
0N/A result.append(mangleChar(ch));
0N/A }
0N/A } else if (mtype == Mangle.Type.SIGNATURE) {
0N/A if (isprint(ch)) {
0N/A result.append(ch);
0N/A } else {
0N/A result.append(mangleChar(ch));
0N/A }
0N/A } else {
0N/A result.append(mangleChar(ch));
0N/A }
0N/A }
0N/A
0N/A return result.toString();
0N/A }
0N/A
415N/A public String mangleMethod(ExecutableElement method, TypeElement clazz,
711N/A int mtype) throws TypeSignature.SignatureException {
0N/A StringBuffer result = new StringBuffer(100);
0N/A result.append("Java_");
0N/A
0N/A if (mtype == Mangle.Type.METHOD_JDK_1) {
415N/A result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS));
0N/A result.append('_');
415N/A result.append(mangle(method.getSimpleName(),
0N/A Mangle.Type.FIELD));
0N/A result.append("_stub");
0N/A return result.toString();
0N/A }
0N/A
0N/A /* JNI */
0N/A result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI));
0N/A result.append('_');
415N/A result.append(mangle(method.getSimpleName(),
0N/A Mangle.Type.JNI));
0N/A if (mtype == Mangle.Type.METHOD_JNI_LONG) {
0N/A result.append("__");
415N/A String typesig = signature(method);
415N/A TypeSignature newTypeSig = new TypeSignature(elems);
415N/A String sig = newTypeSig.getTypeSignature(typesig, method.getReturnType());
0N/A sig = sig.substring(1);
0N/A sig = sig.substring(0, sig.lastIndexOf(')'));
0N/A sig = sig.replace('/', '.');
0N/A result.append(mangle(sig, Mangle.Type.JNI));
0N/A }
0N/A
0N/A return result.toString();
0N/A }
0N/A //where
415N/A private String getInnerQualifiedName(TypeElement clazz) {
415N/A return elems.getBinaryName(clazz).toString();
0N/A }
0N/A
415N/A public final String mangleChar(char ch) {
0N/A String s = Integer.toHexString(ch);
0N/A int nzeros = 5 - s.length();
0N/A char[] result = new char[6];
0N/A result[0] = '_';
0N/A for (int i = 1; i <= nzeros; i++)
0N/A result[i] = '0';
0N/A for (int i = nzeros+1, j = 0; i < 6; i++, j++)
0N/A result[i] = s.charAt(j);
0N/A return new String(result);
0N/A }
0N/A
415N/A // Warning: duplicated in Gen
415N/A private String signature(ExecutableElement e) {
415N/A StringBuffer sb = new StringBuffer();
415N/A String sep = "(";
415N/A for (VariableElement p: e.getParameters()) {
415N/A sb.append(sep);
415N/A sb.append(types.erasure(p.asType()).toString());
415N/A sep = ",";
415N/A }
415N/A sb.append(")");
415N/A return sb.toString();
415N/A }
415N/A
0N/A /* Warning: Intentional ASCII operation. */
0N/A private static final boolean isalnum(char ch) {
0N/A return ch <= 0x7f && /* quick test */
0N/A ((ch >= 'A' && ch <= 'Z') ||
0N/A (ch >= 'a' && ch <= 'z') ||
0N/A (ch >= '0' && ch <= '9'));
0N/A }
0N/A
0N/A /* Warning: Intentional ASCII operation. */
0N/A private static final boolean isprint(char ch) {
0N/A return ch >= 32 && ch <= 126;
0N/A }
0N/A}