langtools/src/share/classes/com/sun/tools/javac/jvm/JNIWriter.java
author mcimadamore
Sat, 17 Nov 2012 19:01:03 +0000
changeset 14547 86d8d242b0c4
parent 14539 507556c4e622
child 15385 ee1eebe7e210
permissions -rw-r--r--
8003280: Add lambda tests Summary: Turn on lambda expression, method reference and default method support Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12213
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     1
/*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     4
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    10
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    15
 * accompanied this code).
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    16
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    20
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    23
 * questions.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    24
 */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    25
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    26
package com.sun.tools.javac.jvm;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    27
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    28
import java.io.IOException;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    29
import java.io.Writer;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    30
import java.util.ArrayList;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    31
import java.util.List;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    32
import java.util.Stack;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    33
import java.util.StringTokenizer;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    34
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    35
import javax.lang.model.element.Element;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    36
import javax.lang.model.element.ExecutableElement;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    37
import javax.lang.model.element.Modifier;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    38
import javax.lang.model.element.Name;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    39
import javax.lang.model.element.TypeElement;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    40
import javax.lang.model.element.VariableElement;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    41
import javax.lang.model.type.ArrayType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    42
import javax.lang.model.type.DeclaredType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    43
import javax.lang.model.type.NoType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    44
import javax.lang.model.type.PrimitiveType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    45
import javax.lang.model.type.TypeKind;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    46
import javax.lang.model.type.TypeMirror;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    47
import javax.lang.model.type.TypeVariable;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    48
import javax.lang.model.type.TypeVisitor;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    49
import javax.lang.model.util.ElementFilter;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    50
import javax.lang.model.util.Elements;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    51
import javax.lang.model.util.SimpleTypeVisitor8;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    52
import javax.lang.model.util.Types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    53
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    54
import javax.tools.FileObject;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    55
import javax.tools.JavaFileManager;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    56
import javax.tools.StandardLocation;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    57
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    58
import com.sun.tools.javac.code.Attribute;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    59
import com.sun.tools.javac.code.Flags;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    60
import com.sun.tools.javac.code.Kinds;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    61
import com.sun.tools.javac.code.Scope;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    62
import com.sun.tools.javac.code.Symbol.ClassSymbol;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    63
import com.sun.tools.javac.code.Symtab;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    64
import com.sun.tools.javac.model.JavacElements;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    65
import com.sun.tools.javac.model.JavacTypes;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    66
import com.sun.tools.javac.util.Assert;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    67
import com.sun.tools.javac.util.Context;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    68
import com.sun.tools.javac.util.Log;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    69
import com.sun.tools.javac.util.Options;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    70
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    71
import static com.sun.tools.javac.main.Option.*;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    72
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    73
/** This class provides operations to write native header files for classes.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    74
 *
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    75
 *  <p><b>This is NOT part of any supported API.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    76
 *  If you write code that depends on this, you do so at your own risk.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    77
 *  This code and its internal interfaces are subject to change or
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    78
 *  deletion without notice.</b>
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    79
 */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    80
