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