OSDN Git Service

* c-format.c (maybe_read_dollar_number): Use safe-ctype macros
[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 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC 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 GNU CC 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 GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 "jcf.h"
31 #include "tree.h"
32 #include "java-tree.h"
33 #include "obstack.h"
34 #include "toplev.h"
35 #include "obstack.h"
36
37 static void append_unicode_mangled_name PARAMS ((const char *, int));
38 #ifndef HAVE_AS_UTF8
39 static int  unicode_mangling_length PARAMS ((const char *, int));
40 #endif
41
42 extern struct obstack *mangle_obstack;
43
44 /* If the assembler doesn't support UTF8 in symbol names, some
45    characters might need to be escaped.  */
46
47 #ifndef HAVE_AS_UTF8
48
49 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
50    appropriately mangled (with Unicode escapes if needed) to
51    MANGLE_OBSTACK.  Note that `java', `lang' and `Object' are used so
52    frequently that they could be cached.  */
53
54 void
55 append_gpp_mangled_name (name, len)
56      const char *name;
57      int len;
58 {
59   int encoded_len = unicode_mangling_length (name, len);
60   int needs_escapes = encoded_len > 0;
61   char buf[6];
62
63   sprintf (buf, "%d", (needs_escapes ? encoded_len : len));
64   obstack_grow (mangle_obstack, buf, strlen (buf));
65
66   if (needs_escapes)
67     append_unicode_mangled_name (name, len);
68   else
69     obstack_grow (mangle_obstack, name, len);
70 }
71
72 /* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
73    appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
74    Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
75    which case `__U' will be mangled `__U_'. `$' is mangled `$' or
76    __U24_ according to NO_DOLLAR_IN_LABEL.  */
77
78 static void
79 append_unicode_mangled_name (name, len)
80      const char *name;
81      int len;
82 {
83   const unsigned char *ptr;
84   const unsigned char *limit = (const unsigned char *)name + len;
85   int uuU = 0;
86   for (ptr = (const unsigned char *) name;  ptr < limit;  )
87     {
88       int ch = UTF8_GET(ptr, limit);
89
90       if ((ISALNUM (ch) && ch != 'U')
91 #ifndef NO_DOLLAR_IN_LABEL
92           || ch == '$'
93 #endif
94           )
95         obstack_1grow (mangle_obstack, ch);
96       /* Everything else needs encoding */
97       else
98         {
99           char buf [9];
100           if (ch == '_' || ch == 'U')
101             {
102               /* Prepare to recognize __U */
103               if (ch == '_' && (uuU < 3))
104                 {
105                   uuU++;
106                   obstack_1grow (mangle_obstack, ch);
107                 }
108               /* We recognize __U that we wish to encode
109                  __U_. Finish the encoding. */
110               else if (ch == 'U' && (uuU == 2))
111                 {
112                   uuU = 0;
113                   obstack_grow (mangle_obstack, "U_", 2);
114                 }
115               /* Otherwise, just reset uuU and emit the character we
116                  have. */
117               else
118                 {
119                   uuU = 0;
120                   obstack_1grow (mangle_obstack, ch);
121                 }
122               continue;
123             }
124           sprintf (buf, "__U%x_", ch);
125           obstack_grow (mangle_obstack, buf, strlen (buf));
126           uuU = 0;
127         }
128     }
129 }
130
131 /* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
132    length of the string as mangled (a la g++) including Unicode
133    escapes.  If no escapes are needed, return 0.  */
134
135 static int
136 unicode_mangling_length (name, len)
137      const char *name; 
138      int len; 
139 {
140   const unsigned char *ptr;
141   const unsigned char *limit = (const unsigned char *)name + len;
142   int need_escapes = 0;         /* Whether we need an escape or not */
143   int num_chars = 0;            /* Number of characters in the mangled name */
144   int uuU = 0;                  /* Help us to find __U. 0: '_', 1: '__' */
145   for (ptr = (const unsigned char *) name;  ptr < limit;  )
146     {
147       int ch = UTF8_GET(ptr, limit);
148
149       if (ch < 0)
150         error ("internal error - invalid Utf8 name");
151       if ((ISALNUM (ch) && ch != 'U')
152 #ifndef NO_DOLLAR_IN_LABEL
153           || ch == '$'
154 #endif
155           )
156         num_chars++;
157       /* Everything else needs encoding */
158       else
159         {
160           int encoding_length = 2;
161
162           if (ch == '_' || ch == 'U')
163             {
164               /* It's always at least one character. */
165               num_chars++;
166
167               /* Prepare to recognize __U */
168               if (ch == '_' && (uuU < 3))
169                 uuU++;
170
171               /* We recognize __U that we wish to encode __U_, we
172                  count one more character. */
173               else if (ch == 'U' && (uuU == 2))
174                 {
175                   num_chars++;
176                   need_escapes = 1;
177                   uuU = 0;
178                 }
179               /* Otherwise, just reset uuU */
180               else
181                 uuU = 0;
182
183               continue;
184             }
185           
186           if (ch > 0xff)
187             encoding_length++;
188           if (ch > 0xfff)
189             encoding_length++;
190           
191           num_chars += (4 + encoding_length);
192           need_escapes = 1;
193           uuU = 0;
194         }
195     }
196   if (need_escapes)
197     return num_chars;
198   else
199     return 0;
200 }
201
202 #else
203
204 /* The assembler supports UTF8, we don't use escapes. Mangling is
205    simply <N>NAME. <N> is the number of UTF8 encoded characters that
206    are found in NAME. Note that `java', `lang' and `Object' are used
207    so frequently that they could be cached.  */
208
209 void
210 append_gpp_mangled_name (name, len)
211      const char *name;
212      int len;
213 {
214   const unsigned char *ptr;
215   const unsigned char *limit = (const unsigned char *)name + len;
216   int encoded_len;
217   char buf [6];
218   
219   /* Compute the length of the string we wish to mangle. */
220   for (encoded_len =  0, ptr = (const unsigned char *) name;
221        ptr < limit; encoded_len++)
222     {
223       int ch = UTF8_GET(ptr, limit);
224
225       if (ch < 0)
226         error ("internal error - invalid Utf8 name");
227     }
228
229   sprintf (buf, "%d", encoded_len);
230   obstack_grow (mangle_obstack, buf, strlen (buf));
231   obstack_grow (mangle_obstack, name, len);
232 }
233
234 #endif /* HAVE_AS_UTF8 */