Mangle.java 6.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
8
 * particular file as subject to the "Classpath" exception as provided
9
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
10 11 12 13 14 15 16 17 18 19 20
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
21 22 23
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
24 25 26 27 28
 */


package com.sun.tools.javah;

29 30 31 32 33
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
D
duke 已提交
34 35 36 37 38 39

/**
 * A utility for mangling java identifiers into C names.  Should make
 * this more fine grained and distribute the functionality to the
 * generators.
 *
40
 * <p><b>This is NOT part of any supported API.
41 42 43 44
 * If you write code that depends on this, you do so at your own
 * risk.  This code and its internal interfaces are subject to change
 * or deletion without notice.</b></p>
 *
D
duke 已提交
45 46
 * @author  Sucheta Dambalkar(Revised)
 */
47
public class Mangle {
D
duke 已提交
48 49 50 51

    public static class Type {
        public static final int CLASS            = 1;
        public static final int FIELDSTUB        = 2;
52
        public static final int FIELD            = 3;
D
duke 已提交
53 54 55 56 57 58 59
        public static final int JNI              = 4;
        public static final int SIGNATURE        = 5;
        public static final int METHOD_JDK_1     = 6;
        public static final int METHOD_JNI_SHORT = 7;
        public static final int METHOD_JNI_LONG  = 8;
    };

60 61 62 63 64 65 66
    private Elements elems;
    private Types types;

    Mangle(Elements elems, Types types) {
        this.elems = elems;
        this.types = types;
    }
D
duke 已提交
67

68
    public final String mangle(CharSequence name, int mtype) {
D
duke 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        StringBuffer result = new StringBuffer(100);
        int length = name.length();

        for (int i = 0; i < length; i++) {
            char ch = name.charAt(i);
            if (isalnum(ch)) {
                result.append(ch);
            } else if ((ch == '.') &&
                       mtype == Mangle.Type.CLASS) {
                result.append('_');
            } else if (( ch == '$') &&
                       mtype == Mangle.Type.CLASS) {
                result.append('_');
                result.append('_');
            } else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) {
                result.append('_');
            } else if (ch == '_' && mtype == Mangle.Type.CLASS) {
                result.append('_');
            } else if (mtype == Mangle.Type.JNI) {
                String esc = null;
                if (ch == '_')
                    esc = "_1";
                else if (ch == '.')
                    esc = "_";
                else if (ch == ';')
                    esc = "_2";
                else if (ch == '[')
                    esc = "_3";
                if (esc != null) {
                    result.append(esc);
                } else {
                    result.append(mangleChar(ch));
                }
            } else if (mtype == Mangle.Type.SIGNATURE) {
                if (isprint(ch)) {
                    result.append(ch);
                } else {
                    result.append(mangleChar(ch));
                }
            } else {
                result.append(mangleChar(ch));
            }
        }

        return result.toString();
    }

116
    public String mangleMethod(ExecutableElement method, TypeElement clazz,
117
                                      int mtype) throws TypeSignature.SignatureException {
D
duke 已提交
118 119 120 121
        StringBuffer result = new StringBuffer(100);
        result.append("Java_");

        if (mtype == Mangle.Type.METHOD_JDK_1) {
122
            result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS));
D
duke 已提交
123
            result.append('_');
124
            result.append(mangle(method.getSimpleName(),
D
duke 已提交
125 126 127 128 129 130 131 132
                                 Mangle.Type.FIELD));
            result.append("_stub");
            return result.toString();
        }

        /* JNI */
        result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI));
        result.append('_');
133
        result.append(mangle(method.getSimpleName(),
D
duke 已提交
134 135 136
                             Mangle.Type.JNI));
        if (mtype == Mangle.Type.METHOD_JNI_LONG) {
            result.append("__");
137 138 139
            String typesig = signature(method);
            TypeSignature newTypeSig = new TypeSignature(elems);
            String sig = newTypeSig.getTypeSignature(typesig,  method.getReturnType());
D
duke 已提交
140 141 142 143 144 145 146 147 148
            sig = sig.substring(1);
            sig = sig.substring(0, sig.lastIndexOf(')'));
            sig = sig.replace('/', '.');
            result.append(mangle(sig, Mangle.Type.JNI));
        }

        return result.toString();
    }
    //where
149 150
        private String getInnerQualifiedName(TypeElement clazz) {
            return elems.getBinaryName(clazz).toString();
D
duke 已提交
151 152
        }

153
    public final String mangleChar(char ch) {
D
duke 已提交
154 155 156 157 158 159 160 161 162 163 164
        String s = Integer.toHexString(ch);
        int nzeros = 5 - s.length();
        char[] result = new char[6];
        result[0] = '_';
        for (int i = 1; i <= nzeros; i++)
            result[i] = '0';
        for (int i = nzeros+1, j = 0; i < 6; i++, j++)
            result[i] = s.charAt(j);
        return new String(result);
    }

165 166 167 168 169 170 171 172 173 174 175 176 177
    // Warning: duplicated in Gen
    private String signature(ExecutableElement e) {
        StringBuffer sb = new StringBuffer();
        String sep = "(";
        for (VariableElement p: e.getParameters()) {
            sb.append(sep);
            sb.append(types.erasure(p.asType()).toString());
            sep = ",";
        }
        sb.append(")");
        return sb.toString();
    }

D
duke 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190
    /* Warning: Intentional ASCII operation. */
    private static final boolean isalnum(char ch) {
        return ch <= 0x7f && /* quick test */
            ((ch >= 'A' && ch <= 'Z') ||
             (ch >= 'a' && ch <= 'z') ||
             (ch >= '0' && ch <= '9'));
    }

    /* Warning: Intentional ASCII operation. */
    private static final boolean isprint(char ch) {
        return ch >= 32 && ch <= 126;
    }
}