OSDN Git Service

2005-06-28 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / java / mangle_name.c
1 /* Shared functions related to mangling names for the GNU compiler
2    for the Java(TM) language.
3    Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. 
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
25
26 /* Written by Alexandre Petit-Bianco <apbianco@cygnus.com> */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "jcf.h"
33 #include "tree.h"
34 #include "java-tree.h"
35 #include "obstack.h"
36 #include "toplev.h"
37
38 static void append_unicode_mangled_name (const char *, int);
39 #ifndef HAVE_AS_UTF8
40 static int  unicode_mangling_length (const char *, int);
41 #endif
42
43 extern struct obstack *mangle_obstack;
44
45 /* If the assembler doesn't support UTF8 in symbol names, some
46    characters might need to be escaped.  */
47
48 #ifndef HAVE_AS_UTF8
49
50 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
51    appropriately mangled (with Unicode escapes if needed) to
52    MANGLE_OBSTACK.  Note that `java', `lang' and `Object' are used so
53    frequently that they could be cached.  */
54
55 void
56 append_gpp_mangled_name (const char *name, int len)
57 {
58   int encoded_len = unicode_mangling_length (name, len);
59   int needs_escapes = encoded_len > 0;
60   char buf[6];
61
62   sprintf (buf, "%d", (needs_escapes ? encoded_len : len));
63   obstack_grow (mangle_obstack, buf, strlen (buf));
64
65   if (needs_escapes)
66     append_unicode_mangled_name (name, len);
67   else
68     obstack_grow (mangle_obstack, name, len);
69 }
70
71 /* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
72    appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
73    Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
74    which case `__U' will be mangled `__U_'.  */
75
76 static void
77 append_unicode_mangled_name (const char *name, int len)
78 {
79   const unsigned char *ptr;
80   const unsigned char *limit = (const unsigned char *)name + len;
81   int uuU = 0;
82   for (ptr = (const unsigned char *) name;  ptr < limit;  )
83     {
84       int ch = UTF8_GET(ptr, limit);
85
86       if ((ISALNUM (ch) && ch != 'U') || ch == '$')
87         obstack_1grow (mangle_obstack, ch);
88       /* Everything else needs encoding */
89       else
90         {
91           char buf [9];
92           if (ch == '_' || ch == 'U')
93             {
94               /* Prepare to recognize __U */
95               if (ch == '_' && (uuU < 3))
96                 {
97                   uuU++;
98                   obstack_1grow (mangle_obstack, ch);
99                 }
100               /* We recognize __U that we wish to encode
101                  __U_. Finish the encoding. */
102               else if (ch == 'U' && (uuU == 2))
103                 {
104                   uuU = 0;
105                   obstack_grow (mangle_obstack, "U_", 2);
106                 }
107               /* Otherwise, just reset uuU and emit the character we
108                  have. */
109               else
110                 {
111                   uuU = 0;
112                   obstack_1grow (mangle_obstack, ch);
113                 }
114               continue;
115             }
116           sprintf (buf, "__U%x_", ch);
117           obstack_grow (mangle_obstack, buf, strlen (buf));
118           uuU = 0;
119         }
120     }
121 }
122
123 /* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
124    length of the string as mangled (a la g++) including Unicode
125    escapes.  If no escapes are needed, return 0.  */
126
127 static int
128 unicode_mangling_length (const char *name, int len)
129 {
130   const unsigned char *ptr;
131   const unsigned char *limit = (const unsigned char *)name + len;
132   int need_escapes = 0;         /* Whether we need an escape or not */
133   int num_chars = 0;            /* Number of characters in the mangled name */
134   int uuU = 0;                  /* Help us to find __U. 0: '_', 1: '__' */
135   for (ptr = (const unsigned char *) name;  ptr < limit;  )
136     {
137       int ch = UTF8_GET(ptr, limit);
138
139       if (ch < 0)
140         error ("internal error - invalid Utf8 name");
141       if ((ISALNUM (ch) && ch != 'U') || ch == '$')
142         num_chars++;
143       /* Everything else needs encoding */
144       else
145         {
146           int encoding_length = 2;
147
148           if (ch == '_' || ch == 'U')
149             {
150               /* It's always at least one character. */
151               num_chars++;
152
153               /* Prepare to recognize __U */
154               if (ch == '_' && (uuU < 3))
155                 uuU++;
156
157               /* We recognize __U that we wish to encode __U_, we
158                  count one more character. */
159               else if (ch == 'U' && (uuU == 2))
160                 {
161                   num_chars++;
162                   need_escapes = 1;
163                   uuU = 0;
164                 }
165               /* Otherwise, just reset uuU */
166               else
167                 uuU = 0;
168
169               continue;
170             }
171           
172           if (ch > 0xff)
173             encoding_length++;
174           if (ch > 0xfff)
175             encoding_length++;
176           
177           num_chars += (4 + encoding_length);
178           need_escapes = 1;
179           uuU = 0;
180         }
181     }
182   if (need_escapes)
183     return num_chars;
184   else
185     return 0;
186 }
187
188 #else
189
190 /* The assembler supports UTF8, we don't use escapes. Mangling is
191    simply <N>NAME. <N> is the number of UTF8 encoded characters that
192    are found in NAME. Note that `java', `lang' and `Object' are used
193    so frequently that they could be cached.  */
194
195 void
196 append_gpp_mangled_name (const char *name, int len)
197 {
198   const unsigned char *ptr;
199   const unsigned char *limit = (const unsigned char *)name + len;
200   int encoded_len;
201   char buf [6];
202   
203   /* Compute the length of the string we wish to mangle. */
204   for (encoded_len =  0, ptr = (const unsigned char *) name;
205        ptr < limit; encoded_len++)
206     {
207       int ch = UTF8_GET(ptr, limit);
208
209       if (ch < 0)
210         error ("internal error - invalid Utf8 name");
211     }
212
213   sprintf (buf, "%d", encoded_len);
214   obstack_grow (mangle_obstack, buf, strlen (buf));
215   obstack_grow (mangle_obstack, name, len);
216 }
217
218 #endif /* HAVE_AS_UTF8 */