OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [javah/] [CniPrintStream.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* CniPrintStream.java - PrintStream that emits CNI declarations
2
 Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
 This file is part of GNU Classpath.
5
 
6
 GNU Classpath is free software; you can redistribute it and/or modify
7
 it under the terms of the GNU General Public License as published by
8
 the Free Software Foundation; either version 2, or (at your option)
9
 any later version.
10
 
11
 GNU Classpath is distributed in the hope that it will be useful, but
12
 WITHOUT ANY WARRANTY; without even the implied warranty of
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 General Public License for more details.
15
 
16
 You should have received a copy of the GNU General Public License
17
 along with GNU Classpath; see the file COPYING.  If not, write to the
18
 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 02110-1301 USA.
20
 
21
 Linking this library statically or dynamically with other modules is
22
 making a combined work based on this library.  Thus, the terms and
23
 conditions of the GNU General Public License cover the whole
24
 combination.
25
 
26
 As a special exception, the copyright holders of this library give you
27
 permission to link this library with independent modules to produce an
28
 executable, regardless of the license terms of these independent
29
 modules, and to copy and distribute the resulting executable under
30
 terms of your choice, provided that you also meet, for each linked
31
 independent module, the terms and conditions of the license of that
32
 module.  An independent module is a module which is not derived from
33
 or based on this library.  If you modify this library, you may extend
34
 this exception to your version of the library, but you are not
35
 obligated to do so.  If you do not wish to do so, delete this
36
 exception statement from your version. */
37
 
38
 
39
package gnu.classpath.tools.javah;
40
 
41
import java.io.OutputStream;
42
import java.io.PrintStream;
43
import java.lang.reflect.Modifier;
44
import java.util.Arrays;
45
import java.util.HashSet;
46
 
47
import org.objectweb.asm.Type;
48
 
49
public class CniPrintStream
50
    extends PrintStream
51
{
52
  int currentModifiers = Modifier.PRIVATE;
53
 
54
  // True if we saw an array type.
55
  boolean sawArray;
56
 
57
  // All the classes referenced by this header.
58
  HashSet<String> allClasses = new HashSet<String>();
59
 
60
  String[] previousPackage = new String[0];
61
 
62
  public CniPrintStream(OutputStream out)
63
  {
64
    super(out);
65
  }
66
 
67
  public void addClass(ClassWrapper cw)
68
  {
69
    allClasses.add(cw.name);
70
  }
71
 
72
  public void setModifiers(int newMods)
73
  {
74
    newMods &= (Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE);
75
    if (newMods != currentModifiers)
76
      {
77
        switch (newMods)
78
          {
79
          case Modifier.PUBLIC:
80
            println("public:");
81
            break;
82
          case Modifier.PROTECTED:
83
            println("public: // actually protected");
84
            break;
85
          case Modifier.PRIVATE:
86
            println("private:");
87
            break;
88
          default:
89
            println("public: // actually package-private");
90
            break;
91
          }
92
        currentModifiers = newMods;
93
      }
94
  }
95
 
96
  private String getName(Type type)
97
  {
98
    if (type == Type.BOOLEAN_TYPE)
99
      return "jboolean";
100
    else if (type == Type.BYTE_TYPE)
101
      return "jbyte";
102
    else if (type == Type.CHAR_TYPE)
103
      return "jchar";
104
    else if (type == Type.SHORT_TYPE)
105
      return "jshort";
106
    else if (type == Type.INT_TYPE)
107
      return "jint";
108
    else if (type == Type.LONG_TYPE)
109
      return "jlong";
110
    else if (type == Type.FLOAT_TYPE)
111
      return "jfloat";
112
    else if (type == Type.DOUBLE_TYPE)
113
      return "jdouble";
114
    else
115
      {
116
        assert type == Type.VOID_TYPE;
117
        return "void";
118
      }
119
  }
120
 
121
  public String getClassName(Type type)
122
  {
123
    String name = type.toString();
124
    name = name.substring(1, name.length() - 1);
125
    // Add the plain class name; we'll handle it when
126
    // we process namespaces.
127
    allClasses.add(name);
128
    return name;
129
  }
130
 
131
  // Print the C++ form of TYPE, mangling C++ keywords.
132
  public void print(Type type)
133
  {
134
    int arrayCount = 0;
135
    if (type.getSort() == Type.ARRAY)
136
      {
137
        arrayCount = type.getDimensions();
138
        for (int i = 0; i < arrayCount; ++i)
139
          print("JArray< ");
140
        type = type.getElementType();
141
        sawArray = true;
142
      }
143
    if (type.getSort() == Type.OBJECT)
144
      {
145
        print("::");
146
        printName(getClassName(type));
147
        print(" *");
148
      }
149
    else
150
      {
151
        print(getName(type));
152
      }
153
    if (arrayCount > 0)
154
      {
155
        while (arrayCount-- > 0)
156
          {
157
            print(" > *");
158
          }
159
      }
160
  }
161
 
162
  // Print NAME, converting into C++ syntax and mangling C++ keywords
163
  // as we go.
164
  public final static void printName(PrintStream out, String name)
165
  {
166
    String[] parts = name.split("::|/");
167
    for (int i = 0; i < parts.length; i++)
168
      {
169
        if (i != 0)
170
          out.print("::");
171
        out.print(Keywords.getCxxName(parts[i]));
172
      }
173
  }
174
 
175
  // Println NAME, converting into C++ syntax and mangling C++
176
  // keywords as we go.
177
  public final static void printlnName(PrintStream out, String name)
178
  {
179
    printName(out, name);
180
    out.println();
181
  }
182
 
183
  // Print NAME, converting into C++ syntax and mangling C++ keywords
184
  // as we go.
185
  final void printName(String name)
186
  {
187
    printName(this, name);
188
  }
189
 
190
  private void indent(PrintStream out, int n)
191
  {
192
    for (int i = 0; i < n; ++i)
193
      {
194
        out.print("  ");
195
      }
196
  }
197
 
198
  private void moveToPackage(PrintStream out, String[] pkgParts)
199
  {
200
    // Find greatest common part.
201
    int commonIndex;
202
    for (commonIndex = 0; commonIndex < previousPackage.length; ++commonIndex)
203
      {
204
        if (commonIndex >= pkgParts.length)
205
          break;
206
        if (! previousPackage[commonIndex].equals(pkgParts[commonIndex]))
207
          break;
208
      }
209
    // Close old parts after the common part.
210
    for (int j = previousPackage.length - 1; j >= commonIndex; --j)
211
      {
212
        indent(out, j + 1);
213
        out.println("}");
214
      }
215
    // Open new parts.
216
    for (int j = commonIndex; j < pkgParts.length; ++j)
217
      {
218
        indent(out, j + 1);
219
        out.print("namespace ");
220
        printlnName(out, pkgParts[j]);
221
        indent(out, j + 1);
222
        out.println("{");
223
      }
224
    previousPackage = pkgParts;
225
  }
226
 
227
  private void writeClass(PrintStream out, String klass)
228
  {
229
    int index = klass.lastIndexOf('/');
230
    String pkg = index == -1 ? "" : klass.substring(0, index);
231
    String[] pkgParts = index == -1 ? new String[0] : pkg.split("/");
232
    String className = index == -1 ? klass : klass.substring(index + 1);
233
    moveToPackage(out, pkgParts);
234
    indent(out, pkgParts.length + 2);
235
    out.print("class ");
236
    printName(out, className);
237
    out.println(";");
238
  }
239
 
240
  public void printNamespaces(PrintStream out)
241
  {
242
    if (sawArray)
243
      {
244
        out.println("#include <gcj/array.h>");
245
        out.println();
246
      }
247
 
248
    String[] classes = allClasses.toArray(new String[0]);
249
    Arrays.sort(classes);
250
 
251
    boolean first = true;
252
    boolean seen = false;
253
    for (int i = 0; i < classes.length; ++i)
254
      {
255
        String klass = classes[i];
256
        if (klass.startsWith("java/lang/") || klass.startsWith("java/io/")
257
            || klass.startsWith("java/util/"))
258
          continue;
259
        if (first)
260
          {
261
            out.println("extern \"Java\"");
262
            out.println("{");
263
            first = false;
264
            seen = true;
265
          }
266
        writeClass(out, klass);
267
      }
268
    if (seen)
269
      {
270
        moveToPackage(out, new String[0]);
271
        out.println("}");
272
      }
273
  }
274
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.