Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javah/Mangle.java
38899 views
/*1* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package com.sun.tools.javah;2728import javax.lang.model.element.ExecutableElement;29import javax.lang.model.element.TypeElement;30import javax.lang.model.element.VariableElement;31import javax.lang.model.util.Elements;32import javax.lang.model.util.Types;3334/**35* A utility for mangling java identifiers into C names. Should make36* this more fine grained and distribute the functionality to the37* generators.38*39* <p><b>This is NOT part of any supported API.40* If you write code that depends on this, you do so at your own41* risk. This code and its internal interfaces are subject to change42* or deletion without notice.</b></p>43*44* @author Sucheta Dambalkar(Revised)45*/46public class Mangle {4748public static class Type {49public static final int CLASS = 1;50public static final int FIELDSTUB = 2;51public static final int FIELD = 3;52public static final int JNI = 4;53public static final int SIGNATURE = 5;54public static final int METHOD_JDK_1 = 6;55public static final int METHOD_JNI_SHORT = 7;56public static final int METHOD_JNI_LONG = 8;57};5859private Elements elems;60private Types types;6162Mangle(Elements elems, Types types) {63this.elems = elems;64this.types = types;65}6667public final String mangle(CharSequence name, int mtype) {68StringBuilder result = new StringBuilder(100);69int length = name.length();7071for (int i = 0; i < length; i++) {72char ch = name.charAt(i);73if (isalnum(ch)) {74result.append(ch);75} else if ((ch == '.') &&76mtype == Mangle.Type.CLASS) {77result.append('_');78} else if (( ch == '$') &&79mtype == Mangle.Type.CLASS) {80result.append('_');81result.append('_');82} else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) {83result.append('_');84} else if (ch == '_' && mtype == Mangle.Type.CLASS) {85result.append('_');86} else if (mtype == Mangle.Type.JNI) {87String esc = null;88if (ch == '_')89esc = "_1";90else if (ch == '.')91esc = "_";92else if (ch == ';')93esc = "_2";94else if (ch == '[')95esc = "_3";96if (esc != null) {97result.append(esc);98} else {99result.append(mangleChar(ch));100}101} else if (mtype == Mangle.Type.SIGNATURE) {102if (isprint(ch)) {103result.append(ch);104} else {105result.append(mangleChar(ch));106}107} else {108result.append(mangleChar(ch));109}110}111112return result.toString();113}114115public String mangleMethod(ExecutableElement method, TypeElement clazz,116int mtype) throws TypeSignature.SignatureException {117StringBuilder result = new StringBuilder(100);118result.append("Java_");119120if (mtype == Mangle.Type.METHOD_JDK_1) {121result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS));122result.append('_');123result.append(mangle(method.getSimpleName(),124Mangle.Type.FIELD));125result.append("_stub");126return result.toString();127}128129/* JNI */130result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI));131result.append('_');132result.append(mangle(method.getSimpleName(),133Mangle.Type.JNI));134if (mtype == Mangle.Type.METHOD_JNI_LONG) {135result.append("__");136String typesig = signature(method);137TypeSignature newTypeSig = new TypeSignature(elems);138String sig = newTypeSig.getTypeSignature(typesig, method.getReturnType());139sig = sig.substring(1);140sig = sig.substring(0, sig.lastIndexOf(')'));141sig = sig.replace('/', '.');142result.append(mangle(sig, Mangle.Type.JNI));143}144145return result.toString();146}147//where148private String getInnerQualifiedName(TypeElement clazz) {149return elems.getBinaryName(clazz).toString();150}151152public final String mangleChar(char ch) {153String s = Integer.toHexString(ch);154int nzeros = 5 - s.length();155char[] result = new char[6];156result[0] = '_';157for (int i = 1; i <= nzeros; i++)158result[i] = '0';159for (int i = nzeros+1, j = 0; i < 6; i++, j++)160result[i] = s.charAt(j);161return new String(result);162}163164// Warning: duplicated in Gen165private String signature(ExecutableElement e) {166StringBuilder sb = new StringBuilder();167String sep = "(";168for (VariableElement p: e.getParameters()) {169sb.append(sep);170sb.append(types.erasure(p.asType()).toString());171sep = ",";172}173sb.append(")");174return sb.toString();175}176177/* Warning: Intentional ASCII operation. */178private static final boolean isalnum(char ch) {179return ch <= 0x7f && /* quick test */180((ch >= 'A' && ch <= 'Z') ||181(ch >= 'a' && ch <= 'z') ||182(ch >= '0' && ch <= '9'));183}184185/* Warning: Intentional ASCII operation. */186private static final boolean isprint(char ch) {187return ch >= 32 && ch <= 126;188}189}190191192