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.
5 This file is part of GNU CC.
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)
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.
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.
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. */
26 /* Written by Alexandre Petit-Bianco <apbianco@cygnus.com> */
32 #include "java-tree.h"
37 static void append_unicode_mangled_name PARAMS ((const char *, int));
39 static int unicode_mangling_length PARAMS ((const char *, int));
42 extern struct obstack *mangle_obstack;
44 /* If the assembler doesn't support UTF8 in symbol names, some
45 characters might need to be escaped. */
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. */
55 append_gpp_mangled_name (name, len)
59 int encoded_len = unicode_mangling_length (name, len);
60 int needs_escapes = encoded_len > 0;
63 sprintf (buf, "%d", (needs_escapes ? encoded_len : len));
64 obstack_grow (mangle_obstack, buf, strlen (buf));
67 append_unicode_mangled_name (name, len);
69 obstack_grow (mangle_obstack, name, len);
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. */
79 append_unicode_mangled_name (name, len)
83 const unsigned char *ptr;
84 const unsigned char *limit = (const unsigned char *)name + len;
86 for (ptr = (const unsigned char *) name; ptr < limit; )
88 int ch = UTF8_GET(ptr, limit);
90 if ((ISALNUM (ch) && ch != 'U')
91 #ifndef NO_DOLLAR_IN_LABEL
95 obstack_1grow (mangle_obstack, ch);
96 /* Everything else needs encoding */
100 if (ch == '_' || ch == 'U')
102 /* Prepare to recognize __U */
103 if (ch == '_' && (uuU < 3))
106 obstack_1grow (mangle_obstack, ch);
108 /* We recognize __U that we wish to encode
109 __U_. Finish the encoding. */
110 else if (ch == 'U' && (uuU == 2))
113 obstack_grow (mangle_obstack, "U_", 2);
115 /* Otherwise, just reset uuU and emit the character we
120 obstack_1grow (mangle_obstack, ch);
124 sprintf (buf, "__U%x_", ch);
125 obstack_grow (mangle_obstack, buf, strlen (buf));
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. */
136 unicode_mangling_length (name, len)
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; )
147 int ch = UTF8_GET(ptr, limit);
150 error ("internal error - invalid Utf8 name");
151 if ((ISALNUM (ch) && ch != 'U')
152 #ifndef NO_DOLLAR_IN_LABEL
157 /* Everything else needs encoding */
160 int encoding_length = 2;
162 if (ch == '_' || ch == 'U')
164 /* It's always at least one character. */
167 /* Prepare to recognize __U */
168 if (ch == '_' && (uuU < 3))
171 /* We recognize __U that we wish to encode __U_, we
172 count one more character. */
173 else if (ch == 'U' && (uuU == 2))
179 /* Otherwise, just reset uuU */
191 num_chars += (4 + encoding_length);
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. */
210 append_gpp_mangled_name (name, len)
214 const unsigned char *ptr;
215 const unsigned char *limit = (const unsigned char *)name + len;
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++)
223 int ch = UTF8_GET(ptr, limit);
226 error ("internal error - invalid Utf8 name");
229 sprintf (buf, "%d", encoded_len);
230 obstack_grow (mangle_obstack, buf, strlen (buf));
231 obstack_grow (mangle_obstack, name, len);
234 #endif /* HAVE_AS_UTF8 */