langtools/src/share/classes/com/sun/tools/javah/LLNI.java
author mcimadamore
Tue, 13 Jan 2009 13:27:14 +0000
changeset 1789 7ac8c0815000
parent 10 06bc494ca11e
child 3996 dc676a9093b3
permissions -rw-r--r--
6765045: Remove rawtypes warnings from langtools Summary: Removed all occurrences of rawtypes warnings from langtools Reviewed-by: jjg, bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2002-2005 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
package com.sun.tools.javah;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.io.File;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.io.PrintWriter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Hashtable;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 /*
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
  * @author  Sucheta Dambalkar(Revised)
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
  */
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
public class LLNI extends Gen {
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    protected final char  pathChar = File.separatorChar;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    protected final char  innerDelim = '$';     /* For inner classes */
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    42
    protected Hashtable<Object, Object>   doneHandleTypes;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    MemberDoc []fields;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    MemberDoc [] methods;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    private boolean       doubleAlign;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    private int           padFieldNum = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    LLNI(boolean doubleAlign, RootDoc root) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        super(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        this.doubleAlign = doubleAlign;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    protected String getIncludes() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        return "";
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    protected void write(OutputStream o, ClassDoc clazz)
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        String cname     = mangleClassName(clazz.qualifiedName());
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        PrintWriter pw   = wrapWriter(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        fields = clazz.fields();
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        methods = clazz.methods();
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        generateDeclsForClass(pw, clazz, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    protected void generateDeclsForClass(PrintWriter pw,
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                                         ClassDoc clazz, String cname)
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        throws ClassNotFoundException {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 10
diff changeset
    71
        doneHandleTypes  = new Hashtable<Object, Object>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        /* The following handle types are predefined in "typedefs.h". Suppress
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
           inclusion in the output by generating them "into the blue" here. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        genHandleType(null, "java.lang.Class");
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        genHandleType(null, "java.lang.ClassLoader");
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        genHandleType(null, "java.lang.Object");
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        genHandleType(null, "java.lang.String");
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        genHandleType(null, "java.lang.Thread");
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        genHandleType(null, "java.lang.ThreadGroup");
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
        genHandleType(null, "java.lang.Throwable");
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        pw.println("/* LLNI Header for class " + clazz.qualifiedName() + " */" + lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        pw.println("#ifndef _Included_" + cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        pw.println("#define _Included_" + cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        pw.println("#include \"typedefs.h\"");
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        pw.println("#include \"llni.h\"");
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        pw.println("#include \"jni.h\"" + lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        forwardDecls(pw, clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        structSectionForClass(pw, clazz, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        methodSectionForClass(pw, clazz, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        pw.println("#endif");
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    protected void genHandleType(PrintWriter pw, String clazzname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        String cname = mangleClassName(clazzname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        if (!doneHandleTypes.containsKey(cname)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            doneHandleTypes.put(cname, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            if (pw != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                pw.println("#ifndef DEFINED_" + cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                pw.println("    #define DEFINED_" + cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                pw.println("    GEN_HANDLE_TYPES(" + cname + ");");
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                pw.println("#endif" + lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    protected String mangleClassName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        return s.replace('.', '_')
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            .replace(pathChar, '_')
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            .replace(innerDelim, '_');
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    protected void forwardDecls(PrintWriter pw, ClassDoc clazz)
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        ClassDoc clazzfield = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        if (clazz.qualifiedName().equals("java.lang.Object"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        genHandleType(pw, clazz.qualifiedName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        ClassDoc superClass = clazz.superclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if(superClass != null){
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            String superClassName = superClass.qualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            forwardDecls(pw, superClass);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        for (int i = 0; i < fields.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            FieldDoc field = (FieldDoc)fields[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            if (!field.isStatic()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                Type t = field.type();
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                String tname = t.qualifiedTypeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                TypeSignature newTypeSig = new TypeSignature(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                String sig = newTypeSig.getTypeSignature(tname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                if (sig.charAt(0) != '[')
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                    forwardDeclsFromSig(pw, sig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        for (int i = 0; i < methods.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            MethodDoc method = (MethodDoc)methods[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            if (method.isNative()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                Type retType = method.returnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                String typesig = method.signature();
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                TypeSignature newTypeSig = new TypeSignature(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                String sig = newTypeSig.getTypeSignature(typesig, retType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                if (sig.charAt(0) != '[')
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                    forwardDeclsFromSig(pw, sig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    protected void forwardDeclsFromSig(PrintWriter pw, String sig) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        int    len = sig.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        int    i   = sig.charAt(0) == '(' ? 1 : 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        /* Skip the initial "(". */
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        while (i < len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            if (sig.charAt(i) == 'L') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                int j = i + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                while (sig.charAt(j) != ';') j++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                genHandleType(pw, sig.substring(i + 1, j));
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                i = j + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    protected void structSectionForClass(PrintWriter pw,
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                                         ClassDoc jclazz, String cname)
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        String jname = jclazz.qualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        if (cname.equals("java_lang_Object")) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            pw.println("/* struct java_lang_Object is defined in typedefs.h. */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            pw.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        pw.println("#if !defined(__i386)");
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        pw.println("#pragma pack(4)");
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        pw.println("#endif");
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        pw.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        pw.println("struct " + cname + " {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        pw.println("    ObjHeader h;");
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
        pw.print(fieldDefs(jclazz, cname));
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        if (jname.equals("java.lang.Class"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            pw.println("    Class *LLNI_mask(cClass);" +
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                       "  /* Fake field; don't access (see oobj.h) */");
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        pw.println("};" + lineSep + lineSep + "#pragma pack()");
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        pw.println();
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    private static class FieldDefsRes {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        public String className;        /* Name of the current class. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        public FieldDefsRes parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        public String s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        public int byteSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
        public boolean bottomMost;
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        public boolean printedOne = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        FieldDefsRes(ClassDoc clazz, FieldDefsRes parent, boolean bottomMost) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
            this.className = clazz.qualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
            this.parent = parent;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            this.bottomMost = bottomMost;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            int byteSize = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            if (parent == null) this.s = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
            else this.s = parent.s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    /* Returns "true" iff added a field. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    private boolean doField(FieldDefsRes res, FieldDoc field,
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                            String cname, boolean padWord)
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        String fieldDef = addStructMember(field, cname, padWord);
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        if (fieldDef != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            if (!res.printedOne) { /* add separator */
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                if (res.bottomMost) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                    if (res.s.length() != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                        res.s = res.s + "    /* local members: */" + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                    res.s = res.s + "    /* inherited members from " +
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                        res.className + ": */" + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                res.printedOne = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            res.s = res.s + fieldDef;
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        // Otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    private int doTwoWordFields(FieldDefsRes res, ClassDoc clazz,
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                                int offset, String cname, boolean padWord)
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        FieldDoc[] fields = clazz.fields();
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        for (int i = 0; i <fields.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            FieldDoc field = fields[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            String tc =field.type().typeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
            boolean twoWords = (tc.equals("long") || tc.equals("double"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            if (twoWords && doField(res, field, cname, first && padWord)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                offset += 8; first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        return offset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    protected String fieldDefs(ClassDoc clazz, String cname)
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        FieldDefsRes res = fieldDefs(clazz, cname, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
        return res.s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    protected FieldDefsRes fieldDefs(ClassDoc clazz, String cname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                                     boolean bottomMost)
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        FieldDefsRes res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        int offset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        boolean didTwoWordFields = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        ClassDoc superclazz = clazz.superclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        if (superclazz != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            String supername = superclazz.qualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            res = new FieldDefsRes(clazz,
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
                                   fieldDefs(superclazz, cname, false),
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                                   bottomMost);
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            offset = res.parent.byteSize;
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
            res = new FieldDefsRes(clazz, null, bottomMost);
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            offset = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        FieldDoc[] fields = clazz.fields();
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        for (int i = 0; i < fields.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            FieldDoc field = fields[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            if (doubleAlign && !didTwoWordFields && (offset % 8) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                offset = doTwoWordFields(res, clazz, offset, cname, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                didTwoWordFields = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            String tc = field.type().typeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            boolean twoWords = (tc.equals("long") ||tc.equals("double"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            if (!doubleAlign || !twoWords) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                if (doField(res, field, cname, false)) offset += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        if (doubleAlign && !didTwoWordFields) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            if ((offset % 8) != 0) offset += 4;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            offset = doTwoWordFields(res, clazz, offset, cname, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        res.byteSize = offset;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    /* OVERRIDE: This method handles instance fields */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    protected String addStructMember(FieldDoc member, String cname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
                                     boolean padWord)
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        String res = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        if (member.isStatic()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
            res = addStaticStructMember(member, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
            //   if (res == null) /* JNI didn't handle it, print comment. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
            //  res = "    /* Inaccessible static: " + member + " */" + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
            if (padWord) res = "    java_int padWord" + padFieldNum++ + ";" + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            res = "    " + llniType(member.type(), false, false) + " " + llniFieldName(member);
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
            if (isLongOrDouble(member.type())) res = res + "[2]";
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
            res = res + ";" + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
    static private final boolean isWindows =
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        System.getProperty("os.name").startsWith("Windows");
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
    /*
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
     * This method only handles static final fields.
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
    protected String addStaticStructMember(FieldDoc field, String cname)
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        String res = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        Object exp = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        if (!field.isStatic())
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
            return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        if (!field.isFinal())
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
            return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        exp = field.constantValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
        if (exp != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            /* Constant. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
            String     cn     = cname + "_" + field.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
            String     suffix = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
            long           val = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
            /* Can only handle int, long, float, and double fields. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            if (exp instanceof Integer) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                suffix = "L";
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                val = ((Integer)exp).intValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
            if (exp instanceof Long) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                // Visual C++ supports the i64 suffix, not LL
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                suffix = isWindows ? "i64" : "LL";
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                val = ((Long)exp).longValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
            if (exp instanceof Float)  suffix = "f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            if (exp instanceof Double) suffix = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
            if (suffix != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                // Some compilers will generate a spurious warning
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                // for the integer constants for Integer.MIN_VALUE
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                // and Long.MIN_VALUE so we handle them specially.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                if ((suffix.equals("L") && (val == Integer.MIN_VALUE)) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                    (suffix.equals("LL") && (val == Long.MIN_VALUE))) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                    res = "    #undef  " + cn + lineSep
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                        + "    #define " + cn
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                        + " (" + (val + 1) + suffix + "-1)" + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                    res = "    #undef  " + cn + lineSep
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                        + "    #define " + cn + " "+ exp.toString() + suffix + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
    protected void methodSectionForClass(PrintWriter pw,
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                                         ClassDoc clazz, String cname)
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
        String methods = methodDecls(clazz, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        if (methods.length() != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
            pw.println("/* Native method declarations: */" + lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            pw.println("#ifdef __cplusplus");
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
            pw.println("extern \"C\" {");
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
            pw.println("#endif" + lineSep);
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            pw.println(methods);
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
            pw.println("#ifdef __cplusplus");
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
            pw.println("}");
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
            pw.println("#endif");
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
    protected String methodDecls(ClassDoc clazz, String cname)
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        String res = "";
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
        for (int i = 0; i < methods.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
            MethodDoc method = (MethodDoc)methods[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            if (method.isNative())
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                res = res + methodDecl(method, clazz, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    protected String methodDecl(MethodDoc method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
                                ClassDoc clazz, String cname)
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        String res = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
        Type retType = method.returnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        String typesig = method.signature();
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        TypeSignature newTypeSig = new TypeSignature(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        String sig = newTypeSig.getTypeSignature(typesig, retType);
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
        boolean longName = needLongName(method, clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
        if (sig.charAt(0) != '(')
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
            Util.error("invalid.method.signature", sig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        res = "JNIEXPORT " + jniType(retType) + " JNICALL" + lineSep + jniMethodName(method, cname, longName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            + "(JNIEnv *, " + cRcvrDecl(method, cname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        Parameter[] params = method.parameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
        Type argTypes[] = new Type[params.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
        for(int p = 0; p <  params.length; p++){
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
            argTypes[p] =  params[p].type();
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
        /* It would have been nice to include the argument names in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
           declaration, but there seems to be a bug in the "BinaryField"
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
           class, causing the getArguments() method to return "null" for
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
           most (non-constructor) methods. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
        for (int i = 0; i < argTypes.length; i++)
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            res = res + ", " + jniType(argTypes[i]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
        res = res + ");" + lineSep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
    protected final boolean needLongName(MethodDoc method,
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
                                         ClassDoc clazz)
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        throws ClassNotFoundException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        String methodName = method.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
        for (int i = 0; i < methods.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
            MethodDoc memberMethod = (MethodDoc) methods[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            if ((memberMethod != method) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
                memberMethod.isNative() && (methodName == memberMethod.name()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    protected final String jniMethodName(MethodDoc method, String cname,
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
                                         boolean longName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
        String res = "Java_" + cname + "_" + method.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        if (longName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
            Type mType =  method.returnType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
            Parameter[] params = method.parameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
            Type argTypes[] = new Type[params.length];
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
            for(int p = 0; p <  params.length; p++){
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
                argTypes[p] =  params[p].type();
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
            res = res + "__";
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
            for (int i = 0; i < argTypes.length; i++){
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
                Type t = argTypes[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
                String tname = t.typeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                TypeSignature newTypeSig = new TypeSignature(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                String sig = newTypeSig.getTypeSignature(tname);
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                res = res + nameToIdentifier(sig);
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
    protected final String jniType(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
        String elmT =t.typeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        if (t.dimension().indexOf("[]") != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
            if(elmT.equals("boolean"))return "jbooleanArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
            else if(elmT.equals("byte"))return "jbyteArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
            else if(elmT.equals("char"))return "jcharArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            else if(elmT.equals("short"))return "jshortArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            else if(elmT.equals("int"))return "jintArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
            else if(elmT.equals("long"))return "jlongArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
            else if(elmT.equals("float"))return "jfloatArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            else if(elmT.equals("double"))return "jdoubleArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
            else if((t.dimension().indexOf("[][]") != -1) || (t.asClassDoc() != null))  return "jobjectArray";
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
            if(elmT.equals("void"))return "void";
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            else if(elmT.equals("boolean"))return "jboolean";
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            else if(elmT.equals("byte"))return "jbyte";
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            else if(elmT.equals("char"))return "jchar";
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            else if(elmT.equals("short"))return "jshort";
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            else if(elmT.equals("int"))return "jint";
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            else if(elmT.equals("long"))return "jlong";
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
            else if(elmT.equals("float"))return "jfloat";
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            else if(elmT.equals("double"))return "jdouble";
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            else if (t.asClassDoc() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
                if (elmT.equals("String"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
                    return "jstring";
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
                else if (t.asClassDoc().subclassOf(root.classNamed("java.lang.Class")))
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                    return "jclass";
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
                    return "jobject";
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
        Util.bug("jni.unknown.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
        return null; /* dead code. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
    protected String llniType(Type t, boolean handleize, boolean longDoubleOK) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
        String res = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        String elmt = t.typeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
        if (t.dimension().indexOf("[]") != -1) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
            if((t.dimension().indexOf("[][]") != -1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
               || (t.asClassDoc() != null)) res = "IArrayOfRef";
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            else if(elmt.equals("boolean")) res =  "IArrayOfBoolean";
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            else if(elmt.equals("byte")) res =  "IArrayOfByte";
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
            else if(elmt.equals("char")) res =  "IArrayOfChar";
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            else if(elmt.equals("int")) res =  "IArrayOfInt";
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
            else if(elmt.equals("long")) res =  "IArrayOfLong";
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            else if(elmt.equals("float")) res =  "IArrayOfFloat";
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            else if(elmt.equals("double")) res =  "IArrayOfDouble";
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            if (!handleize) res = "DEREFERENCED_" + res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
            if(elmt.equals("void")) res =  "void";
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            else if( (elmt.equals("boolean")) || (elmt.equals("byte"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                     ||(elmt.equals("char")) || (elmt.equals("short"))
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
                     || (elmt.equals("int")))   res = "java_int";
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
            else   if(elmt.equals("long")) res = longDoubleOK
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                                               ? "java_long" : "val32 /* java_long */";
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            else   if(elmt.equals("float")) res =  "java_float";
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
            else   if(elmt.equals("double")) res =  res = longDoubleOK
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                                                 ? "java_double" : "val32 /* java_double */";
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            else if(t.asClassDoc() != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                res = "I" +  mangleClassName(t.asClassDoc().qualifiedName());
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
                if (!handleize) res = "DEREFERENCED_" + res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
    protected final String cRcvrDecl(MemberDoc field, String cname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        return (field.isStatic() ? "jclass" : "jobject");
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
    protected String maskName(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        return "LLNI_mask(" + s + ")";
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
    protected String llniFieldName(MemberDoc field) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
        return maskName(field.name());
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
    protected final boolean isLongOrDouble(Type t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        String tc = t.typeName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
        return (tc.equals("long") || tc.equals("double"));
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
    /* Do unicode to ansi C identifier conversion.
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
       %%% This may not be right, but should be called more often. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
    protected final String nameToIdentifier(String name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        int len = name.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        StringBuffer buf = new StringBuffer(len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
        for (int i = 0; i < len; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
            char c = name.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
            if (isASCIILetterOrDigit(c))
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
                buf.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
            else if (c == '/')
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
                buf.append('_');
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            else if (c == '.')
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
                buf.append('_');
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
            else if (c == '_')
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
                buf.append("_1");
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
            else if (c == ';')
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
                buf.append("_2");
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            else if (c == '[')
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
                buf.append("_3");
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
                buf.append("_0" + ((int)c));
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        return new String(buf);
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    protected final boolean isASCIILetterOrDigit(char c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
        if (((c >= 'A') && (c <= 'Z')) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
            ((c >= 'a') && (c <= 'z')) ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            ((c >= '0') && (c <= '9')))
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
}