Fix type in the changelog entry,
[official-gcc.git] / gcc / java / mangle_name.c
blob26c1931d9f4c2a2a3873a977acca369633f36df6
1 /* Shared functions related to mangling names for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 This file is part of GCC.
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 3, or (at your option)
10 any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Alexandre Petit-Bianco <apbianco@cygnus.com> */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "jcf.h"
31 #include "alias.h"
32 #include "tree.h"
33 #include "options.h"
34 #include "java-tree.h"
35 #include "obstack.h"
36 #include "diagnostic-core.h"
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
43 extern struct obstack *mangle_obstack;
45 static int
46 utf8_cmp (const unsigned char *str, int length, const char *name)
48 const unsigned char *limit = str + length;
49 int i;
51 for (i = 0; name[i]; ++i)
53 int ch = UTF8_GET (str, limit);
54 if (ch != name[i])
55 return ch - name[i];
58 return str == limit ? 0 : 1;
61 /* A sorted list of all C++ keywords. If you change this, be sure
62 also to change the list in
63 libjava/classpath/tools/gnu/classpath/tools/javah/Keywords.java. */
64 static const char *const cxx_keywords[] =
66 "_Complex",
67 "__alignof",
68 "__alignof__",
69 "__asm",
70 "__asm__",
71 "__attribute",
72 "__attribute__",
73 "__builtin_va_arg",
74 "__complex",
75 "__complex__",
76 "__const",
77 "__const__",
78 "__extension__",
79 "__imag",
80 "__imag__",
81 "__inline",
82 "__inline__",
83 "__label__",
84 "__null",
85 "__real",
86 "__real__",
87 "__restrict",
88 "__restrict__",
89 "__signed",
90 "__signed__",
91 "__typeof",
92 "__typeof__",
93 "__volatile",
94 "__volatile__",
95 "and",
96 "and_eq",
97 "asm",
98 "auto",
99 "bitand",
100 "bitor",
101 "bool",
102 "break",
103 "case",
104 "catch",
105 "char",
106 "class",
107 "compl",
108 "const",
109 "const_cast",
110 "continue",
111 "default",
112 "delete",
113 "do",
114 "double",
115 "dynamic_cast",
116 "else",
117 "enum",
118 "explicit",
119 "export",
120 "extern",
121 "false",
122 "float",
123 "for",
124 "friend",
125 "goto",
126 "if",
127 "inline",
128 "int",
129 "long",
130 "mutable",
131 "namespace",
132 "new",
133 "not",
134 "not_eq",
135 "operator",
136 "or",
137 "or_eq",
138 "private",
139 "protected",
140 "public",
141 "register",
142 "reinterpret_cast",
143 "return",
144 "short",
145 "signed",
146 "sizeof",
147 "static",
148 "static_cast",
149 "struct",
150 "switch",
151 "template",
152 "this",
153 "throw",
154 "true",
155 "try",
156 "typedef",
157 "typeid",
158 "typename",
159 "typeof",
160 "union",
161 "unsigned",
162 "using",
163 "virtual",
164 "void",
165 "volatile",
166 "wchar_t",
167 "while",
168 "xor",
169 "xor_eq"
172 /* Return true if NAME is a C++ keyword. */
174 cxx_keyword_p (const char *name, int length)
176 int last = ARRAY_SIZE (cxx_keywords);
177 int first = 0;
178 int mid = (last + first) / 2;
179 int old = -1;
181 for (mid = (last + first) / 2;
182 mid != old;
183 old = mid, mid = (last + first) / 2)
185 int kwl = strlen (cxx_keywords[mid]);
186 int min_length = kwl > length ? length : kwl;
187 int r = utf8_cmp ((const unsigned char *) name, min_length, cxx_keywords[mid]);
189 if (r == 0)
191 int i;
192 /* We've found a match if all the remaining characters are `$'. */
193 for (i = min_length; i < length && name[i] == '$'; ++i)
195 if (i == length)
196 return 1;
197 r = 1;
200 if (r < 0)
201 last = mid;
202 else
203 first = mid;
205 return 0;
208 /* If NAME happens to be a C++ keyword, add `$'. */
209 #define MANGLE_CXX_KEYWORDS(NAME, LEN) \
210 do \
212 if (cxx_keyword_p ((NAME), (LEN))) \
214 char *tmp_buf = (char *)alloca ((LEN)+1); \
215 memcpy (tmp_buf, (NAME), (LEN)); \
216 tmp_buf[LEN]= '$'; \
217 (NAME) = tmp_buf; \
218 (LEN)++; \
221 while (0)
224 /* If the assembler doesn't support UTF8 in symbol names, some
225 characters might need to be escaped. */
227 #ifndef HAVE_AS_UTF8
229 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
230 appropriately mangled (with Unicode escapes if needed) to
231 MANGLE_OBSTACK. Note that `java', `lang' and `Object' are used so
232 frequently that they could be cached. */
234 void
235 append_gpp_mangled_name (const char *name, int len)
237 int encoded_len, needs_escapes;
238 char buf[6];
240 MANGLE_CXX_KEYWORDS (name, len);
242 encoded_len = unicode_mangling_length (name, len);
243 needs_escapes = encoded_len > 0;
245 sprintf (buf, "%d", (needs_escapes ? encoded_len : len));
246 obstack_grow (mangle_obstack, buf, strlen (buf));
248 if (needs_escapes)
249 append_unicode_mangled_name (name, len);
250 else
251 obstack_grow (mangle_obstack, name, len);
254 /* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
255 appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
256 Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
257 which case `__U' will be mangled `__U_'. */
259 static void
260 append_unicode_mangled_name (const char *name, int len)
262 const unsigned char *ptr;
263 const unsigned char *limit = (const unsigned char *)name + len;
264 int uuU = 0;
265 for (ptr = (const unsigned char *) name; ptr < limit; )
267 int ch = UTF8_GET(ptr, limit);
269 if ((ISALNUM (ch) && ch != 'U') || ch == '$')
271 obstack_1grow (mangle_obstack, ch);
272 uuU = 0;
274 /* Everything else needs encoding */
275 else
277 char buf [9];
278 if (ch == '_' || ch == 'U')
280 /* Prepare to recognize __U */
281 if (ch == '_' && (uuU < 3))
283 uuU++;
284 obstack_1grow (mangle_obstack, ch);
286 /* We recognize __U that we wish to encode
287 __U_. Finish the encoding. */
288 else if (ch == 'U' && (uuU == 2))
290 uuU = 0;
291 obstack_grow (mangle_obstack, "U_", 2);
293 /* Otherwise, just reset uuU and emit the character we
294 have. */
295 else
297 uuU = 0;
298 obstack_1grow (mangle_obstack, ch);
300 continue;
302 sprintf (buf, "__U%x_", ch);
303 obstack_grow (mangle_obstack, buf, strlen (buf));
304 uuU = 0;
309 /* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
310 length of the string as mangled (a la g++) including Unicode
311 escapes. If no escapes are needed, return 0. */
313 static int
314 unicode_mangling_length (const char *name, int len)
316 const unsigned char *ptr;
317 const unsigned char *limit = (const unsigned char *)name + len;
318 int need_escapes = 0; /* Whether we need an escape or not */
319 int num_chars = 0; /* Number of characters in the mangled name */
320 int uuU = 0; /* Help us to find __U. 0: '_', 1: '__' */
321 for (ptr = (const unsigned char *) name; ptr < limit; )
323 int ch = UTF8_GET(ptr, limit);
325 if (ch < 0)
326 error ("internal error - invalid Utf8 name");
327 if ((ISALNUM (ch) && ch != 'U') || ch == '$')
329 num_chars++;
330 uuU = 0;
332 /* Everything else needs encoding */
333 else
335 int encoding_length = 2;
337 if (ch == '_' || ch == 'U')
339 /* It's always at least one character. */
340 num_chars++;
342 /* Prepare to recognize __U */
343 if (ch == '_' && (uuU < 3))
344 uuU++;
346 /* We recognize __U that we wish to encode __U_, we
347 count one more character. */
348 else if (ch == 'U' && (uuU == 2))
350 num_chars++;
351 need_escapes = 1;
352 uuU = 0;
354 /* Otherwise, just reset uuU */
355 else
356 uuU = 0;
358 continue;
361 if (ch > 0xff)
362 encoding_length++;
363 if (ch > 0xfff)
364 encoding_length++;
366 num_chars += (4 + encoding_length);
367 need_escapes = 1;
368 uuU = 0;
371 if (need_escapes)
372 return num_chars;
373 else
374 return 0;
377 #else
379 /* The assembler supports UTF8, we don't use escapes. Mangling is
380 simply <N>NAME. <N> is the number of UTF8 encoded characters that
381 are found in NAME. Note that `java', `lang' and `Object' are used
382 so frequently that they could be cached. */
384 void
385 append_gpp_mangled_name (const char *name, int len)
387 const unsigned char *ptr;
388 const unsigned char *limit;
389 int encoded_len;
390 char buf [6];
392 MANGLE_CXX_KEYWORDS (name, len);
394 limit = (const unsigned char *)name + len;
396 /* Compute the length of the string we wish to mangle. */
397 for (encoded_len = 0, ptr = (const unsigned char *) name;
398 ptr < limit; encoded_len++)
400 int ch = UTF8_GET(ptr, limit);
402 if (ch < 0)
403 error ("internal error - invalid Utf8 name");
406 sprintf (buf, "%d", encoded_len);
407 obstack_grow (mangle_obstack, buf, strlen (buf));
408 obstack_grow (mangle_obstack, name, len);
411 #endif /* HAVE_AS_UTF8 */