public class JNIWriter {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    81
    protected static final Context.Key<JNIWriter> jniWriterKey =
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    82
        new Context.Key<JNIWriter>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    83
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    84
    /** Access to files. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    85
    private final JavaFileManager fileManager;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    86
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    87
    JavacElements elements;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    88
    JavacTypes types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    89
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    90
    /** The log to use for verbose output.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    91
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    92
    private final Log log;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    93
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    94
    /** Switch: verbose output.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    95
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    96
    private boolean verbose;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    97
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    98
    /** Switch: check all nested classes of top level class
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
    99
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   100
    private boolean checkAll;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   101
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   102
    private Mangle mangler;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   103
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   104
    private Context context;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   105
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   106
    private Symtab syms;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   107
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   108
    private String lineSep;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   109
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   110
    private final boolean isWindows =
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   111
        System.getProperty("os.name").startsWith("Windows");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   112
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   113
    /** Get the ClassWriter instance for this context. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   114
    public static JNIWriter instance(Context context) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   115
        JNIWriter instance = context.get(jniWriterKey);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   116
        if (instance == null)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   117
            instance = new JNIWriter(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   118
        return instance;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   119
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   120
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   121
    /** Construct a class writer, given an options table.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   122
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   123
    private JNIWriter(Context context) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   124
        context.put(jniWriterKey, this);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   125
        fileManager = context.get(JavaFileManager.class);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   126
        log = Log.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   127
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   128
        Options options = Options.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   129
        verbose = options.isSet(VERBOSE);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   130
        checkAll = options.isSet("javah:full");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   131
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   132
        this.context = context; // for lazyInit()
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   133
        syms = Symtab.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   134
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   135
        lineSep = System.getProperty("line.separator");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   136
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   137
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   138
    private void lazyInit() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   139
        if (mangler == null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   140
            elements = JavacElements.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   141
            types = JavacTypes.instance(context);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   142
            mangler = new Mangle(elements, types);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   143
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   144
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   145
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   146
    public boolean needsHeader(ClassSymbol c) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   147
        if (c.isLocal() || (c.flags() & Flags.SYNTHETIC) != 0)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   148
            return false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   149
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   150
        if (checkAll)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   151
            return needsHeader(c.outermostClass(), true);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   152
        else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   153
            return needsHeader(c, false);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   154
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   155
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   156
    private boolean needsHeader(ClassSymbol c, boolean checkNestedClasses) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   157
        if (c.isLocal() || (c.flags() & Flags.SYNTHETIC) != 0)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   158
            return false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   159
14539
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   160
        /* temporary code for backwards compatibility */
13689
4d519199a6aa 7151010: Add compiler support for repeating annotations
jfranck
parents: 12213
diff changeset
   161
        for (Attribute.Compound a: c.annotations.getAttributes()) {
14539
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   162
            if (a.type.tsym == syms.nativeHeaderType_old.tsym)
12213
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   163
                return true;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   164
        }
14539
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   165
        /* end of temporary code for backwards compatibility */
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   166
12213
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   167
        for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   168
            if (i.sym.kind == Kinds.MTH && (i.sym.flags() & Flags.NATIVE) != 0)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   169
                return true;
14539
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   170
            for (Attribute.Compound a: i.sym.annotations.getAttributes()) {
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   171
                if (a.type.tsym == syms.nativeHeaderType.tsym)
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   172
                    return true;
507556c4e622 8003412: javac needs to understand java.lang.annotation.Native
jjg
parents: 13689
diff changeset
   173
            }
12213
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   174
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   175
        if (checkNestedClasses) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   176
            for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   177
                if ((i.sym.kind == Kinds.TYP) && needsHeader(((ClassSymbol) i.sym), true))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   178
                    return true;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   179
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   180
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   181
        return false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   182
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   183
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   184
    /** Emit a class file for a given class.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   185
     *  @param c      The class from which a class file is generated.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   186
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   187
    public FileObject write(ClassSymbol c)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   188
        throws IOException
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   189
    {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   190
        String className = c.flatName().toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   191
        FileObject outFile
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   192
            = fileManager.getFileForOutput(StandardLocation.NATIVE_HEADER_OUTPUT,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   193
                "", className.replaceAll("[.$]", "_") + ".h", null);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   194
        Writer out = outFile.openWriter();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   195
        try {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   196
            write(out, c);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   197
            if (verbose)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   198
                log.printVerbose("wrote.file", outFile);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   199
            out.close();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   200
            out = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   201
        } finally {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   202
            if (out != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   203
                // if we are propogating an exception, delete the file
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   204
                out.close();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   205
                outFile.delete();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   206
                outFile = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   207
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   208
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   209
        return outFile; // may be null if write failed
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   210
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   211
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   212
    public void write(Writer out, ClassSymbol sym)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   213
            throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   214
        lazyInit();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   215
        try {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   216
            String cname = mangler.mangle(sym.fullname, Mangle.Type.CLASS);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   217
            println(out, fileTop());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   218
            println(out, includes());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   219
            println(out, guardBegin(cname));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   220
            println(out, cppGuardBegin());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   221
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   222
            writeStatics(out, sym);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   223
            writeMethods(out, sym, cname);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   224
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   225
            println(out, cppGuardEnd());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   226
            println(out, guardEnd(cname));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   227
        } catch (TypeSignature.SignatureException e) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   228
            throw new IOException(e);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   229
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   230
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   231
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   232
    protected void writeStatics(Writer out, ClassSymbol sym) throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   233
        List<VariableElement> classfields = getAllFields(sym);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   234
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   235
        for (VariableElement v: classfields) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   236
            if (!v.getModifiers().contains(Modifier.STATIC))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   237
                continue;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   238
            String s = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   239
            s = defineForStatic(sym, v);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   240
            if (s != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   241
                println(out, s);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   242
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   243
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   244
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   245
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   246
    /**
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   247
     * Including super class fields.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   248
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   249
    List<VariableElement> getAllFields(TypeElement subclazz) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   250
        List<VariableElement> fields = new ArrayList<VariableElement>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   251
        TypeElement cd = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   252
        Stack<TypeElement> s = new Stack<TypeElement>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   253
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   254
        cd = subclazz;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   255
        while (true) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   256
            s.push(cd);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   257
            TypeElement c = (TypeElement) (types.asElement(cd.getSuperclass()));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   258
            if (c == null)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   259
                break;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   260
            cd = c;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   261
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   262
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   263
        while (!s.empty()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   264
            cd = s.pop();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   265
            fields.addAll(ElementFilter.fieldsIn(cd.getEnclosedElements()));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   266
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   267
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   268
        return fields;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   269
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   270
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   271
    protected String defineForStatic(TypeElement c, VariableElement f) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   272
        CharSequence cnamedoc = c.getQualifiedName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   273
        CharSequence fnamedoc = f.getSimpleName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   274
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   275
        String cname = mangler.mangle(cnamedoc, Mangle.Type.CLASS);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   276
        String fname = mangler.mangle(fnamedoc, Mangle.Type.FIELDSTUB);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   277
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   278
        Assert.check(f.getModifiers().contains(Modifier.STATIC));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   279
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   280
        if (f.getModifiers().contains(Modifier.FINAL)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   281
            Object value = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   282
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   283
            value = f.getConstantValue();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   284
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   285
            if (value != null) { /* so it is a ConstantExpression */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   286
                String constString = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   287
                if ((value instanceof Integer)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   288
                    || (value instanceof Byte)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   289
                    || (value instanceof Short)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   290
                    /* covers byte, short, int */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   291
                    constString = value.toString() + "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   292
                } else if (value instanceof Boolean) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   293
                    constString = ((Boolean) value) ? "1L" : "0L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   294
                } else if (value instanceof Character) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   295
                    Character ch = (Character) value;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   296
                    constString = String.valueOf(((int) ch) & 0xffff) + "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   297
                } else if (value instanceof Long) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   298
                    // Visual C++ supports the i64 suffix, not LL.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   299
                    if (isWindows)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   300
                        constString = value.toString() + "i64";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   301
                    else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   302
                        constString = value.toString() + "LL";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   303
                } else if (value instanceof Float) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   304
                    /* bug for bug */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   305
                    float fv = ((Float)value).floatValue();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   306
                    if (Float.isInfinite(fv))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   307
                        constString = ((fv < 0) ? "-" : "") + "Inff";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   308
                    else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   309
                        constString = value.toString() + "f";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   310
                } else if (value instanceof Double) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   311
                    /* bug for bug */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   312
                    double d = ((Double)value).doubleValue();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   313
                    if (Double.isInfinite(d))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   314
                        constString = ((d < 0) ? "-" : "") + "InfD";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   315
                    else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   316
                        constString = value.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   317
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   318
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   319
                if (constString != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   320
                    StringBuilder s = new StringBuilder("#undef ");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   321
                    s.append(cname); s.append("_"); s.append(fname); s.append(lineSep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   322
                    s.append("#define "); s.append(cname); s.append("_");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   323
                    s.append(fname); s.append(" "); s.append(constString);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   324
                    return s.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   325
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   326
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   327
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   328
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   329
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   330
        return null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   331
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   332
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   333
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   334
    protected void writeMethods(Writer out, ClassSymbol sym, String cname)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   335
            throws IOException, TypeSignature.SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   336
        List<ExecutableElement> classmethods = ElementFilter.methodsIn(sym.getEnclosedElements());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   337
        for (ExecutableElement md: classmethods) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   338
            if(md.getModifiers().contains(Modifier.NATIVE)){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   339
                TypeMirror mtr = types.erasure(md.getReturnType());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   340
                String sig = signature(md);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   341
                TypeSignature newtypesig = new TypeSignature(elements);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   342
                CharSequence methodName = md.getSimpleName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   343
                boolean longName = false;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   344
                for (ExecutableElement md2: classmethods) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   345
                    if ((md2 != md)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   346
                        && (methodName.equals(md2.getSimpleName()))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   347
                        && (md2.getModifiers().contains(Modifier.NATIVE)))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   348
                        longName = true;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   349
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   350
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   351
                println(out, "/*");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   352
                println(out, " * Class:     " + cname);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   353
                println(out, " * Method:    " +
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   354
                           mangler.mangle(methodName, Mangle.Type.FIELDSTUB));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   355
                println(out, " * Signature: " + newtypesig.getTypeSignature(sig, mtr));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   356
                println(out, " */");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   357
                println(out, "JNIEXPORT " + jniType(mtr) +
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   358
                           " JNICALL " +
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   359
                           mangler.mangleMethod(md, sym,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   360
                                               (longName) ?
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   361
                                               Mangle.Type.METHOD_JNI_LONG :
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   362
                                               Mangle.Type.METHOD_JNI_SHORT));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   363
                print(out, "  (JNIEnv *, ");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   364
                List<? extends VariableElement> paramargs = md.getParameters();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   365
                List<TypeMirror> args = new ArrayList<TypeMirror>();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   366
                for (VariableElement p: paramargs) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   367
                    args.add(types.erasure(p.asType()));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   368
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   369
                if (md.getModifiers().contains(Modifier.STATIC))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   370
                    print(out, "jclass");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   371
                else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   372
                    print(out, "jobject");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   373
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   374
                for (TypeMirror arg: args) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   375
                    print(out, ", ");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   376
                    print(out, jniType(arg));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   377
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   378
                println(out, ");"
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   379
                        + lineSep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   380
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   381
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   382
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   383
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   384
    // c.f. MethodDoc.signature
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   385
    String signature(ExecutableElement e) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   386
        StringBuilder sb = new StringBuilder("(");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   387
        String sep = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   388
        for (VariableElement p: e.getParameters()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   389
            sb.append(sep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   390
            sb.append(types.erasure(p.asType()).toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   391
            sep = ",";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   392
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   393
        sb.append(")");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   394
        return sb.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   395
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   396
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   397
    protected final String jniType(TypeMirror t) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   398
        TypeElement throwable = elements.getTypeElement("java.lang.Throwable");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   399
        TypeElement jClass = elements.getTypeElement("java.lang.Class");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   400
        TypeElement jString = elements.getTypeElement("java.lang.String");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   401
        Element tclassDoc = types.asElement(t);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   402
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   403
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   404
        switch (t.getKind()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   405
            case ARRAY: {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   406
                TypeMirror ct = ((ArrayType) t).getComponentType();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   407
                switch (ct.getKind()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   408
                    case BOOLEAN:  return "jbooleanArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   409
                    case BYTE:     return "jbyteArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   410
                    case CHAR:     return "jcharArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   411
                    case SHORT:    return "jshortArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   412
                    case INT:      return "jintArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   413
                    case LONG:     return "jlongArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   414
                    case FLOAT:    return "jfloatArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   415
                    case DOUBLE:   return "jdoubleArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   416
                    case ARRAY:
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   417
                    case DECLARED: return "jobjectArray";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   418
                    default: throw new Error(ct.toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   419
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   420
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   421
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   422
            case VOID:     return "void";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   423
            case BOOLEAN:  return "jboolean";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   424
            case BYTE:     return "jbyte";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   425
            case CHAR:     return "jchar";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   426
            case SHORT:    return "jshort";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   427
            case INT:      return "jint";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   428
            case LONG:     return "jlong";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   429
            case FLOAT:    return "jfloat";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   430
            case DOUBLE:   return "jdouble";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   431
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   432
            case DECLARED: {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   433
                if (tclassDoc.equals(jString))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   434
                    return "jstring";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   435
                else if (types.isAssignable(t, throwable.asType()))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   436
                    return "jthrowable";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   437
                else if (types.isAssignable(t, jClass.asType()))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   438
                    return "jclass";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   439
                else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   440
                    return "jobject";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   441
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   442
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   443
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   444
        Assert.check(false, "jni unknown type");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   445
        return null; /* dead code. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   446
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   447
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   448
    protected String fileTop() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   449
        return "/* DO NOT EDIT THIS FILE - it is machine generated */";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   450
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   451
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   452
    protected String includes() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   453
        return "#include <jni.h>";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   454
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   455
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   456
    /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   457
     * Deal with the C pre-processor.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   458
     */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   459
    protected String cppGuardBegin() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   460
        return "#ifdef __cplusplus" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   461
                + "extern \"C\" {" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   462
                + "#endif";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   463
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   464
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   465
    protected String cppGuardEnd() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   466
        return "#ifdef __cplusplus" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   467
                + "}" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   468
                + "#endif";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   469
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   470
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   471
    protected String guardBegin(String cname) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   472
        return "/* Header for class " + cname + " */" + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   473
                + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   474
                + "#ifndef _Included_" + cname + lineSep
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   475
                + "#define _Included_" + cname;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   476
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   477
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   478
    protected String guardEnd(String cname) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   479
        return "#endif";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   480
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   481
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   482
    protected void print(Writer out, String text) throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   483
        out.write(text);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   484
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   485
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   486
    protected void println(Writer out, String text) throws IOException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   487
        out.write(text);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   488
        out.write(lineSep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   489
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   490
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   491
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   492
    private static class Mangle {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   493
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   494
        public static class Type {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   495
            public static final int CLASS            = 1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   496
            public static final int FIELDSTUB        = 2;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   497
            public static final int FIELD            = 3;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   498
            public static final int JNI              = 4;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   499
            public static final int SIGNATURE        = 5;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   500
            public static final int METHOD_JDK_1     = 6;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   501
            public static final int METHOD_JNI_SHORT = 7;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   502
            public static final int METHOD_JNI_LONG  = 8;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   503
        };
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   504
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   505
        private Elements elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   506
        private Types types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   507
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   508
        Mangle(Elements elems, Types types) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   509
            this.elems = elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   510
            this.types = types;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   511
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   512
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   513
        public final String mangle(CharSequence name, int mtype) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   514
            StringBuilder result = new StringBuilder(100);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   515
            int length = name.length();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   516
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   517
            for (int i = 0; i < length; i++) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   518
                char ch = name.charAt(i);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   519
                if (isalnum(ch)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   520
                    result.append(ch);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   521
                } else if ((ch == '.') &&
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   522
                           mtype == Mangle.Type.CLASS) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   523
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   524
                } else if (( ch == '$') &&
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   525
                           mtype == Mangle.Type.CLASS) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   526
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   527
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   528
                } else if (ch == '_' && mtype == Mangle.Type.FIELDSTUB) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   529
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   530
                } else if (ch == '_' && mtype == Mangle.Type.CLASS) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   531
                    result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   532
                } else if (mtype == Mangle.Type.JNI) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   533
                    String esc = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   534
                    if (ch == '_')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   535
                        esc = "_1";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   536
                    else if (ch == '.')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   537
                        esc = "_";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   538
                    else if (ch == ';')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   539
                        esc = "_2";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   540
                    else if (ch == '[')
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   541
                        esc = "_3";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   542
                    if (esc != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   543
                        result.append(esc);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   544
                    } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   545
                        result.append(mangleChar(ch));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   546
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   547
                } else if (mtype == Mangle.Type.SIGNATURE) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   548
                    if (isprint(ch)) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   549
                        result.append(ch);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   550
                    } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   551
                        result.append(mangleChar(ch));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   552
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   553
                } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   554
                    result.append(mangleChar(ch));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   555
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   556
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   557
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   558
            return result.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   559
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   560
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   561
        public String mangleMethod(ExecutableElement method, TypeElement clazz,
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   562
                                          int mtype) throws TypeSignature.SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   563
            StringBuilder result = new StringBuilder(100);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   564
            result.append("Java_");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   565
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   566
            if (mtype == Mangle.Type.METHOD_JDK_1) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   567
                result.append(mangle(clazz.getQualifiedName(), Mangle.Type.CLASS));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   568
                result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   569
                result.append(mangle(method.getSimpleName(),
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   570
                                     Mangle.Type.FIELD));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   571
                result.append("_stub");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   572
                return result.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   573
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   574
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   575
            /* JNI */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   576
            result.append(mangle(getInnerQualifiedName(clazz), Mangle.Type.JNI));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   577
            result.append('_');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   578
            result.append(mangle(method.getSimpleName(),
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   579
                                 Mangle.Type.JNI));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   580
            if (mtype == Mangle.Type.METHOD_JNI_LONG) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   581
                result.append("__");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   582
                String typesig = signature(method);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   583
                TypeSignature newTypeSig = new TypeSignature(elems);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   584
                String sig = newTypeSig.getTypeSignature(typesig,  method.getReturnType());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   585
                sig = sig.substring(1);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   586
                sig = sig.substring(0, sig.lastIndexOf(')'));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   587
                sig = sig.replace('/', '.');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   588
                result.append(mangle(sig, Mangle.Type.JNI));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   589
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   590
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   591
            return result.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   592
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   593
        //where
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   594
            private String getInnerQualifiedName(TypeElement clazz) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   595
                return elems.getBinaryName(clazz).toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   596
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   597
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   598
        public final String mangleChar(char ch) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   599
            String s = Integer.toHexString(ch);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   600
            int nzeros = 5 - s.length();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   601
            char[] result = new char[6];
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   602
            result[0] = '_';
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   603
            for (int i = 1; i <= nzeros; i++)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   604
                result[i] = '0';
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   605
            for (int i = nzeros+1, j = 0; i < 6; i++, j++)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   606
                result[i] = s.charAt(j);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   607
            return new String(result);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   608
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   609
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   610
        // Warning: duplicated in Gen
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   611
        private String signature(ExecutableElement e) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   612
            StringBuilder sb = new StringBuilder();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   613
            String sep = "(";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   614
            for (VariableElement p: e.getParameters()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   615
                sb.append(sep);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   616
                sb.append(types.erasure(p.asType()).toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   617
                sep = ",";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   618
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   619
            sb.append(")");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   620
            return sb.toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   621
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   622
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   623
        /* Warning: Intentional ASCII operation. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   624
        private static boolean isalnum(char ch) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   625
            return ch <= 0x7f && /* quick test */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   626
                ((ch >= 'A' && ch <= 'Z') ||
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   627
                 (ch >= 'a' && ch <= 'z') ||
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   628
                 (ch >= '0' && ch <= '9'));
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   629
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   630
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   631
        /* Warning: Intentional ASCII operation. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   632
        private static boolean isprint(char ch) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   633
            return ch >= 32 && ch <= 126;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   634
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   635
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   636
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   637
    private static class TypeSignature {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   638
        static class SignatureException extends Exception {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   639
            private static final long serialVersionUID = 1L;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   640
            SignatureException(String reason) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   641
                super(reason);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   642
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   643
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   644
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   645
        Elements elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   646
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   647
        /* Signature Characters */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   648
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   649
        private static final String SIG_VOID                   = "V";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   650
        private static final String SIG_BOOLEAN                = "Z";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   651
        private static final String SIG_BYTE                   = "B";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   652
        private static final String SIG_CHAR                   = "C";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   653
        private static final String SIG_SHORT                  = "S";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   654
        private static final String SIG_INT                    = "I";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   655
        private static final String SIG_LONG                   = "J";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   656
        private static final String SIG_FLOAT                  = "F";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   657
        private static final String SIG_DOUBLE                 = "D";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   658
        private static final String SIG_ARRAY                  = "[";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   659
        private static final String SIG_CLASS                  = "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   660
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   661
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   662
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   663
        public TypeSignature(Elements elems){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   664
            this.elems = elems;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   665
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   666
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   667
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   668
         * Returns the type signature of a field according to JVM specs
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   669
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   670
        public String getTypeSignature(String javasignature) throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   671
            return getParamJVMSignature(javasignature);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   672
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   673
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   674
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   675
         * Returns the type signature of a method according to JVM specs
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   676
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   677
        public String getTypeSignature(String javasignature, TypeMirror returnType)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   678
                throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   679
            String signature = null; //Java type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   680
            String typeSignature = null; //Internal type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   681
            List<String> params = new ArrayList<String>(); //List of parameters.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   682
            String paramsig = null; //Java parameter signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   683
            String paramJVMSig = null; //Internal parameter signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   684
            String returnSig = null; //Java return type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   685
            String returnJVMType = null; //Internal return type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   686
            int dimensions = 0; //Array dimension.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   687
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   688
            int startIndex = -1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   689
            int endIndex = -1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   690
            StringTokenizer st = null;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   691
            int i = 0;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   692
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   693
            // Gets the actual java signature without parentheses.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   694
            if (javasignature != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   695
                startIndex = javasignature.indexOf("(");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   696
                endIndex = javasignature.indexOf(")");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   697
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   698
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   699
            if (((startIndex != -1) && (endIndex != -1))
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   700
                &&(startIndex+1 < javasignature.length())
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   701
                &&(endIndex < javasignature.length())) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   702
                signature = javasignature.substring(startIndex+1, endIndex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   703
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   704
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   705
            // Separates parameters.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   706
            if (signature != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   707
                if (signature.indexOf(",") != -1) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   708
                    st = new StringTokenizer(signature, ",");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   709
                    if (st != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   710
                        while (st.hasMoreTokens()) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   711
                            params.add(st.nextToken());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   712
                        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   713
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   714
                } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   715
                    params.add(signature);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   716
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   717
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   718
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   719
            /* JVM type signature. */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   720
            typeSignature = "(";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   721
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   722
            // Gets indivisual internal parameter signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   723
            while (params.isEmpty() != true) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   724
                paramsig = params.remove(i).trim();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   725
                paramJVMSig  = getParamJVMSignature(paramsig);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   726
                if (paramJVMSig != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   727
                    typeSignature += paramJVMSig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   728
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   729
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   730
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   731
            typeSignature += ")";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   732
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   733
            // Get internal return type signature.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   734
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   735
            returnJVMType = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   736
            if (returnType != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   737
                dimensions = dimensions(returnType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   738
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   739
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   740
            //Gets array dimension of return type.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   741
            while (dimensions-- > 0) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   742
                returnJVMType += "[";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   743
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   744
            if (returnType != null) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   745
                returnSig = qualifiedTypeName(returnType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   746
                returnJVMType += getComponentType(returnSig);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   747
            } else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   748
                System.out.println("Invalid return type.");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   749
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   750
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   751
            typeSignature += returnJVMType;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   752
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   753
            return typeSignature;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   754
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   755
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   756
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   757
         * Returns internal signature of a parameter.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   758
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   759
        private String getParamJVMSignature(String paramsig) throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   760
            String paramJVMSig = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   761
            String componentType ="";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   762
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   763
            if(paramsig != null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   764
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   765
                if(paramsig.indexOf("[]") != -1) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   766
                    // Gets array dimension.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   767
                    int endindex = paramsig.indexOf("[]");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   768
                    componentType = paramsig.substring(0, endindex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   769
                    String dimensionString =  paramsig.substring(endindex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   770
                    if(dimensionString != null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   771
                        while(dimensionString.indexOf("[]") != -1){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   772
                            paramJVMSig += "[";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   773
                            int beginindex = dimensionString.indexOf("]") + 1;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   774
                            if(beginindex < dimensionString.length()){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   775
                                dimensionString = dimensionString.substring(beginindex);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   776
                            }else
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   777
                                dimensionString = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   778
                        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   779
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   780
                } else componentType = paramsig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   781
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   782
                paramJVMSig += getComponentType(componentType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   783
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   784
            return paramJVMSig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   785
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   786
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   787
        /*
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   788
         * Returns internal signature of a component.
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   789
         */
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   790
        private String getComponentType(String componentType) throws SignatureException {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   791
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   792
            String JVMSig = "";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   793
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   794
            if(componentType != null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   795
                if(componentType.equals("void")) JVMSig += SIG_VOID ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   796
                else if(componentType.equals("boolean"))  JVMSig += SIG_BOOLEAN ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   797
                else if(componentType.equals("byte")) JVMSig += SIG_BYTE ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   798
                else if(componentType.equals("char"))  JVMSig += SIG_CHAR ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   799
                else if(componentType.equals("short"))  JVMSig += SIG_SHORT ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   800
                else if(componentType.equals("int"))  JVMSig += SIG_INT ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   801
                else if(componentType.equals("long"))  JVMSig += SIG_LONG ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   802
                else if(componentType.equals("float")) JVMSig += SIG_FLOAT ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   803
                else if(componentType.equals("double"))  JVMSig += SIG_DOUBLE ;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   804
                else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   805
                    if(!componentType.equals("")){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   806
                        TypeElement classNameDoc = elems.getTypeElement(componentType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   807
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   808
                        if(classNameDoc == null){
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   809
                            throw new SignatureException(componentType);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   810
                        }else {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   811
                            String classname = classNameDoc.getQualifiedName().toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   812
                            String newclassname = classname.replace('.', '/');
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   813
                            JVMSig += "L";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   814
                            JVMSig += newclassname;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   815
                            JVMSig += ";";
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   816
                        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   817
                    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   818
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   819
            }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   820
            return JVMSig;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   821
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   822
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   823
        int dimensions(TypeMirror t) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   824
            if (t.getKind() != TypeKind.ARRAY)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   825
                return 0;
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   826
            return 1 + dimensions(((ArrayType) t).getComponentType());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   827
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   828
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   829
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   830
        String qualifiedTypeName(TypeMirror type) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   831
            TypeVisitor<Name, Void> v = new SimpleTypeVisitor8<Name, Void>() {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   832
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   833
                public Name visitArray(ArrayType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   834
                    return t.getComponentType().accept(this, p);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   835
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   836
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   837
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   838
                public Name visitDeclared(DeclaredType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   839
                    return ((TypeElement) t.asElement()).getQualifiedName();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   840
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   841
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   842
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   843
                public Name visitPrimitive(PrimitiveType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   844
                    return elems.getName(t.toString());
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   845
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   846
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   847
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   848
                public Name visitNoType(NoType t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   849
                    if (t.getKind() == TypeKind.VOID)
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   850
                        return elems.getName("void");
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   851
                    return defaultAction(t, p);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   852
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   853
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   854
                @Override
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   855
                public Name visitTypeVariable(TypeVariable t, Void p) {
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   856
                    return t.getUpperBound().accept(this, p);
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   857
                }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   858
            };
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   859
            return v.visit(type).toString();
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   860
        }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   861
    }
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   862
516b112d6c68 7150368: javac should include basic ability to generate native headers
jjg
parents:
diff changeset
   863
}