OSDN Git Service

* Make-lang.in, boehm.c, buffer.c,
[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, 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, 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 "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 PARAMS ((const char *, int));
39 #ifndef HAVE_AS_UTF8
40 static int  unicode_mangling_length PARAMS ((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 (name, len)
57      const char *name;
58      int len;
59 {
60   int encoded_len = unicode_mangling_length (name, len);
61   int needs_escapes = encoded_len > 0;
62   char buf[6];
63
64   sprintf (buf, "%d", (needs_escapes ? encoded_len : len));
65   obstack_grow (mangle_obstack, buf, strlen (buf));
66
67   if (needs_escapes)
68     append_unicode_mangled_name (name, len);
69   else
70     obstack_grow (mangle_obstack, name, len);
71 }
72
73 /* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
74    appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
75    Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
76    which case `__U' will be mangled `__U_'.  */
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') || ch == '$')
91         obstack_1grow (mangle_obstack, ch);
92       /* Everything else needs encoding */
93       else
94         {
95           char buf [9];
96           if (ch == '_' || ch == 'U')
97             {
98               /* Prepare to recognize __U */
99               if (ch == '_' && (uuU < 3))
100                 {
101                   uuU++;
102                   obstack_1grow (mangle_obstack, ch);
103                 }
104               /* We recognize __U that we wish to encode
105                  __U_. Finish the encoding. */
106               else if (ch == 'U' && (uuU == 2))
107                 {
108                   uuU = 0;
109                   obstack_grow (mangle_obstack, "U_", 2);
110                 }
111               /* Otherwise, just reset uuU and emit the character we
112                  have. */
113               else
114                 {
115                   uuU = 0;
116                   obstack_1grow (mangle_obstack, ch);
117                 }
118               continue;
119             }
120           sprintf (buf, "__U%x_", ch);
121           obstack_grow (mangle_obstack, buf, strlen (buf));
122           uuU = 0;
123         }
124     }
125 }
126
127 /* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
128    length of the string as mangled (a la g++) including Unicode
129    escapes.  If no escapes are needed, return 0.  */
130
131 static int
132 unicode_mangling_length (name, len)
133      const char *name; 
134      int len; 
135 {
136   const unsigned char *ptr;
137   const unsigned char *limit = (const unsigned char *)name + len;
138   int need_escapes = 0;         /* Whether we need an escape or not */
139   int num_chars = 0;            /* Number of characters in the mangled name */
140   int uuU = 0;                  /* Help us to find __U. 0: '_', 1: '__' */
141   for (ptr = (const unsigned char *) name;  ptr < limit;  )
142     {
143       int ch = UTF8_GET(ptr, limit);
144
145       if (ch < 0)
146         error ("internal error - invalid Utf8 name");
147       if ((ISALNUM (ch) && ch != 'U') || ch == '$')
148         num_chars++;
149       /* Everything else needs encoding */
150       else
151         {
152           int encoding_length = 2;
153
154           if (ch == '_' || ch == 'U')
155             {
156               /* It's always at least one character. */
157               num_chars++;
158
159               /* Prepare to recognize __U */
160               if (ch == '_' && (uuU < 3))
161                 uuU++;
162
163               /* We recognize __U that we wish to encode __U_, we
164                  count one more character. */
165               else if (ch == 'U' && (uuU == 2))
166                 {
167                   num_chars++;
168                   need_escapes = 1;
169                   uuU = 0;
170                 }
171               /* Otherwise, just reset uuU */
172               else
173                 uuU = 0;
174
175               continue;
176             }
177           
178           if (ch > 0xff)
179             encoding_length++;
180           if (ch > 0xfff)
181             encoding_length++;
182           
183           num_chars += (4 + encoding_length);
184           need_escapes = 1;
185           uuU = 0;
186         }
187     }
188   if (need_escapes)
189     return num_chars;
190   else
191     return 0;
192 }
193
194 #else
195
196 /* The assembler supports UTF8, we don't use escapes. Mangling is
197    simply <N>NAME. <N> is the number of UTF8 encoded characters that
198    are found in NAME. Note that `java', `lang' and `Object' are used
199    so frequently that they could be cached.  */
200
201 void
202 append_gpp_mangled_name (name, len)
203      const char *name;
204      int len;
205 {
206   const unsigned char *ptr;
207   const unsigned char *limit = (const unsigned char *)name + len;
208   int encoded_len;
209   char buf [6];
210   
211   /* Compute the length of the string we wish to mangle. */
212   for (encoded_len =  0, ptr = (const unsigned char *) name;
213        ptr < limit; encoded_len++)
214     {
215       int ch = UTF8_GET(ptr, limit);
216
217       if (ch < 0)
218         error ("internal error - invalid Utf8 name");
219     }
220
221   sprintf (buf, "%d", encoded_len);
222   obstack_grow (mangle_obstack, buf, strlen (buf));
223   obstack_grow (mangle_obstack, name, len);
224 }
225
226 #endif /* HAVE_AS_UTF8 */