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)
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> */
29 #include "coretypes.h"
35 #include "java-tree.h"
37 #include "diagnostic-core.h"
39 static void append_unicode_mangled_name (const char *, int);
41 static int unicode_mangling_length (const char *, int);
44 extern struct obstack
*mangle_obstack
;
47 utf8_cmp (const unsigned char *str
, int length
, const char *name
)
49 const unsigned char *limit
= str
+ length
;
52 for (i
= 0; name
[i
]; ++i
)
54 int ch
= UTF8_GET (str
, limit
);
59 return str
== limit
? 0 : 1;
62 /* A sorted list of all C++ keywords. If you change this, be sure
63 also to change the list in
64 libjava/classpath/tools/gnu/classpath/tools/javah/Keywords.java. */
65 static const char *const cxx_keywords
[] =
173 /* Return true if NAME is a C++ keyword. */
175 cxx_keyword_p (const char *name
, int length
)
177 int last
= ARRAY_SIZE (cxx_keywords
);
179 int mid
= (last
+ first
) / 2;
182 for (mid
= (last
+ first
) / 2;
184 old
= mid
, mid
= (last
+ first
) / 2)
186 int kwl
= strlen (cxx_keywords
[mid
]);
187 int min_length
= kwl
> length
? length
: kwl
;
188 int r
= utf8_cmp ((const unsigned char *) name
, min_length
, cxx_keywords
[mid
]);
193 /* We've found a match if all the remaining characters are `$'. */
194 for (i
= min_length
; i
< length
&& name
[i
] == '$'; ++i
)
209 /* If NAME happens to be a C++ keyword, add `$'. */
210 #define MANGLE_CXX_KEYWORDS(NAME, LEN) \
213 if (cxx_keyword_p ((NAME), (LEN))) \
215 char *tmp_buf = (char *)alloca ((LEN)+1); \
216 memcpy (tmp_buf, (NAME), (LEN)); \
225 /* If the assembler doesn't support UTF8 in symbol names, some
226 characters might need to be escaped. */
230 /* Assuming (NAME, LEN) is a Utf8-encoding string, emit the string
231 appropriately mangled (with Unicode escapes if needed) to
232 MANGLE_OBSTACK. Note that `java', `lang' and `Object' are used so
233 frequently that they could be cached. */
236 append_gpp_mangled_name (const char *name
, int len
)
238 int encoded_len
, needs_escapes
;
241 MANGLE_CXX_KEYWORDS (name
, len
);
243 encoded_len
= unicode_mangling_length (name
, len
);
244 needs_escapes
= encoded_len
> 0;
246 sprintf (buf
, "%d", (needs_escapes
? encoded_len
: len
));
247 obstack_grow (mangle_obstack
, buf
, strlen (buf
));
250 append_unicode_mangled_name (name
, len
);
252 obstack_grow (mangle_obstack
, name
, len
);
255 /* Assuming (NAME, LEN) is a Utf8-encoded string, emit the string
256 appropriately mangled (with Unicode escapes) to MANGLE_OBSTACK.
257 Characters needing an escape are encoded `__UNN_' to `__UNNNN_', in
258 which case `__U' will be mangled `__U_'. */
261 append_unicode_mangled_name (const char *name
, int len
)
263 const unsigned char *ptr
;
264 const unsigned char *limit
= (const unsigned char *)name
+ len
;
266 for (ptr
= (const unsigned char *) name
; ptr
< limit
; )
268 int ch
= UTF8_GET(ptr
, limit
);
270 if ((ISALNUM (ch
) && ch
!= 'U') || ch
== '$')
272 obstack_1grow (mangle_obstack
, ch
);
275 /* Everything else needs encoding */
279 if (ch
== '_' || ch
== 'U')
281 /* Prepare to recognize __U */
282 if (ch
== '_' && (uuU
< 3))
285 obstack_1grow (mangle_obstack
, ch
);
287 /* We recognize __U that we wish to encode
288 __U_. Finish the encoding. */
289 else if (ch
== 'U' && (uuU
== 2))
292 obstack_grow (mangle_obstack
, "U_", 2);
294 /* Otherwise, just reset uuU and emit the character we
299 obstack_1grow (mangle_obstack
, ch
);
303 sprintf (buf
, "__U%x_", ch
);
304 obstack_grow (mangle_obstack
, buf
, strlen (buf
));
310 /* Assuming (NAME, LEN) is a Utf8-encoding string, calculate the
311 length of the string as mangled (a la g++) including Unicode
312 escapes. If no escapes are needed, return 0. */
315 unicode_mangling_length (const char *name
, int len
)
317 const unsigned char *ptr
;
318 const unsigned char *limit
= (const unsigned char *)name
+ len
;
319 int need_escapes
= 0; /* Whether we need an escape or not */
320 int num_chars
= 0; /* Number of characters in the mangled name */
321 int uuU
= 0; /* Help us to find __U. 0: '_', 1: '__' */
322 for (ptr
= (const unsigned char *) name
; ptr
< limit
; )
324 int ch
= UTF8_GET(ptr
, limit
);
327 error ("internal error - invalid Utf8 name");
328 if ((ISALNUM (ch
) && ch
!= 'U') || ch
== '$')
333 /* Everything else needs encoding */
336 int encoding_length
= 2;
338 if (ch
== '_' || ch
== 'U')
340 /* It's always at least one character. */
343 /* Prepare to recognize __U */
344 if (ch
== '_' && (uuU
< 3))
347 /* We recognize __U that we wish to encode __U_, we
348 count one more character. */
349 else if (ch
== 'U' && (uuU
== 2))
355 /* Otherwise, just reset uuU */
367 num_chars
+= (4 + encoding_length
);
380 /* The assembler supports UTF8, we don't use escapes. Mangling is
381 simply <N>NAME. <N> is the number of UTF8 encoded characters that
382 are found in NAME. Note that `java', `lang' and `Object' are used
383 so frequently that they could be cached. */
386 append_gpp_mangled_name (const char *name
, int len
)
388 const unsigned char *ptr
;
389 const unsigned char *limit
;
393 MANGLE_CXX_KEYWORDS (name
, len
);
395 limit
= (const unsigned char *)name
+ len
;
397 /* Compute the length of the string we wish to mangle. */
398 for (encoded_len
= 0, ptr
= (const unsigned char *) name
;
399 ptr
< limit
; encoded_len
++)
401 int ch
= UTF8_GET(ptr
, limit
);
404 error ("internal error - invalid Utf8 name");
407 sprintf (buf
, "%d", encoded_len
);
408 obstack_grow (mangle_obstack
, buf
, strlen (buf
));
409 obstack_grow (mangle_obstack
, name
, len
);
412 #endif /* HAVE_AS_UTF8 */